forked from playframework/playframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AhcWSSpec.scala
580 lines (505 loc) · 21.2 KB
/
AhcWSSpec.scala
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/
package play.api.libs.ws.ahc
import java.util
import java.nio.charset.StandardCharsets
import akka.stream.Materializer
import akka.stream.scaladsl.FileIO
import akka.stream.scaladsl.Source
import akka.util.ByteString
import akka.util.Timeout
import org.specs2.concurrent.ExecutionEnv
import org.specs2.matcher.FutureMatchers
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.oauth.ConsumerKey
import play.api.libs.oauth.OAuthCalculator
import play.api.libs.oauth.RequestToken
import play.api.libs.ws._
import play.api.mvc._
import play.api.test.DefaultAwaitTimeout
import play.api.test.FutureAwaits
import play.api.test.Helpers
import play.api.test.WithServer
import play.shaded.ahc.io.netty.handler.codec.http.DefaultHttpHeaders
import play.shaded.ahc.org.asynchttpclient.Realm.AuthScheme
import play.shaded.ahc.io.netty.handler.codec.http.cookie.{ Cookie => NettyCookie }
import play.shaded.ahc.io.netty.handler.codec.http.cookie.{ DefaultCookie => NettyDefaultCookie }
import play.shaded.ahc.org.asynchttpclient.Param
import play.shaded.ahc.org.asynchttpclient.{ Request => AHCRequest }
import play.shaded.ahc.org.asynchttpclient.{ Response => AHCResponse }
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.language.implicitConversions
class AhcWSSpec(implicit ee: ExecutionEnv)
extends Specification
with Mockito
with FutureMatchers
with FutureAwaits
with DefaultAwaitTimeout {
sequential
"Ahc WSClient" should {
"support several query string values for a parameter" in {
val r: AhcWSRequest = makeAhcRequest("http://playframework.com/")
.withQueryStringParameters("foo" -> "foo1", "foo" -> "foo2")
.asInstanceOf[AhcWSRequest]
val req: AHCRequest = r.underlying.buildRequest()
import scala.jdk.CollectionConverters._
val paramsList: scala.collection.Seq[Param] = req.getQueryParams.asScala
paramsList.exists(p => (p.getName == "foo") && (p.getValue == "foo1")) must beTrue
paramsList.exists(p => (p.getName == "foo") && (p.getValue == "foo2")) must beTrue
paramsList.count(p => p.getName == "foo") must beEqualTo(2)
}
"support http headers" in {
import scala.jdk.CollectionConverters._
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.addHttpHeaders("key" -> "value1", "key" -> "value2")
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
req.getHeaders.getAll("key").asScala must containTheSameElementsAs(Seq("value1", "value2"))
}
}
def makeAhcRequest(url: String): AhcWSRequest = {
implicit val materializer = mock[Materializer]
val client = StandaloneAhcWSClient(AhcWSClientConfig())
val standalone = StandaloneAhcWSRequest(client, url)
AhcWSRequest(standalone)
}
"not make Content-Type header if there is Content-Type in headers already" in {
import scala.jdk.CollectionConverters._
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.addHttpHeaders("content-type" -> "fake/contenttype; charset=utf-8")
.withBody(<aaa>value1</aaa>)
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
req.getHeaders.getAll("Content-Type").asScala must_== Seq("fake/contenttype; charset=utf-8")
}
"Have form params on POST of content type application/x-www-form-urlencoded" in {
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.withBody(Map("param1" -> Seq("value1")))
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
new String(req.getByteData, StandardCharsets.UTF_8) must_== ("param1=value1")
}
"Have form body on POST of content type text/plain" in {
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.addHttpHeaders("Content-Type" -> "text/plain")
.withBody("HELLO WORLD")
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
new String(req.getByteData, StandardCharsets.UTF_8) must be_==("HELLO WORLD")
val headers = req.getHeaders
headers.get("Content-Length") must beNull
}
"Have form body on POST of content type application/x-www-form-urlencoded explicitly set" in {
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.addHttpHeaders("Content-Type" -> "application/x-www-form-urlencoded") // set content type by hand
.withBody("HELLO WORLD") // and body is set to string (see #5221)
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
new String(req.getByteData, StandardCharsets.UTF_8) must be_==("HELLO WORLD") // should result in byte data.
}
"support a custom signature calculator" in {
var called = false
val calc = new play.shaded.ahc.org.asynchttpclient.SignatureCalculator with WSSignatureCalculator {
override def calculateAndAddSignature(
request: play.shaded.ahc.org.asynchttpclient.Request,
requestBuilder: play.shaded.ahc.org.asynchttpclient.RequestBuilderBase[_]
): Unit = {
called = true
}
}
val req = makeAhcRequest("http://playframework.com/")
.sign(calc)
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
called must beTrue
}
"Have form params on POST of content type application/x-www-form-urlencoded when signed" in {
import scala.jdk.CollectionConverters._
val consumerKey = ConsumerKey("key", "secret")
val requestToken = RequestToken("token", "secret")
val calc = OAuthCalculator(consumerKey, requestToken)
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.withBody(Map("param1" -> Seq("value1")))
.sign(calc)
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
// Note we use getFormParams instead of getByteData here.
req.getFormParams.asScala must containTheSameElementsAs(
List(new play.shaded.ahc.org.asynchttpclient.Param("param1", "value1"))
)
req.getByteData must beNull // should NOT result in byte data.
val headers = req.getHeaders
headers.get("Content-Length") must beNull
}
"Not remove a user defined content length header" in {
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.withBody(Map("param1" -> Seq("value1")))
.addHttpHeaders("Content-Length" -> "9001") // add a meaningless content length here...
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
new String(req.getByteData, StandardCharsets.UTF_8) must be_==("param1=value1") // should result in byte data.
val headers = req.getHeaders
headers.get("Content-Length") must_== ("9001")
}
"Remove a user defined content length header if we are parsing body explicitly when signed" in {
import scala.jdk.CollectionConverters._
val consumerKey = ConsumerKey("key", "secret")
val requestToken = RequestToken("token", "secret")
val calc = OAuthCalculator(consumerKey, requestToken)
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.withBody(Map("param1" -> Seq("value1")))
.addHttpHeaders("Content-Length" -> "9001") // add a meaningless content length here...
.sign(calc) // this is signed, so content length is no longer valid per #5221
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
val headers = req.getHeaders
req.getByteData must beNull // should NOT result in byte data.
req.getFormParams.asScala must containTheSameElementsAs(
List(new play.shaded.ahc.org.asynchttpclient.Param("param1", "value1"))
)
headers.get("Content-Length") must beNull // no content length!
}
"Verify Content-Type header is passed through correctly" in {
import scala.jdk.CollectionConverters._
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.addHttpHeaders("Content-Type" -> "text/plain; charset=US-ASCII")
.withBody("HELLO WORLD")
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
req.getHeaders.getAll("Content-Type").asScala must_== Seq("text/plain; charset=US-ASCII")
}
"POST binary data as is" in {
val binData = ByteString((0 to 511).map(_.toByte).toArray)
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.addHttpHeaders("Content-Type" -> "application/x-custom-bin-data")
.withBody(binData)
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
ByteString(req.getByteData) must_== binData
}
"support a virtual host" in {
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.withVirtualHost("192.168.1.1")
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
(req.getVirtualHost must be).equalTo("192.168.1.1")
}
"support follow redirects" in {
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.withFollowRedirects(follow = true)
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
req.getFollowRedirect must beEqualTo(true)
}
"support finite timeout" in {
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.withRequestTimeout(1000.millis)
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
(req.getRequestTimeout must be).equalTo(1000)
}
"support infinite timeout" in {
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.withRequestTimeout(Duration.Inf)
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
(req.getRequestTimeout must be).equalTo(-1)
}
"not support negative timeout" in {
makeAhcRequest("http://playframework.com/").withRequestTimeout(-1.millis) should throwAn[IllegalArgumentException]
}
"not support a timeout greater than Int.MaxValue" in {
makeAhcRequest("http://playframework.com/").withRequestTimeout((Int.MaxValue.toLong + 1).millis) should throwAn[
IllegalArgumentException
]
}
"support a proxy server with basic" in {
val proxy = DefaultWSProxyServer(
protocol = Some("https"),
host = "localhost",
port = 8080,
principal = Some("principal"),
password = Some("password")
)
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.withProxyServer(proxy)
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
val actual = req.getProxyServer
(actual.getHost must be).equalTo("localhost")
(actual.getPort must be).equalTo(8080)
(actual.getRealm.getPrincipal must be).equalTo("principal")
(actual.getRealm.getPassword must be).equalTo("password")
(actual.getRealm.getScheme must be).equalTo(AuthScheme.BASIC)
}
"support a proxy server with NTLM" in {
val proxy = DefaultWSProxyServer(
protocol = Some("ntlm"),
host = "localhost",
port = 8080,
principal = Some("principal"),
password = Some("password"),
ntlmDomain = Some("somentlmdomain")
)
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.withProxyServer(proxy)
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
val actual = req.getProxyServer
(actual.getHost must be).equalTo("localhost")
(actual.getPort must be).equalTo(8080)
(actual.getRealm.getPrincipal must be).equalTo("principal")
(actual.getRealm.getPassword must be).equalTo("password")
(actual.getRealm.getNtlmDomain must be).equalTo("somentlmdomain")
(actual.getRealm.getScheme must be).equalTo(AuthScheme.NTLM)
}
"Set Realm.UsePreemptiveAuth to false when WSAuthScheme.DIGEST being used" in {
val req = makeAhcRequest("http://playframework.com/")
.withAuth("usr", "pwd", WSAuthScheme.DIGEST)
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
req.getRealm.isUsePreemptiveAuth must beFalse
}
"Set Realm.UsePreemptiveAuth to true when WSAuthScheme.DIGEST not being used" in {
val req = makeAhcRequest("http://playframework.com/")
.withAuth("usr", "pwd", WSAuthScheme.BASIC)
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
req.getRealm.isUsePreemptiveAuth must beTrue
}
"support a proxy server" in {
val proxy = DefaultWSProxyServer(host = "localhost", port = 8080)
val req: AHCRequest = makeAhcRequest("http://playframework.com/")
.withProxyServer(proxy)
.asInstanceOf[AhcWSRequest]
.underlying
.buildRequest()
val actual = req.getProxyServer
(actual.getHost must be).equalTo("localhost")
(actual.getPort must be).equalTo(8080)
actual.getRealm must beNull
}
def patchFakeApp = {
val routes: (Application) => PartialFunction[(String, String), Handler] = { (app: Application) =>
{
case ("PATCH", "/") =>
val action = app.injector.instanceOf(classOf[DefaultActionBuilder])
action {
Results.Ok(play.api.libs.json.Json.parse("""{
| "data": "body"
|}
""".stripMargin))
}
}
}
GuiceApplicationBuilder().appRoutes(routes).build()
}
"support patch method" in new WithServer(patchFakeApp) {
// NOTE: if you are using a client proxy like Privoxy or Polipo, your proxy may not support PATCH & return 400.
{
val wsClient = app.injector.instanceOf(classOf[play.api.libs.ws.WSClient])
val futureResponse = wsClient.url(s"http://localhost:${Helpers.testServerPort}/").patch("body")
// This test experiences CI timeouts. Give it more time.
val reallyLongTimeout = Timeout(defaultAwaitTimeout.duration * 3)
val rep = await(futureResponse)(reallyLongTimeout)
rep.status must ===(200)
(rep.json \ "data").asOpt[String] must beSome("body")
}
}
def gzipFakeApp = {
import java.io._
import java.util.zip._
lazy val Action = ActionBuilder.ignoringBody
val routes: Application => PartialFunction[(String, String), Handler] = { app =>
{
case ("GET", "/") =>
Action { request =>
request.headers.get("Accept-Encoding") match {
case Some(encoding) if encoding.contains("gzip") =>
val os = new ByteArrayOutputStream
val gzipOs = new GZIPOutputStream(os)
gzipOs.write("gziped response".getBytes(StandardCharsets.UTF_8))
gzipOs.close()
Results.Ok(os.toByteArray).as("text/plain").withHeaders("Content-Encoding" -> "gzip")
case _ =>
Results.Ok("plain response")
}
}
}
}
GuiceApplicationBuilder()
.configure("play.ws.compressionEnabled" -> true)
.appRoutes(routes)
.build()
}
"support gziped encoding" in new WithServer(gzipFakeApp) {
val client = app.injector.instanceOf[WSClient]
val req = client.url("http://localhost:" + port + "/").get()
val rep = Await.result(req, 1.second)
rep.body must ===("gziped response")
}
def multipartFormDataFakeApp = {
val routes: (Application) => PartialFunction[(String, String), Handler] = { (app: Application) =>
{
case ("POST", "/") =>
val action = app.injector.instanceOf(classOf[DefaultActionBuilder])
action { request =>
Results.Ok(
request.body.asMultipartFormData
.map(mpf => {
"dataPart name: " + mpf.dataParts.keys.mkString(",") + "\n" +
"filePart names: " + mpf.files.map(_.key).mkString(",") + "\n" +
"filePart filenames: " + mpf.files.map(_.filename).mkString(",")
})
.getOrElse("")
)
}
}
}
GuiceApplicationBuilder().appRoutes(routes).build()
}
"escape 'name' and 'filename' params of a multipart form body" in new WithServer(multipartFormDataFakeApp) {
{
val wsClient = app.injector.instanceOf(classOf[play.api.libs.ws.WSClient])
val file = new java.io.File(this.getClass.getResource("/testassets/foo.txt").toURI)
val dp = MultipartFormData.DataPart("h\"e\rl\nl\"o\rwo\nrld", "world")
val fp =
MultipartFormData.FilePart("u\"p\rl\no\"a\rd", "f\"o\ro\n_\"b\ra\nr.txt", None, FileIO.fromPath(file.toPath))
val source = Source(List(dp, fp))
val futureResponse = wsClient.url(s"http://localhost:${Helpers.testServerPort}/").post(source)
// This test could experience CI timeouts. Give it more time.
val reallyLongTimeout = Timeout(defaultAwaitTimeout.duration * 3)
val rep = await(futureResponse)(reallyLongTimeout)
rep.status must ===(200)
rep.body must be_==("""dataPart name: h%22e%0Dl%0Al%22o%0Dwo%0Arld
|filePart names: u%22p%0Dl%0Ao%22a%0Dd
|filePart filenames: f%22o%0Do%0A_%22b%0Da%0Ar.txt""".stripMargin)
}
}
"Ahc WS Response" should {
"get cookies from an AHC response" in {
val ahcResponse: AHCResponse = mock[AHCResponse]
val (name, value, wrap, domain, path, maxAge, secure, httpOnly) =
("someName", "someValue", true, "example.com", "/", 1000L, false, false)
val ahcCookie = createCookie(name, value, wrap, domain, path, maxAge, secure, httpOnly)
ahcResponse.getCookies.returns(util.Arrays.asList(ahcCookie))
val response = makeAhcResponse(ahcResponse)
val cookies: scala.collection.Seq[WSCookie] = response.cookies
val cookie = cookies.head
cookie.name must ===(name)
cookie.value must ===(value)
cookie.domain must beSome(domain)
cookie.path must beSome(path)
cookie.maxAge must beSome(maxAge)
cookie.secure must beFalse
}
"get a single cookie from an AHC response" in {
val ahcResponse: AHCResponse = mock[AHCResponse]
val (name, value, wrap, domain, path, maxAge, secure, httpOnly) =
("someName", "someValue", true, "example.com", "/", 1000L, false, false)
val ahcCookie = createCookie(name, value, wrap, domain, path, maxAge, secure, httpOnly)
ahcResponse.getCookies.returns(util.Arrays.asList(ahcCookie))
val response = makeAhcResponse(ahcResponse)
val optionCookie = response.cookie("someName")
optionCookie must beSome[WSCookie].which { cookie =>
cookie.name must ===(name)
cookie.value must ===(value)
cookie.domain must beSome(domain)
cookie.path must beSome(path)
cookie.maxAge must beSome(maxAge)
cookie.secure must beFalse
}
}
"return -1 values of expires and maxAge as None" in {
val ahcResponse: AHCResponse = mock[AHCResponse]
val ahcCookie = createCookie("someName", "value", true, "domain", "path", -1L, false, false)
ahcResponse.getCookies.returns(util.Arrays.asList(ahcCookie))
val response = makeAhcResponse(ahcResponse)
val optionCookie = response.cookie("someName")
optionCookie must beSome[WSCookie].which { cookie => cookie.maxAge must beNone }
}
"get the body as bytes from the AHC response" in {
val ahcResponse: AHCResponse = mock[AHCResponse]
val bytes = ByteString(-87, -72, 96, -63, -32, 46, -117, -40, -128, -7, 61, 109, 80, 45, 44, 30)
ahcResponse.getResponseBodyAsBytes.returns(bytes.toArray)
val response = makeAhcResponse(ahcResponse)
response.bodyAsBytes must_== bytes
}
"get headers from an AHC response in a case insensitive map" in {
val ahcResponse: AHCResponse = mock[AHCResponse]
val ahcHeaders = new DefaultHttpHeaders(true)
ahcHeaders.add("Foo", "bar")
ahcHeaders.add("Foo", "baz")
ahcHeaders.add("Bar", "baz")
ahcResponse.getHeaders.returns(ahcHeaders)
val response = makeAhcResponse(ahcResponse)
val headers = response.headers
headers must beEqualTo(Map("Foo" -> Seq("bar", "baz"), "Bar" -> Seq("baz")))
headers.contains("foo") must beTrue
headers.contains("Foo") must beTrue
headers.contains("BAR") must beTrue
headers.contains("Bar") must beTrue
}
}
def createCookie(
name: String,
value: String,
wrap: Boolean,
domain: String,
path: String,
maxAge: Long,
secure: Boolean,
httpOnly: Boolean
): NettyCookie = {
val ahcCookie = new NettyDefaultCookie(name, value)
ahcCookie.setWrap(wrap)
ahcCookie.setDomain(domain)
ahcCookie.setPath(path)
ahcCookie.setMaxAge(maxAge)
ahcCookie.setSecure(secure)
ahcCookie.setHttpOnly(httpOnly)
ahcCookie
}
def makeAhcResponse(ahcResponse: AHCResponse): AhcWSResponse = {
AhcWSResponse(StandaloneAhcWSResponse(ahcResponse))
}
"Ahc WS Config" should {
"support overriding secure default values" in {
val ahcConfig = new AhcConfigBuilder()
.modifyUnderlying { builder =>
builder.setCompressionEnforced(false)
builder.setFollowRedirect(false)
}
.build()
ahcConfig.isCompressionEnforced must beFalse
ahcConfig.isFollowRedirect must beFalse
ahcConfig.getConnectTimeout must_== 120000
ahcConfig.getRequestTimeout must_== 120000
ahcConfig.getReadTimeout must_== 120000
}
}
}