-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrest_test.go
141 lines (133 loc) · 3.69 KB
/
rest_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package rest
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestGone(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
Gone(w, req)
if w.Code != 410 {
t.Errorf("expected code to be 410, got %d", w.Code)
}
var e Error
err := json.NewDecoder(w.Body).Decode(&e)
if err != nil {
t.Fatal(err)
}
if e.Title != gone.Title {
t.Errorf("expected Title to be %s, got %s", serverError.Title, e.Title)
}
if e.ID != gone.ID {
t.Errorf("expected ID to be %s, got %s", serverError.ID, e.ID)
}
if e.Status != 410 {
t.Errorf("expected code to be 410, got %d", e.Status)
}
}
func TestServerError(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
ServerError(w, req, errors.New("foo bar"))
if w.Code != 500 {
t.Errorf("expected code to be 500, got %d", w.Code)
}
var e Error
err := json.NewDecoder(w.Body).Decode(&e)
if err != nil {
t.Fatal(err)
}
if e.Title == "" {
t.Errorf("expected Title to be %s, got the empty string", serverError.Title)
}
if e.Title != serverError.Title {
t.Errorf("expected Title to be %s, got %s", serverError.Title, e.Title)
}
if e.ID != serverError.ID {
t.Errorf("expected ID to be %s, got %s", serverError.ID, e.ID)
}
if e.Status != 500 {
t.Errorf("expected code to be 500, got %d", e.Status)
}
}
func TestBadRequest(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
BadRequest(w, req, &Error{
Title: "Please provide a widget",
ID: "missing_widget",
})
if w.Code != 400 {
t.Errorf("expected code to be 400, got %d", w.Code)
}
var e Error
err := json.NewDecoder(w.Body).Decode(&e)
if err != nil {
t.Fatal(err)
}
if e.Title == "" {
t.Errorf("expected Title to be %s, got the empty string", "Please provide a widget")
}
if e.Title != "Please provide a widget" {
t.Errorf("expected Title to be %s, got %s", "Please provide a widget", e.Title)
}
if e.Status != 400 {
t.Errorf("expected code to be 400, got %d", e.Status)
}
if e.ID != "missing_widget" {
t.Errorf("expected ID to be %s, got %s", "missing_widget", e.ID)
}
}
func TestNoContent(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
w.Header().Set("Content-Type", "application/json")
NoContent(w)
if w.Code != 204 {
t.Errorf("expected Code to be 204, got %d", w.Code)
}
if hdr := w.Header().Get("Content-Type"); hdr != "" {
t.Errorf("expected Content-Type to be empty, got %s", hdr)
}
}
func TestUnauthorized(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
Unauthorized(w, req, "foo")
if w.Code != 401 {
t.Errorf("expected Code to be 401, got %d", w.Code)
}
expected := `Basic realm="foo"`
if hdr := w.Header().Get("WWW-Authenticate"); hdr != expected {
t.Errorf("expected WWW-Authenticate header to be %s, got %s", expected, hdr)
}
}
func TestRegisterNilHandlerDeletes(t *testing.T) {
RegisterHandler(500, nil)
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
ServerError(w, req, errors.New("bad"))
if ctype := w.Header().Get("Content-Type"); ctype != jsonContentType {
t.Errorf("expected default JSON content-type, got %s", ctype)
}
}
func TestRegister401CallsIt(t *testing.T) {
RegisterHandler(401, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Custom-Handler", "true")
w.WriteHeader(401)
}))
defer RegisterHandler(401, nil)
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
Unauthorized(w, req, "domain")
if hdr := w.Header().Get("Custom-Handler"); hdr != "true" {
t.Errorf("expected custom handler to be called, got %v", hdr)
}
}