-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.go
74 lines (59 loc) · 1.94 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
72
73
74
package auth
import (
"net/http"
"net/url"
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestParseRequest(t *testing.T) {
if !testing.Verbose() {
t.Parallel()
}
Convey("Given an request with valid POST parameters", t, func() {
data := url.Values{
"username": []string{"test_user"},
"password": []string{"test_pass"},
"token": []string{"test_token"},
}
req, err := http.NewRequest("POST", "/auth", strings.NewReader(data.Encode()))
So(err, ShouldBeNil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
Convey("When the request is parsed", func() {
parsed, err := ParseRequest(req)
So(err, ShouldBeNil)
Convey("Then the request fields should be returned", func() {
So(parsed.Username, ShouldEqual, "test_user")
So(parsed.Username, ShouldEqual, "test_user")
So(parsed.Token, ShouldEqual, "test_token")
})
})
})
Convey("Given an request with valid JSON data", t, func() {
data := `{"username": "test_user", "password": "test_pass", "token": "test_token"}`
req, err := http.NewRequest("POST", "/auth", strings.NewReader(data))
So(err, ShouldBeNil)
req.Header.Set("Content-Type", "application/json")
Convey("When the request is parsed", func() {
parsed, err := ParseRequest(req)
So(err, ShouldBeNil)
Convey("hen the request fields should be returned", func() {
So(parsed.Username, ShouldEqual, "test_user")
So(parsed.Password, ShouldEqual, "test_pass")
So(parsed.Token, ShouldEqual, "test_token")
})
})
})
Convey("Given an request with invalid JSON data", t, func() {
req, err := http.NewRequest("POST", "/auth", strings.NewReader(""))
So(err, ShouldBeNil)
req.Header.Set("Content-Type", "application/json")
Convey("When the request is parsed", func() {
parsed, err := ParseRequest(req)
Convey("Then a parse error should be returned", func() {
So(err, ShouldNotBeNil)
So(parsed, ShouldBeNil)
})
})
})
}