-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrequest_test.go
More file actions
150 lines (122 loc) · 4.09 KB
/
request_test.go
File metadata and controls
150 lines (122 loc) · 4.09 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package hermes
import (
"context"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/valyala/fasthttp"
)
func newRequest() *BaseRequest {
return &BaseRequest{
ctx: context.Background(),
r: &fasthttp.RequestCtx{},
}
}
type contextKey string
var contextKeyTestID contextKey = "test:context:id"
var _ = Describe("Hermes", func() {
Describe("Request", func() {
It("should have a context", func() {
req := newRequest()
req.WithContext(context.WithValue(req.Context(), contextKeyTestID, "123"))
Expect(req.Context().Value(contextKeyTestID)).To(Equal("123"))
})
It("should want JSON", func() {
req := newRequest()
req.Raw().Request.Header.Set("Accept", "application/json, text/html")
Expect(req.WantsJSON()).To(BeTrue())
})
It("should not want JSON", func() {
req := newRequest()
req.Raw().Request.Header.Set("Accept", "text/html, application/xhtml+xml, application/xml")
Expect(req.WantsJSON()).To(BeFalse())
})
It("should not want JSON if not first", func() {
req := newRequest()
req.Raw().Request.Header.Set("Accept", "text/html,application/json")
Expect(req.WantsJSON()).To(BeFalse())
})
It("should accept the Content-type as JSON", func() {
req := newRequest()
req.Raw().Request.Header.SetContentType("application/json")
req.Raw().Request.AppendBodyString(`{"foo":"bar"}`)
Expect(req.IsJSON()).To(BeTrue())
})
It("should accept the Content-type with charset as JSON", func() {
req := newRequest()
req.Raw().Request.Header.SetContentType("application/json; charset=utf-8")
req.Raw().Request.AppendBodyString(`{"foo":"bar"}`)
Expect(req.IsJSON()).To(BeTrue())
})
It("should not accept the Content-type as JSON", func() {
req := newRequest()
Expect(req.IsJSON()).To(BeFalse())
})
It("should return the JSON structure in the body of the requisition", func() {
req := newRequest()
req.Raw().Request.Header.SetContentType("application/json; charset=utf-8")
req.Raw().Request.AppendBodyString(`{"foo":"bar"}`)
var data struct {
Foo string `json:"foo"`
}
Expect(req.Data(&data)).To(BeNil())
Expect(data.Foo).To(Equal("bar"))
})
It("should get header", func() {
req := newRequest()
req.Raw().Request.Header.Set("X-Awesome", "Fully")
Expect(string(req.Header("X-Awesome"))).To(Equal("Fully"))
})
It("should get query value", func() {
req := newRequest()
req.Raw().QueryArgs().Set("is_test", "true")
Expect(string(req.Query("is_test"))).To(Equal("true"))
})
It("should get multiple query values", func() {
req := newRequest()
req.Raw().QueryArgs().Add("id", "1")
req.Raw().QueryArgs().Add("id", "2")
req.Raw().QueryArgs().Add("id", "3")
values := req.QueryMulti("id")
found := make([]string, len(values))
for i, v := range values {
found[i] = string(v)
}
Expect(found).To(ConsistOf("1", "2", "3"))
})
It("should get post value", func() {
req := newRequest()
req.Raw().PostArgs().Set("is_test", "true")
Expect(string(req.Post("is_test"))).To(Equal("true"))
})
It("should get multiple post values", func() {
req := newRequest()
req.Raw().PostArgs().Add("id", "1")
req.Raw().PostArgs().Add("id", "2")
req.Raw().PostArgs().Add("id", "3")
values := req.PostMulti("id")
found := make([]string, len(values))
for i, v := range values {
found[i] = string(v)
}
Expect(found).To(ConsistOf("1", "2", "3"))
})
It("should get cookie", func() {
req := newRequest()
req.Raw().Request.Header.SetCookie("session", "6194438f-f2f5-48b5-867b-1767b0f7d408")
Expect(string(req.Cookie("session"))).To(Equal("6194438f-f2f5-48b5-867b-1767b0f7d408"))
})
It("should get host", func() {
req := newRequest()
req.Raw().Request.SetHost("www.gijoe.io")
Expect(string(req.Host())).To(Equal("www.gijoe.io"))
})
It("should get URI", func() {
req := newRequest()
req.Raw().Request.SetRequestURI("http://localhost:5000/v1/api")
uri := req.URI()
Expect(string(uri.Host())).To(Equal("localhost:5000"))
Expect(string(uri.Path())).To(Equal("/v1/api"))
Expect(string(uri.Scheme())).To(Equal("http"))
})
})
})