@@ -4,12 +4,17 @@ import (
44 "net/http"
55 "net/http/httptest"
66 "testing"
7+ "time"
78
89 "github.com/franela/goblin"
10+ . "github.com/onsi/gomega"
11+ "github.com/rs/zerolog"
912)
1013
1114func TestErisedInfoRoute (t * testing.T ) {
15+ zerolog .SetGlobalLevel (zerolog .Disabled )
1216 g := goblin .Goblin (t )
17+ RegisterFailHandler (func (m string , _ ... int ) { g .Fail (m ) })
1318 exp := `{"Host":"localhost:8080","Method":"GET","Protocol":"HTTP/1.1","Request URI":"http://localhost:8080/erised/info"}`
1419 svr := server {}
1520 req := httptest .NewRequest (http .MethodGet , "http://localhost:8080/erised/info" , nil )
@@ -18,22 +23,23 @@ func TestErisedInfoRoute(t *testing.T) {
1823
1924 g .Describe ("Test erised/info" , func () {
2025 g .It ("Should return StatusOK" , func () {
21- g . Assert (res .Code ).Equal (http .StatusOK )
26+ Ω (res .Code ).Should ( Equal (http .StatusOK ) )
2227 })
2328
2429 g .It ("Should match expected body" , func () {
25- g . Assert (res .Body .String ()).Equal (exp )
30+ Ω (res .Body .String ()).Should ( Equal (exp ) )
2631 })
2732
2833 g .It ("Should match Content-Type header" , func () {
29- g . Assert (res .Header ().Get ("Content-Type" )).Equal ("application/json" )
34+ Ω (res .Header ().Get ("Content-Type" )).Should ( Equal ("application/json" ) )
3035 })
31-
3236 })
3337}
3438
3539func TestErisedIPRoute (t * testing.T ) {
40+ zerolog .SetGlobalLevel (zerolog .Disabled )
3641 g := goblin .Goblin (t )
42+ RegisterFailHandler (func (m string , _ ... int ) { g .Fail (m ) })
3743 exp := `{"Client IP":"192.0.2.1:1234"}`
3844 svr := server {}
3945 req := httptest .NewRequest (http .MethodGet , "http://localhost:8080/erised/ip" , nil )
@@ -42,21 +48,23 @@ func TestErisedIPRoute(t *testing.T) {
4248
4349 g .Describe ("Test erised/ip" , func () {
4450 g .It ("Should return StatusOK" , func () {
45- g . Assert (res .Code ).Equal (http .StatusOK )
51+ Ω (res .Code ).Should ( Equal (http .StatusOK ) )
4652 })
4753
4854 g .It ("Should match expected body" , func () {
49- g . Assert (res .Body .String ()).Equal (exp )
55+ Ω (res .Body .String ()).Should ( Equal (exp ) )
5056 })
5157
5258 g .It ("Should match Content-Type header" , func () {
53- g . Assert (res .Header ().Get ("Content-Type" )).Equal ("application/json" )
59+ Ω (res .Header ().Get ("Content-Type" )).Should ( Equal ("application/json" ) )
5460 })
5561 })
5662}
5763
5864func TestErisedHeadersRoute (t * testing.T ) {
65+ zerolog .SetGlobalLevel (zerolog .Disabled )
5966 g := goblin .Goblin (t )
67+ RegisterFailHandler (func (m string , _ ... int ) { g .Fail (m ) })
6068 exp := `{"Host":"localhost:8080"}`
6169 svr := server {}
6270 req := httptest .NewRequest (http .MethodGet , "http://localhost:8080/erised/headers" , nil )
@@ -65,21 +73,23 @@ func TestErisedHeadersRoute(t *testing.T) {
6573
6674 g .Describe ("Test erised/headers" , func () {
6775 g .It ("Should return StatusOK" , func () {
68- g . Assert (res .Code ).Equal (http .StatusOK )
76+ Ω (res .Code ).Should ( Equal (http .StatusOK ) )
6977 })
7078
7179 g .It ("Should match expected body" , func () {
72- g . Assert (res .Body .String ()).Equal (exp )
80+ Ω (res .Body .String ()).Should ( Equal (exp ) )
7381 })
7482
7583 g .It ("Should match Content-Type header" , func () {
76- g . Assert (res .Header ().Get ("Content-Type" )).Equal ("application/json" )
84+ Ω (res .Header ().Get ("Content-Type" )).Should ( Equal ("application/json" ) )
7785 })
7886 })
7987}
8088
8189func TestErisedLandingRoute (t * testing.T ) {
90+ zerolog .SetGlobalLevel (zerolog .Disabled )
8291 g := goblin .Goblin (t )
92+ RegisterFailHandler (func (m string , _ ... int ) { g .Fail (m ) })
8393 svr := server {}
8494
8595 g .Describe ("Test /" , func () {
@@ -88,32 +98,33 @@ func TestErisedLandingRoute(t *testing.T) {
8898 req := httptest .NewRequest (http .MethodGet , "http://localhost:8080/" , nil )
8999 svr .handleLanding ().ServeHTTP (res , req )
90100
91- g . Assert (res . Code ). Equal ( http .StatusOK )
92- g . Assert (res .Body .String ()).Equal ( "" )
101+ Ω (res ). Should ( HaveHTTPStatus ( http .StatusOK ) )
102+ Ω (res .Body .String ()).Should ( BeEmpty () )
93103 })
94104
95- g .It ("Should return TemporaryRedirect and Location" , func () {
105+ g .It ("Should return TemporaryRedirect and Location url " , func () {
96106 res := httptest .NewRecorder ()
97107 req := httptest .NewRequest (http .MethodGet , "http://localhost:8080/" , nil )
98108 req .Header .Set ("X-Erised-Status-Code" , "TemporaryRedirect" )
99109 req .Header .Set ("X-Erised-Location" , "https://www.example.com" )
100110 svr .handleLanding ().ServeHTTP (res , req )
101111
102- g . Assert (res . Code ). Equal ( http .StatusTemporaryRedirect )
103- g . Assert (res .Header ().Get ("Location" )).Equal ("https://www.example.com" )
112+ Ω (res ). Should ( HaveHTTPStatus ( http .StatusTemporaryRedirect ) )
113+ Ω (res .Header ().Get ("Location" )).Should ( Equal ("https://www.example.com" ) )
104114 })
105115
106- g .It ("Should return JSON body" , func () {
116+ g .It ("Should return json body" , func () {
107117 exp := `{"hello":"world"}`
108118 res := httptest .NewRecorder ()
109119 req := httptest .NewRequest (http .MethodGet , "http://localhost:8080/" , nil )
110120 req .Header .Set ("X-Erised-Content-Type" , "json" )
111121 req .Header .Set ("X-Erised-Data" , exp )
112122 svr .handleLanding ().ServeHTTP (res , req )
113123
114- g .Assert (res .Code ).Equal (http .StatusOK )
115- g .Assert (res .Header ().Get ("Content-Type" )).Equal ("application/json" )
116- g .Assert (res .Body .String ()).Equal (exp )
124+ Ω (res ).Should (HaveHTTPStatus (http .StatusOK ))
125+ Ω (res .Header ().Get ("Content-Type" )).Should (Equal ("application/json" ))
126+ Ω (res .Header ().Get ("Content-Encoding" )).Should (Equal ("identity" ))
127+ Ω (res .Body .String ()).Should (Equal (exp ))
117128 })
118129
119130 g .It ("Should return text body" , func () {
@@ -124,9 +135,40 @@ func TestErisedLandingRoute(t *testing.T) {
124135 req .Header .Set ("X-Erised-Data" , exp )
125136 svr .handleLanding ().ServeHTTP (res , req )
126137
127- g .Assert (res .Code ).Equal (http .StatusOK )
128- g .Assert (res .Header ().Get ("Content-Type" )).Equal ("text/plain" )
129- g .Assert (res .Body .String ()).Equal (exp )
138+ Ω (res ).Should (HaveHTTPStatus (http .StatusOK ))
139+ Ω (res .Header ().Get ("Content-Type" )).Should (Equal ("text/plain" ))
140+ Ω (res .Header ().Get ("Content-Encoding" )).Should (Equal ("identity" ))
141+ Ω (res .Body .String ()).Should (Equal (exp ))
142+ })
143+
144+ g .It ("Should return xml body" , func () {
145+ exp := "<hello>world</hello>"
146+ res := httptest .NewRecorder ()
147+ req := httptest .NewRequest (http .MethodGet , "http://localhost:8080/" , nil )
148+ req .Header .Set ("X-Erised-Content-Type" , "xml" )
149+ req .Header .Set ("X-Erised-Data" , exp )
150+ svr .handleLanding ().ServeHTTP (res , req )
151+
152+ Ω (res ).Should (HaveHTTPStatus (http .StatusOK ))
153+ Ω (res .Header ().Get ("Content-Type" )).Should (Equal ("application/xml" ))
154+ Ω (res .Header ().Get ("Content-Encoding" )).Should (Equal ("identity" ))
155+ Ω (res .Body .String ()).Should (Equal (exp ))
156+ })
157+
158+ g .It ("Should return gzip body" , func () {
159+ exp := "Lorem ipsum"
160+ gz := "\x1f \x8b \b \x00 \x00 \x00 \x00 \x00 \x00 \xff \xf2 \xc9 /J\xcd U\xc8 ,(.\xcd \x05 \x04 \x00 \x00 \xff \xff Y\xfb K\xf4 \v \x00 \x00 \x00 "
161+
162+ res := httptest .NewRecorder ()
163+ req := httptest .NewRequest (http .MethodGet , "http://localhost:8080/" , nil )
164+ req .Header .Set ("X-Erised-Content-Type" , "gzip" )
165+ req .Header .Set ("X-Erised-Data" , exp )
166+ svr .handleLanding ().ServeHTTP (res , req )
167+
168+ Ω (res ).Should (HaveHTTPStatus (http .StatusOK ))
169+ Ω (res .Header ().Get ("Content-Type" )).Should (Equal ("application/octet-stream" ))
170+ Ω (res .Header ().Get ("Content-Encoding" )).Should (Equal ("gzip" ))
171+ Ω (res .Body .String ()).Should (Equal (gz ))
130172 })
131173
132174 g .It ("Should return headers" , func () {
@@ -136,19 +178,23 @@ func TestErisedLandingRoute(t *testing.T) {
136178 req .Header .Set ("X-Erised-Headers" , exp )
137179 svr .handleLanding ().ServeHTTP (res , req )
138180
139- g . Assert (res . Code ). Equal ( http .StatusOK )
140- g . Assert (res .Header ().Get ("X-Headers-One" )).Equal ("I'm header one" )
141- g . Assert (res .Header ().Get ("X-Headers-Two" )).Equal ("I'm header two" )
181+ Ω (res ). Should ( HaveHTTPStatus ( http .StatusOK ) )
182+ Ω (res .Header ().Get ("X-Headers-One" )).Should ( Equal ("I'm header one" ) )
183+ Ω (res .Header ().Get ("X-Headers-Two" )).Should ( Equal ("I'm header two" ) )
142184 })
143185
144186 g .It ("Should wait 2000ms" , func () {
145187 res := httptest .NewRecorder ()
146188 req := httptest .NewRequest (http .MethodGet , "http://localhost:8080/" , nil )
147189 req .Header .Set ("X-Erised-Response-Delay" , "2000" )
190+
191+ st := time .Now ()
148192 svr .handleLanding ().ServeHTTP (res , req )
193+ el := time .Since (st )
149194
150- g .Assert (res .Code ).Equal (http .StatusOK )
151- g .Assert (res .Body .String ()).Equal ("" )
195+ Ω (res ).Should (HaveHTTPStatus (http .StatusOK ))
196+ Ω (el ).Should (BeNumerically ("~" , time .Millisecond * 2000 , time .Millisecond * 10 ))
197+ Ω (res .Body .String ()).Should (BeEmpty ())
152198 })
153199
154200 g .It ("Should not fail" , func () {
@@ -159,18 +205,16 @@ func TestErisedLandingRoute(t *testing.T) {
159205 req .Header .Set ("X-Erised-Data" , exp )
160206 req .Header .Set ("X-Erised-Headers" , exp )
161207 req .Header .Set ("X-Erised-Location" , "https://www.example.com" )
162- req .Header .Set ("X-Erised-Response-Delay" , "1 " )
208+ req .Header .Set ("X-Erised-Response-Delay" , "0 " )
163209 req .Header .Set ("X-Erised-Status-Code" , "MovedPermanently" )
164210 svr .handleLanding ().ServeHTTP (res , req )
165211
166- g . Assert (res . Code ). Equal ( http .StatusMovedPermanently )
167- g . Assert (res .Header ().Get ("Location" )).Equal ("https://www.example.com" )
168- g . Assert (res .Header ().Get ("Content-Type" )).Equal ("application/json" )
169- g . Assert (res .Header ().Get ("Content-Encoding" )).Equal ("identity" )
170- g . Assert (res .Header ().Get ("hello" )).Equal ("world" )
171- g . Assert (res .Body .String ()).Equal (exp )
212+ Ω (res ). Should ( HaveHTTPStatus ( http .StatusMovedPermanently ) )
213+ Ω (res .Header ().Get ("Location" )).Should ( Equal ("https://www.example.com" ) )
214+ Ω (res .Header ().Get ("Content-Type" )).Should ( Equal ("application/json" ) )
215+ Ω (res .Header ().Get ("Content-Encoding" )).Should ( Equal ("identity" ) )
216+ Ω (res .Header ().Get ("hello" )).Should ( Equal ("world" ) )
217+ Ω (res .Body .String ()).Should ( Equal (exp ) )
172218 })
173-
174219 })
175220}
176-
0 commit comments