...

Source file src/github.com/bluekeyes/hatpear/hatpear_test.go

Documentation: github.com/bluekeyes/hatpear

     1  package hatpear_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/bluekeyes/hatpear"
    12  )
    13  
    14  func TestStoreGet(t *testing.T) {
    15  	h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    16  		err := errors.New("test error")
    17  
    18  		hatpear.Store(r, err)
    19  		if stored := hatpear.Get(r); stored != err {
    20  			t.Errorf("Stored error (%v) does not equal expected error (%v)", stored, err)
    21  		}
    22  
    23  		hatpear.Store(r, nil)
    24  		if stored := hatpear.Get(r); stored != err {
    25  			t.Errorf("Stored error (%v) does not equal expected error (%v)", stored, err)
    26  		}
    27  	})
    28  
    29  	r := httptest.NewRequest(http.MethodGet, "/", nil)
    30  	w := httptest.NewRecorder()
    31  
    32  	catch := hatpear.Catch(func(w http.ResponseWriter, r *http.Request, err error) {})
    33  	catch(h).ServeHTTP(w, r)
    34  }
    35  
    36  func TestTryCatch(t *testing.T) {
    37  	var called bool
    38  	var handledErr error
    39  
    40  	catch := hatpear.Catch(func(w http.ResponseWriter, r *http.Request, err error) {
    41  		called = true
    42  		handledErr = err
    43  	})
    44  
    45  	err := errors.New("test error")
    46  	h := hatpear.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
    47  		return err
    48  	})
    49  
    50  	r := httptest.NewRequest(http.MethodGet, "/", nil)
    51  	w := httptest.NewRecorder()
    52  	catch(hatpear.Try(h)).ServeHTTP(w, r)
    53  
    54  	if !called {
    55  		t.Error("Error handler function was not called by Catch()")
    56  	}
    57  
    58  	if handledErr != err {
    59  		t.Errorf("Caught error (%v) does not equal expected error (%v)", handledErr, err)
    60  	}
    61  }
    62  
    63  func TestStore_unconfigured(t *testing.T) {
    64  	defer func() {
    65  		if v := recover(); v == nil {
    66  			t.Error("Store() with unconfigured request did not panic")
    67  		}
    68  	}()
    69  
    70  	r := httptest.NewRequest(http.MethodGet, "/", nil)
    71  	hatpear.Store(r, errors.New("test error"))
    72  }
    73  
    74  func TestGet_unconfigured(t *testing.T) {
    75  	r := httptest.NewRequest(http.MethodGet, "/", nil)
    76  	if err := hatpear.Get(r); err != nil {
    77  		t.Errorf("Get() with unconfigured request returned %v instead of nil", err)
    78  	}
    79  }
    80  
    81  func TestRecover(t *testing.T) {
    82  	var handledErr error
    83  
    84  	rec := hatpear.Recover()
    85  	catch := hatpear.Catch(func(w http.ResponseWriter, r *http.Request, err error) {
    86  		handledErr = err
    87  	})
    88  
    89  	h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    90  		panic("test panic")
    91  	})
    92  
    93  	r := httptest.NewRequest(http.MethodGet, "/", nil)
    94  	w := httptest.NewRecorder()
    95  	catch(rec(h)).ServeHTTP(w, r)
    96  
    97  	if perr, ok := handledErr.(hatpear.PanicError); ok {
    98  		if !strings.HasPrefix(perr.Error(), "panic:") {
    99  			t.Errorf("Error string does not start with \"panic:\": %s", perr.Error())
   100  		}
   101  
   102  		v := perr.Value()
   103  		if v != "test panic" {
   104  			t.Errorf("Panic value (%v [%T]) does not equal expected value (test panic [string])", v, v)
   105  		}
   106  
   107  		if len(perr.StackTrace()) == 0 {
   108  			t.Error("The stack trace associated with the panic error is empty")
   109  		}
   110  
   111  		hfunc := "hatpear_test.TestRecover.func2"
   112  		trace := fmt.Sprintf("%+v", perr)
   113  
   114  		if !strings.Contains(trace, hfunc) {
   115  			t.Errorf("The stack trace does not contain the handler function (%s):\n%s", hfunc, trace)
   116  		}
   117  	} else {
   118  		t.Errorf("Handled error with type \"%T\" was not a hatpear.PanicError", handledErr)
   119  	}
   120  }
   121  

View as plain text