-
Notifications
You must be signed in to change notification settings - Fork 43
/
request_test.go
71 lines (55 loc) · 1.39 KB
/
request_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
61
62
63
64
65
66
67
68
69
70
71
package neo
import (
"bytes"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"testing"
)
func makeTestCookie(key, value string) *http.Cookie {
return &http.Cookie{
Name: key,
Value: value,
}
}
func makeTestHttpRequest(body io.Reader) *http.Request {
req, err := http.NewRequest(GET, "/some/path", body)
req.AddCookie(makeTestCookie("key1", "value1"))
if err != nil {
return nil
}
return req
}
func TestMakeRequest(t *testing.T) {
req := makeRequest(makeTestHttpRequest(nil))
assert.NotNil(t, req)
cookie, err := req.Cookie("key1")
assert.NotNil(t, cookie)
assert.Nil(t, err)
assert.Equal(t, GET, req.Method)
assert.Equal(t, "/some/path", req.URL.Path)
cookie, err = req.Cookie("keyunknown")
assert.Nil(t, cookie)
assert.NotNil(t, err)
}
type testStruct struct {
Name string
Age int
}
func TestJsonBody(t *testing.T) {
// invalid
httpRequest := makeTestHttpRequest(bytes.NewBuffer([]byte("{'Name': \"SomeName\", \"Age\": 20}")))
request := makeRequest(httpRequest)
person := &testStruct{}
err := request.JsonBody(person)
assert.NotNil(t, err)
// valid
httpRequest = makeTestHttpRequest(bytes.NewBuffer([]byte("{\"Name\": \"SomeName\", \"Age\": 20}")))
request = makeRequest(httpRequest)
person = &testStruct{}
err = request.JsonBody(person)
assert.Nil(t, err)
assert.NotNil(t, person)
assert.Equal(t, "SomeName", person.Name)
assert.Equal(t, 20, person.Age)
}