@@ -3,7 +3,8 @@ package requests
3
3
import utest ._
4
4
import ujson ._
5
5
6
- object RequestTests extends TestSuite {
6
+ object RequestTests extends HttpbinTestSuite {
7
+
7
8
val tests = Tests {
8
9
test(" matchingMethodWorks" ){
9
10
val requesters = Seq (
@@ -13,18 +14,18 @@ object RequestTests extends TestSuite{
13
14
requests.put
14
15
)
15
16
16
- for (protocol <- Seq (" http" , " https" )){
17
+ for (baseUrl <- Seq (s " http:// $localHttpbin " , " https://httpbin.org " )){
17
18
for (r <- requesters){
18
19
for (r2 <- requesters){
19
- val res = r(s " $protocol ://httpbin.org / ${r2.verb.toLowerCase()}" , check = false )
20
+ val res = r(s " $baseUrl / ${r2.verb.toLowerCase()}" , check = false )
20
21
if (r.verb == r2.verb) assert(res.statusCode == 200 )
21
22
else assert(res.statusCode == 405 )
22
23
23
24
if (r.verb == r2.verb){
24
- val res = r(s " $protocol ://httpbin.org / ${r2.verb.toLowerCase()}" )
25
+ val res = r(s " $baseUrl / ${r2.verb.toLowerCase()}" )
25
26
assert(res.statusCode == 200 )
26
27
}else intercept[RequestFailedException ]{
27
- r(s " $protocol ://httpbin.org / ${r2.verb.toLowerCase()}" )
28
+ r(s " $baseUrl / ${r2.verb.toLowerCase()}" )
28
29
}
29
30
}
30
31
}
@@ -34,26 +35,26 @@ object RequestTests extends TestSuite{
34
35
test(" params" ){
35
36
test(" get" ){
36
37
// All in URL
37
- val res1 = requests.get(" https ://httpbin.org /get?hello=world&foo=baz" ).text()
38
+ val res1 = requests.get(s " http ://$localHttpbin /get?hello=world&foo=baz " ).text()
38
39
assert(read(res1).obj(" args" ) == Obj (" foo" -> " baz" , " hello" -> " world" ))
39
40
40
41
// All in params
41
42
val res2 = requests.get(
42
- " https ://httpbin.org /get" ,
43
+ s " http ://$localHttpbin /get " ,
43
44
params = Map (" hello" -> " world" , " foo" -> " baz" )
44
45
)
45
46
assert(read(res2).obj(" args" ) == Obj (" foo" -> " baz" , " hello" -> " world" ))
46
47
47
48
// Mixed URL and params
48
49
val res3 = requests.get(
49
- " https ://httpbin.org /get?hello=world" ,
50
+ s " http ://$localHttpbin /get?hello=world " ,
50
51
params = Map (" foo" -> " baz" )
51
52
).text()
52
53
assert(read(res3).obj(" args" ) == Obj (" foo" -> " baz" , " hello" -> " world" ))
53
54
54
55
// Needs escaping
55
56
val res4 = requests.get(
56
- " https ://httpbin.org /get?hello=world" ,
57
+ s " http ://$localHttpbin /get?hello=world " ,
57
58
params = Map (" ++-- lol" -> " !@#$%" )
58
59
)
59
60
assert(read(res4).obj(" args" ) == Obj (" ++-- lol" -> " !@#$%" , " hello" -> " world" ))
@@ -63,7 +64,7 @@ object RequestTests extends TestSuite{
63
64
test(" multipart" ){
64
65
for (chunkedUpload <- Seq (true , false )) {
65
66
val response = requests.post(
66
- " http://httpbin.org /post" ,
67
+ s " http:// $localHttpbin /post " ,
67
68
data = MultiPart (
68
69
MultiItem (" file1" , " Hello!" .getBytes, " foo.txt" ),
69
70
MultiItem (" file2" , " Goodbye!" )
@@ -79,76 +80,77 @@ object RequestTests extends TestSuite{
79
80
test(" cookies" ){
80
81
test(" session" ){
81
82
val s = requests.Session (cookieValues = Map (" hello" -> " world" ))
82
- val res1 = s.get(" https ://httpbin.org /cookies" ).text().trim
83
+ val res1 = s.get(s " http ://$localHttpbin /cookies " ).text().trim
83
84
assert(read(res1) == Obj (" cookies" -> Obj (" hello" -> " world" )))
84
- s.get(" https ://httpbin.org /cookies/set?freeform=test" )
85
- val res2 = s.get(" https ://httpbin.org /cookies" ).text().trim
85
+ s.get(s " http ://$localHttpbin /cookies/set?freeform=test " )
86
+ val res2 = s.get(s " http ://$localHttpbin /cookies " ).text().trim
86
87
assert(read(res2) == Obj (" cookies" -> Obj (" freeform" -> " test" , " hello" -> " world" )))
87
88
}
88
89
test(" raw" ){
89
- val res1 = requests.get(" https ://httpbin.org /cookies" ).text().trim
90
+ val res1 = requests.get(s " http ://$localHttpbin /cookies " ).text().trim
90
91
assert(read(res1) == Obj (" cookies" -> Obj ()))
91
- requests.get(" https ://httpbin.org /cookies/set?freeform=test" )
92
- val res2 = requests.get(" https ://httpbin.org /cookies" ).text().trim
92
+ requests.get(s " http ://$localHttpbin /cookies/set?freeform=test " )
93
+ val res2 = requests.get(s " http ://$localHttpbin /cookies " ).text().trim
93
94
assert(read(res2) == Obj (" cookies" -> Obj ()))
94
95
}
95
96
test(" space" ){
96
97
val s = requests.Session (cookieValues = Map (" hello" -> " hello, world" ))
97
- val res1 = s.get(" https ://httpbin.org /cookies" ).text().trim
98
+ val res1 = s.get(s " http ://$localHttpbin /cookies " ).text().trim
98
99
assert(read(res1) == Obj (" cookies" -> Obj (" hello" -> " hello, world" )))
99
- s.get(" https ://httpbin.org /cookies/set?freeform=test+test" )
100
- val res2 = s.get(" https ://httpbin.org /cookies" ).text().trim
100
+ s.get(s " http ://$localHttpbin /cookies/set?freeform=test+test " )
101
+ val res2 = s.get(s " http ://$localHttpbin /cookies " ).text().trim
101
102
assert(read(res2) == Obj (" cookies" -> Obj (" freeform" -> " test test" , " hello" -> " hello, world" )))
102
103
}
103
104
}
104
105
105
106
test(" redirects" ){
106
107
test(" max" ){
107
- val res1 = requests.get(" https ://httpbin.org /absolute-redirect/4" )
108
+ val res1 = requests.get(s " http ://$localHttpbin /absolute-redirect/4 " )
108
109
assert(res1.statusCode == 200 )
109
- val res2 = requests.get(" https ://httpbin.org /absolute-redirect/5" )
110
+ val res2 = requests.get(s " http ://$localHttpbin /absolute-redirect/5 " )
110
111
assert(res2.statusCode == 200 )
111
- val res3 = requests.get(" https ://httpbin.org /absolute-redirect/6" , check = false )
112
+ val res3 = requests.get(s " http ://$localHttpbin /absolute-redirect/6 " , check = false )
112
113
assert(res3.statusCode == 302 )
113
- val res4 = requests.get(" https ://httpbin.org /absolute-redirect/6" , maxRedirects = 10 )
114
+ val res4 = requests.get(s " http ://$localHttpbin /absolute-redirect/6 " , maxRedirects = 10 )
114
115
assert(res4.statusCode == 200 )
115
116
}
116
117
test(" maxRelative" ){
117
- val res1 = requests.get(" https ://httpbin.org /relative-redirect/4" )
118
+ val res1 = requests.get(s " http ://$localHttpbin /relative-redirect/4 " )
118
119
assert(res1.statusCode == 200 )
119
- val res2 = requests.get(" https ://httpbin.org /relative-redirect/5" )
120
+ val res2 = requests.get(s " http ://$localHttpbin /relative-redirect/5 " )
120
121
assert(res2.statusCode == 200 )
121
- val res3 = requests.get(" https ://httpbin.org /relative-redirect/6" , check = false )
122
+ val res3 = requests.get(s " http ://$localHttpbin /relative-redirect/6 " , check = false )
122
123
assert(res3.statusCode == 302 )
123
- val res4 = requests.get(" https ://httpbin.org /relative-redirect/6" , maxRedirects = 10 )
124
+ val res4 = requests.get(s " http ://$localHttpbin /relative-redirect/6 " , maxRedirects = 10 )
124
125
assert(res4.statusCode == 200 )
125
126
}
126
127
}
127
128
128
129
test(" test_reproduction" ){
129
- requests.get(" http://httpbin.org /status/304" ).text()
130
+ requests.get(s " http:// $localHttpbin /status/304 " ).text()
130
131
131
132
}
132
133
test(" streaming" ){
133
- val res1 = requests.get(" http://httpbin.org /stream/5" ).text()
134
+ val res1 = requests.get(s " http:// $localHttpbin /stream/5 " ).text()
134
135
assert(res1.linesIterator.length == 5 )
135
- val res2 = requests.get(" http://httpbin.org /stream/52" ).text()
136
+ val res2 = requests.get(s " http:// $localHttpbin /stream/52 " ).text()
136
137
assert(res2.linesIterator.length == 52 )
137
138
}
138
139
139
140
test(" timeouts" ){
140
141
test(" read" ){
141
142
intercept[TimeoutException ] {
142
- requests.get(" https ://httpbin.org /delay/1" , readTimeout = 10 )
143
+ requests.get(s " http ://$localHttpbin /delay/1 " , readTimeout = 10 )
143
144
}
144
- requests.get(" https ://httpbin.org /delay/1" , readTimeout = 2000 )
145
+ requests.get(s " http ://$localHttpbin /delay/1 " , readTimeout = 2000 )
145
146
intercept[TimeoutException ] {
146
- requests.get(" https ://httpbin.org /delay/3" , readTimeout = 2000 )
147
+ requests.get(s " http ://$localHttpbin /delay/3 " , readTimeout = 2000 )
147
148
}
148
149
}
149
150
test(" connect" ){
150
151
intercept[TimeoutException ] {
151
- requests.get(" https://httpbin.org/delay/1" , connectTimeout = 1 )
152
+ // use remote httpbin.org so it needs more time to connect
153
+ requests.get(s " https://httpbin.org/delay/1 " , connectTimeout = 1 )
152
154
}
153
155
}
154
156
}
@@ -167,38 +169,38 @@ object RequestTests extends TestSuite{
167
169
}
168
170
169
171
test(" decompress" ){
170
- val res1 = requests.get(" https ://httpbin.org /gzip" )
171
- assert(read(res1.text()).obj(" headers" ).obj(" Host" ).str == " httpbin.org " )
172
+ val res1 = requests.get(s " http ://$localHttpbin /gzip " )
173
+ assert(read(res1.text()).obj(" headers" ).obj(" Host" ).str == localHttpbin )
172
174
173
- val res2 = requests.get(" https ://httpbin.org /deflate" )
174
- assert(read(res2).obj(" headers" ).obj(" Host" ).str == " httpbin.org " )
175
+ val res2 = requests.get(s " http ://$localHttpbin /deflate " )
176
+ assert(read(res2).obj(" headers" ).obj(" Host" ).str == localHttpbin )
175
177
176
- val res3 = requests.get(" https ://httpbin.org /gzip" , autoDecompress = false )
178
+ val res3 = requests.get(s " http ://$localHttpbin /gzip " , autoDecompress = false )
177
179
assert(res3.bytes.length < res1.bytes.length)
178
180
179
- val res4 = requests.get(" https ://httpbin.org /deflate" , autoDecompress = false )
181
+ val res4 = requests.get(s " http ://$localHttpbin /deflate " , autoDecompress = false )
180
182
assert(res4.bytes.length < res2.bytes.length)
181
183
182
184
(res1.bytes.length, res2.bytes.length, res3.bytes.length, res4.bytes.length)
183
185
}
184
186
185
187
test(" compression" ){
186
188
val res1 = requests.post(
187
- " https ://httpbin.org /post" ,
189
+ s " http ://$localHttpbin /post " ,
188
190
compress = requests.Compress .None ,
189
191
data = new RequestBlob .ByteSourceRequestBlob (" Hello World" )
190
192
)
191
193
assert(res1.text().contains(""" "Hello World"""" ))
192
194
193
195
val res2 = requests.post(
194
- " https ://httpbin.org /post" ,
196
+ s " http ://$localHttpbin /post " ,
195
197
compress = requests.Compress .Gzip ,
196
198
data = new RequestBlob .ByteSourceRequestBlob (" I am cow" )
197
199
)
198
200
assert(read(new String (res2.bytes))(" data" ).toString.contains(" data:application/octet-stream;base64,H4sIAAAAAA" ))
199
201
200
202
val res3 = requests.post(
201
- " https ://httpbin.org /post" ,
203
+ s " http ://$localHttpbin /post " ,
202
204
compress = requests.Compress .Deflate ,
203
205
data = new RequestBlob .ByteSourceRequestBlob (" Hear me moo" )
204
206
)
@@ -208,7 +210,7 @@ object RequestTests extends TestSuite{
208
210
209
211
test(" headers" ){
210
212
test(" default" ){
211
- val res = requests.get(" https ://httpbin.org /headers" ).text()
213
+ val res = requests.get(s " http ://$localHttpbin /headers " ).text()
212
214
val hs = read(res)(" headers" ).obj
213
215
assert(hs(" User-Agent" ).str == " requests-scala" )
214
216
assert(hs(" Accept-Encoding" ).str == " gzip, deflate" )
@@ -306,7 +308,7 @@ object RequestTests extends TestSuite{
306
308
// to the server. This preserves the 0.8.x behavior, and can always be overriden
307
309
// by passing a comma-separated list of headers instead
308
310
test(" duplicateHeaders" ){
309
- val res = requests.get(" https ://httpbin.org /get" , headers = Seq (" x-y" -> " a" , " x-y" -> " b" ))
311
+ val res = requests.get(s " http ://$localHttpbin /get " , headers = Seq (" x-y" -> " a" , " x-y" -> " b" ))
310
312
assert(ujson.read(res)(" headers" )(" X-Y" ) == Str (" b" )) // make sure it's not "a,b"
311
313
}
312
314
}
0 commit comments