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