-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_test.go
More file actions
42 lines (39 loc) · 1004 Bytes
/
encoder_test.go
File metadata and controls
42 lines (39 loc) · 1004 Bytes
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
package mux
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestEncode(t *testing.T) {
h := New()
w := httptest.NewRecorder()
req := newTestRequest(http.MethodGet, "/", nil)
err := h.Encode(w, req, testData{N: 1}, http.StatusTeapot)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
resp := w.Result()
defer resp.Body.Close()
assertStatus(t, resp, http.StatusTeapot)
assertHeader(t, resp, "Content-Type", "application/json; charset=utf-8")
var data testData
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
err = data.Validate()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestErrEncodeMatch(t *testing.T) {
h := New()
w := httptest.NewRecorder()
req := newTestRequest(http.MethodGet, "/", nil)
req.Header.Set("Accept", "text/html")
err := h.Encode(w, req, testData{N: 1}, http.StatusTeapot)
if err != ErrEncodeMatch {
t.Fatalf("unexpected error: %v", err)
}
}