-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathweb_test.go
164 lines (129 loc) · 3.95 KB
/
web_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package problems
import (
"encoding/json"
"encoding/xml"
"log"
"net/http"
"net/http/httptest"
"testing"
)
func testServer(funcs ...http.HandlerFunc) *httptest.Server {
mux := http.NewServeMux()
for _, f := range funcs {
mux.HandleFunc("/", f)
}
ts := httptest.NewServer(mux)
return ts
}
func getResponse(uri string, server *httptest.Server) (*http.Response, error) {
res, err := http.Get(server.URL + uri)
if err != nil {
log.Fatal(err)
}
return res, nil
}
type MyDecoder interface {
Decode(v interface{}) error
}
func TestJSONProblems(t *testing.T) {
notFound := NewStatusProblem(http.StatusNotFound)
notFound.Detail = "That thing doesn't exist."
server := testServer(ProblemHandler(notFound))
defer server.Close()
w, err := getResponse("/", server)
if err != nil {
t.Error(err)
}
var response DefaultProblem
err = json.NewDecoder(w.Body).Decode(&response)
if err != nil {
t.Error(err)
}
if response.Status != notFound.Status {
t.Errorf("Expected response Status to be %d, but got %d", notFound.Status, response.Status)
}
if response.Title != notFound.Title {
t.Errorf("Expected response Title to be %q, but got %q", notFound.Title, response.Title)
}
if response.Detail != notFound.Detail {
t.Errorf("Expected response Detail to be %q, but got %q", notFound.Detail, response.Detail)
}
}
func TestJSONStatusProblems(t *testing.T) {
notFound := NewStatusProblem(http.StatusNotFound)
notFound.Detail = "That thing doesn't exist."
server := testServer(StatusProblemHandler(notFound))
defer server.Close()
w, err := getResponse("/", server)
if err != nil {
t.Error(err)
}
if w.StatusCode != notFound.Status {
t.Errorf("Expected HTTP status code to be %d, got %d", notFound.Status, w.StatusCode)
}
var response DefaultProblem
err = json.NewDecoder(w.Body).Decode(&response)
if err != nil {
t.Error(err)
}
if response.Status != notFound.Status {
t.Errorf("Expected response Status to be %d, but got %d", notFound.Status, response.Status)
}
if response.Title != notFound.Title {
t.Errorf("Expected response Title to be %q, but got %q", notFound.Title, response.Title)
}
if response.Detail != notFound.Detail {
t.Errorf("Expected response Detail to be %q, but got %q", notFound.Detail, response.Detail)
}
}
func TestXMLProblems(t *testing.T) {
notFound := NewStatusProblem(http.StatusNotFound)
notFound.Detail = "That thing doesn't exist."
server := testServer(XMLProblemHandler(notFound))
defer server.Close()
w, err := getResponse("/", server)
if err != nil {
t.Error(err)
}
var response DefaultProblem
err = xml.NewDecoder(w.Body).Decode(&response)
if err != nil {
t.Error(err)
}
if response.Status != notFound.Status {
t.Errorf("Expected response Status to be %d, but got %d", notFound.Status, response.Status)
}
if response.Title != notFound.Title {
t.Errorf("Expected response Title to be %q, but got %q", notFound.Title, response.Title)
}
if response.Detail != notFound.Detail {
t.Errorf("Expected response Detail to be %q, but got %q", notFound.Detail, response.Detail)
}
}
func TestXMLStatusProblems(t *testing.T) {
notFound := NewStatusProblem(404)
notFound.Detail = "That thing doesn't exist."
server := testServer(XMLStatusProblemHandler(notFound))
defer server.Close()
w, err := getResponse("/", server)
if err != nil {
t.Error(err)
}
if w.StatusCode != notFound.Status {
t.Errorf("Expected HTTP status code to be %d, got %d", notFound.Status, w.StatusCode)
}
var response DefaultProblem
err = xml.NewDecoder(w.Body).Decode(&response)
if err != nil {
t.Error(err)
}
if response.Status != notFound.Status {
t.Errorf("Expected response Status to be %d, but got %d", notFound.Status, response.Status)
}
if response.Title != notFound.Title {
t.Errorf("Expected response Title to be %q, but got %q", notFound.Title, response.Title)
}
if response.Detail != notFound.Detail {
t.Errorf("Expected response Detail to be %q, but got %q", notFound.Detail, response.Detail)
}
}