-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathweb_test.go
More file actions
96 lines (76 loc) · 2.26 KB
/
web_test.go
File metadata and controls
96 lines (76 loc) · 2.26 KB
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
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
}
func TestJSONProblems(t *testing.T) {
notFound := NewDetailedProblem(http.StatusNotFound, "That thing doesn't exist.")
server := testServer(ProblemHandler(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 Problem
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 := NewDetailedProblem(http.StatusNotFound, "That thing doesn't exist.")
server := testServer(XMLProblemHandler(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 Problem
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)
}
}