-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathform_test.go
60 lines (48 loc) · 1.39 KB
/
form_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
package legit
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestForm_NewForm(t *testing.T) {
f := NewForm()
assert.False(t, f.Legit.Strict)
assert.Equal(t, Decoders{JSON{}}, f.Decoders)
}
func TestForm_ParseAndValidate(t *testing.T) {
r := bytes.NewReader([]byte(`"foo"`))
var body Lower
err := form.ParseAndValidate(r, "application/json", &body)
assert.NoError(t, err)
assert.Equal(t, Lower("foo"), body)
}
func TestForm_ParseAndValidate_noDecoder(t *testing.T) {
err := form.ParseAndValidate(nil, "application/xml", nil)
assert.Equal(t, ErrEncoding, err)
}
func TestForm_ParseAndValidate_invalidBody(t *testing.T) {
r := bytes.NewReader([]byte(`foo`))
var body string
err := form.ParseAndValidate(r, "application/json", &body)
assert.NotNil(t, err)
}
func TestForm_ParseAndValidate_validation(t *testing.T) {
r := bytes.NewReader([]byte(`"FOO"`))
var body Lower
err := form.ParseAndValidate(r, "application/json", &body)
if assert.NotNil(t, err) {
assert.Equal(t, errLower, err)
}
}
func TestForm_ParseRequestAndValidate(t *testing.T) {
r := &http.Request{
Body: ioutil.NopCloser(bytes.NewReader([]byte(`"foo"`))),
Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
}
var body Lower
err := form.ParseRequestAndValidate(r, &body)
assert.NoError(t, err)
assert.Equal(t, Lower("foo"), body)
}