-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathctx_test.go
43 lines (40 loc) · 1.07 KB
/
ctx_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// +build go1.7
package rest
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestCustomBadRequest(t *testing.T) {
err := &Error{Title: "bad"}
RegisterHandler(400, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := CtxErr(r)
if err == nil {
t.Fatal("expected non-nil error, got nil")
}
if rerr, ok := err.(*Error); ok {
if rerr.Title != "bad" {
t.Errorf("expected err.Title to be 'bad', got %s", rerr.Title)
}
} else {
t.Fatalf("expected err cast to Error, couldn't, err is %v", err)
}
w.Header().Set("Custom-Handler", "true")
w.WriteHeader(400)
w.Write([]byte("hello world"))
}))
defer func() {
handlerMu.Lock()
defer handlerMu.Unlock()
delete(handlerMap, 400)
}()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
BadRequest(w, req, err)
if hdr := w.Header().Get("Custom-Handler"); hdr != "true" {
t.Errorf("expected to get Custom-Handler: true header, got %s", hdr)
}
if body := w.Body.String(); body != "hello world" {
t.Errorf("expected Body to be hello world, got %s", body)
}
}