Skip to content

Commit e0fc4ee

Browse files
authored
Merge pull request google#118 from themccallister/patch-1
Add tests for mismatching accept headers
2 parents a06052d + 081fb8a commit e0fc4ee

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

examples/handler_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,35 @@ func TestExampleHandler_get_list(t *testing.T) {
8484
t.Fatalf("Expected a status of %d, got %d", e, a)
8585
}
8686
}
87+
88+
func TestHttpErrorWhenHeaderDoesNotMatch(t *testing.T) {
89+
r, err := http.NewRequest(http.MethodGet, "/blogs", nil)
90+
if err != nil {
91+
t.Fatal(err)
92+
}
93+
r.Header.Set(headerAccept, "application/xml")
94+
95+
rr := httptest.NewRecorder()
96+
handler := &ExampleHandler{}
97+
handler.ServeHTTP(rr, r)
98+
99+
if rr.Code != http.StatusUnsupportedMediaType {
100+
t.Fatal("expected Unsupported Media Type staus error")
101+
}
102+
}
103+
104+
func TestHttpErrorWhenMethodDoesNotMatch(t *testing.T) {
105+
r, err := http.NewRequest(http.MethodPatch, "/blogs", nil)
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
r.Header.Set(headerAccept, jsonapi.MediaType)
110+
111+
rr := httptest.NewRecorder()
112+
handler := &ExampleHandler{}
113+
handler.ServeHTTP(rr, r)
114+
115+
if rr.Code != http.StatusNotFound {
116+
t.Fatal("expected HTTP Status Not Found status error")
117+
}
118+
}

0 commit comments

Comments
 (0)