-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_test.go
More file actions
64 lines (58 loc) · 1.39 KB
/
decoder_test.go
File metadata and controls
64 lines (58 loc) · 1.39 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
package mux
import (
"bytes"
"encoding/json"
"net/http"
"testing"
)
func TestDecode(t *testing.T) {
var form testData
h, req := testDecodeRequest(t, testData{N: 1})
err := h.Decode(req, &form)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestErrDecodeContentType(t *testing.T) {
var form testData
h, req := testDecodeRequest(t, testData{N: 1})
req.Header.Del("Content-Type")
err := h.Decode(req, &form)
if err != ErrDecodeContentType {
t.Fatalf("unexpected error: %v", err)
}
req.Header.Set("Content-Type", "invalid")
err = h.Decode(req, &form)
if err != ErrDecodeContentType {
t.Fatalf("unexpected error: %v", err)
}
}
func TestErrDecodeRequestData(t *testing.T) {
var form testData
h, req := testDecodeRequest(t, nil)
err := h.Decode(req, &form)
if err != ErrDecodeRequestData {
t.Fatalf("unexpected error: %v", err)
}
}
func TestValidationError(t *testing.T) {
var form testData
h, req := testDecodeRequest(t, testData{N: 0})
err := h.Decode(req, &form)
_, ok := err.(ValidationError)
if !ok {
t.Fatalf("unexpected error: %v", err)
}
}
func testDecodeRequest(t *testing.T, v interface{}) (*Handler, *http.Request) {
h := New()
body := bytes.NewBuffer(make([]byte, 0))
if v != nil {
err := json.NewEncoder(body).Encode(v)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
req := newTestRequest(http.MethodGet, "/", body)
return h, req
}