/
LowLevelOutgoingConnectionSpec.scala
1022 lines (867 loc) · 33.8 KB
/
LowLevelOutgoingConnectionSpec.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.http.impl.engine.client
import com.typesafe.config.ConfigFactory
import scala.concurrent.duration._
import scala.reflect.ClassTag
import org.scalatest.Inside
import akka.http.scaladsl.settings.ClientConnectionSettings
import akka.util.ByteString
import akka.event.NoLogging
import akka.stream.ClosedShape
import akka.stream.TLSProtocol._
import akka.stream.testkit._
import akka.stream.scaladsl._
import akka.http.scaladsl.model.HttpEntity._
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers._
import akka.http.impl.util._
import akka.testkit._
class LowLevelOutgoingConnectionSpec extends AkkaSpecWithMaterializer with Inside {
implicit val dispatcher = system.dispatcher
"The connection-level client implementation" should {
"handle a request/response round-trip" which {
"has a request with empty entity" in new TestSetup {
sendStandardRequest()
sendWireData(
"""HTTP/1.1 200 OK
|Content-Length: 0
|
|""")
expectResponse() shouldEqual HttpResponse()
requestsSub.sendComplete()
netOut.expectComplete()
netInSub.sendComplete()
responses.expectComplete()
}
"has a request with default entity" in new TestSetup {
val probe = TestPublisher.manualProbe[ByteString]()
requestsSub.sendNext(HttpRequest(PUT, entity = HttpEntity(ContentTypes.`application/octet-stream`, 8, Source.fromPublisher(probe))))
expectWireData(
"""PUT / HTTP/1.1
|Host: example.com
|User-Agent: akka-http/test
|Content-Type: application/octet-stream
|Content-Length: 8
|
|""")
val sub = probe.expectSubscription()
sub.expectRequest()
sub.sendNext(ByteString("ABC"))
expectWireData("ABC")
sub.sendNext(ByteString("DEF"))
expectWireData("DEF")
sub.sendNext(ByteString("XY"))
expectWireData("XY")
sub.sendComplete()
sendWireData(
"""HTTP/1.1 200 OK
|Content-Length: 0
|
|""")
expectResponse() shouldEqual HttpResponse()
requestsSub.sendComplete()
netOut.expectComplete()
netInSub.sendComplete()
responses.expectComplete()
}
"has a response with a chunked entity" in new TestSetup {
sendStandardRequest()
sendWireData(
"""HTTP/1.1 200 OK
|Transfer-Encoding: chunked
|
|""")
val HttpResponse(_, _, HttpEntity.Chunked(ct, chunks), _) = expectResponse()
ct shouldEqual ContentTypes.`application/octet-stream`
val probe = TestSubscriber.manualProbe[ChunkStreamPart]()
chunks.runWith(Sink.fromSubscriber(probe))
val sub = probe.expectSubscription()
sendWireData("3\nABC\n")
sub.request(1)
probe.expectNext(HttpEntity.Chunk("ABC"))
sendWireData("4\nDEFX\n")
sub.request(1)
probe.expectNext(HttpEntity.Chunk("DEFX"))
sendWireData("0\n\n")
sub.request(1)
probe.expectNext(HttpEntity.LastChunk)
sub.request(1)
probe.expectComplete()
requestsSub.sendComplete()
netOut.expectComplete()
responses.expectComplete()
}
"has a response with a chunked entity and Connection: close" in new TestSetup {
sendStandardRequest()
sendWireData(
"""HTTP/1.1 200 OK
|Transfer-Encoding: chunked
|Connection: close
|
|""")
sendWireData("3\nABC\n")
sendWireData("4\nDEFX\n")
sendWireData("0\n\n")
val HttpResponse(_, _, HttpEntity.Chunked(ct, chunks), _) = expectResponse()
ct shouldEqual ContentTypes.`application/octet-stream`
val probe = TestSubscriber.manualProbe[ChunkStreamPart]()
chunks.runWith(Sink.fromSubscriber(probe))
val sub = probe.expectSubscription()
sub.request(4)
probe.expectNext(HttpEntity.Chunk("ABC"))
probe.expectNext(HttpEntity.Chunk("DEFX"))
probe.expectNext(HttpEntity.LastChunk)
probe.expectComplete()
// explicit `requestsSub.sendComplete()` not needed
netOut.expectComplete()
responses.expectComplete()
}
"has a request with a chunked entity and Connection: close" in new TestSetup {
requestsSub.sendNext(HttpRequest(
entity = HttpEntity(ContentTypes.`text/plain(UTF-8)`, Source(List("ABC", "DEFX").map(ByteString(_)))),
headers = List(Connection("close"))
))
expectWireData(
"""GET / HTTP/1.1
|Connection: close
|Host: example.com
|User-Agent: akka-http/test
|Transfer-Encoding: chunked
|Content-Type: text/plain; charset=UTF-8
|
|""")
expectWireData("3\nABC\n")
expectWireData("4\nDEFX\n")
expectWireData("0\n\n")
sendWireData(
"""HTTP/1.1 200 OK
|Connection: close
|Content-Length: 0
|
|""")
expectResponse()
// explicit `requestsSub.sendComplete()` not needed
responses.expectComplete()
netOut.expectComplete()
}
"has a request with a overridden User-Agent RawHeader" in new TestSetup {
val request = HttpRequest().addHeader(RawHeader("User-Agent", "akka-http/test-overridden"))
requestsSub.sendNext(request)
expectWireData(
"""GET / HTTP/1.1
|User-Agent: akka-http/test-overridden
|Host: example.com
|
|""")
sendWireData(
"""HTTP/1.1 200 OK
|Content-Length: 0
|
|""")
expectResponse() shouldEqual HttpResponse()
requestsSub.sendComplete()
netOut.expectComplete()
netInSub.sendComplete()
responses.expectComplete()
}
"has a request with a overridden Host RawHeader" in new TestSetup {
val request = HttpRequest().addHeader(RawHeader("Host", "testhost.com"))
requestsSub.sendNext(request)
expectWireData(
"""GET / HTTP/1.1
|Host: testhost.com
|User-Agent: akka-http/test
|
|""")
sendWireData(
"""HTTP/1.1 200 OK
|Content-Length: 0
|
|""")
expectResponse() shouldEqual HttpResponse()
requestsSub.sendComplete()
netOut.expectComplete()
netInSub.sendComplete()
responses.expectComplete()
}
"exhibits eager request stream completion" in new TestSetup {
requestsSub.sendNext(HttpRequest())
requestsSub.sendComplete()
expectWireData(
"""GET / HTTP/1.1
|Host: example.com
|User-Agent: akka-http/test
|
|""")
sendWireData(
"""HTTP/1.1 200 OK
|Content-Length: 0
|
|""")
expectResponse() shouldEqual HttpResponse()
netOut.expectComplete()
netInSub.sendComplete()
responses.expectComplete()
}
}
"close the connection if response entity stream has been cancelled" in new TestSetup {
// two requests are sent in order to make sure that connection
// isn't immediately closed after the first one by the server
requestsSub.sendNext(HttpRequest())
requestsSub.sendNext(HttpRequest())
requestsSub.sendComplete()
expectWireData(
"""GET / HTTP/1.1
|Host: example.com
|User-Agent: akka-http/test
|
|""")
// two chunks sent by server
sendWireData(
"""HTTP/1.1 200 OK
|Transfer-Encoding: chunked
|
|6
|abcdef
|6
|abcdef
|0
|
|""")
inside(expectResponse()) {
case HttpResponse(StatusCodes.OK, _, HttpEntity.Chunked(_, data), _) =>
val dataProbe = TestSubscriber.manualProbe[ChunkStreamPart]()
// but only one consumed by server
data.take(1).to(Sink.fromSubscriber(dataProbe)).run()
val sub = dataProbe.expectSubscription()
sub.request(1)
dataProbe.expectNext(Chunk(ByteString("abcdef")))
dataProbe.expectComplete()
// connection is closed once requested elements are consumed
netInSub.expectCancellation()
}
}
"proceed to next response once previous response's entity has been drained" in new TestSetup {
def twice(action: => Unit): Unit = { action; action }
twice {
requestsSub.sendNext(HttpRequest())
expectWireData(
"""GET / HTTP/1.1
|Host: example.com
|User-Agent: akka-http/test
|
|""")
sendWireData(
"""HTTP/1.1 200 OK
|Transfer-Encoding: chunked
|
|6
|abcdef
|0
|
|""")
val whenComplete = expectResponse().entity.dataBytes.runWith(Sink.ignore)
whenComplete.futureValue should be(akka.Done)
}
}
"handle several requests on one persistent connection" which {
"has a first response that was chunked" in new TestSetup {
requestsSub.sendNext(HttpRequest())
expectWireData(
"""GET / HTTP/1.1
|Host: example.com
|User-Agent: akka-http/test
|
|""")
sendWireData(
"""HTTP/1.1 200 OK
|Transfer-Encoding: chunked
|
|""")
val HttpResponse(_, _, HttpEntity.Chunked(_, chunks), _) = expectResponse()
val probe = TestSubscriber.manualProbe[ChunkStreamPart]()
chunks.runWith(Sink.fromSubscriber(probe))
val sub = probe.expectSubscription()
sendWireData("3\nABC\n")
sub.request(1)
probe.expectNext(HttpEntity.Chunk("ABC"))
sendWireData("0\n\n")
sub.request(1)
probe.expectNext(HttpEntity.LastChunk)
sub.request(1)
probe.expectComplete()
// simulate that response is received before method bypass reaches response parser
sendWireData(
"""HTTP/1.1 200 OK
|Content-Length: 0
|
|""")
responsesSub.request(1)
requestsSub.sendNext(HttpRequest())
expectWireData(
"""GET / HTTP/1.1
|Host: example.com
|User-Agent: akka-http/test
|
|""")
requestsSub.sendComplete()
responses.expectNext(HttpResponse())
netOut.expectComplete()
netInSub.sendComplete()
responses.expectComplete()
}
}
"process the illegal response header value properly" which {
val illegalChar = '\u0001'
val escapeChar = "\\u%04x" format illegalChar.toInt
"catch illegal response header value by default" in new TestSetup {
sendStandardRequest()
sendWireData(
s"""HTTP/1.1 200 OK
|Some-Header: value1$illegalChar
|Other-Header: value2
|
|""")
responsesSub.request(1)
val error @ IllegalResponseException(info) = responses.expectError()
info.summary shouldEqual s"""Illegal character '$escapeChar' in header value"""
netOut.expectError(error)
requestsSub.expectCancellation()
netInSub.expectCancellation()
}
val ignoreConfig =
"""
akka.http.parsing.illegal-response-header-value-processing-mode = ignore
"""
"ignore illegal response header value if setting the config to ignore" in new TestSetup(config = ignoreConfig) {
sendStandardRequest()
sendWireData(
s"""HTTP/1.1 200 OK
|Some-Header: value1$illegalChar
|Other-Header: value2
|
|""")
val HttpResponse(_, headers, _, _) = expectResponse()
val headerStr = headers.map(h => s"${h.name}: ${h.value}").mkString(",")
headerStr shouldEqual "Some-Header: value1,Other-Header: value2"
}
val warnConfig =
"""
akka.http.parsing.illegal-response-header-value-processing-mode = warn
"""
"ignore illegal response header value and log a warning message if setting the config to warn" in new TestSetup(config = warnConfig) {
sendStandardRequest()
sendWireData(
s"""HTTP/1.1 200 OK
|Some-Header: value1$illegalChar
|Other-Header: value2
|
|""")
val HttpResponse(_, headers, _, _) = expectResponse()
val headerStr = headers.map(h => s"${h.name}: ${h.value}").mkString(",")
headerStr shouldEqual "Some-Header: value1,Other-Header: value2"
}
}
"produce proper errors" which {
"catch the request entity stream being shorter than the Content-Length" in new TestSetup {
val probe = TestPublisher.manualProbe[ByteString]()
requestsSub.sendNext(HttpRequest(PUT, entity = HttpEntity(ContentTypes.`application/octet-stream`, 8, Source.fromPublisher(probe))))
expectWireData(
"""PUT / HTTP/1.1
|Host: example.com
|User-Agent: akka-http/test
|Content-Type: application/octet-stream
|Content-Length: 8
|
|""")
val sub = probe.expectSubscription()
sub.expectRequest()
sub.sendNext(ByteString("ABC"))
expectWireData("ABC")
sub.sendNext(ByteString("DEF"))
expectWireData("DEF")
sub.sendComplete()
val InvalidContentLengthException(info) = netOut.expectError()
info.summary shouldEqual "HTTP message had declared Content-Length 8 but entity data stream amounts to 2 bytes less"
netInSub.sendComplete()
responsesSub.request(1)
responses.expectError().getMessage should (
equal("HTTP message had declared Content-Length 8 but entity data stream amounts to 2 bytes less") or // with Akka 2.6
equal("The http server closed the connection unexpectedly before delivering responses for 1 outstanding requests") // with Akka 2.5
)
}
"catch the request entity stream being longer than the Content-Length" in new TestSetup {
val probe = TestPublisher.manualProbe[ByteString]()
requestsSub.sendNext(HttpRequest(PUT, entity = HttpEntity(ContentTypes.`application/octet-stream`, 8, Source.fromPublisher(probe))))
expectWireData(
"""PUT / HTTP/1.1
|Host: example.com
|User-Agent: akka-http/test
|Content-Type: application/octet-stream
|Content-Length: 8
|
|""")
val sub = probe.expectSubscription()
sub.expectRequest()
sub.sendNext(ByteString("ABC"))
expectWireData("ABC")
sub.sendNext(ByteString("DEF"))
expectWireData("DEF")
sub.sendNext(ByteString("XYZ"))
val InvalidContentLengthException(info) = netOut.expectError()
info.summary shouldEqual "HTTP message had declared Content-Length 8 but entity data stream amounts to more bytes"
netInSub.sendComplete()
responsesSub.request(1)
responses.expectError().getMessage should (
equal("HTTP message had declared Content-Length 8 but entity data stream amounts to more bytes") or // with Akka 2.6
equal("The http server closed the connection unexpectedly before delivering responses for 1 outstanding requests") // with Akka 2.5
)
}
"catch illegal response starts" in new TestSetup {
sendStandardRequest()
sendWireData(
"""HTTP/1.2 200 OK
|
|""")
responsesSub.request(1)
val error @ IllegalResponseException(info) = responses.expectError()
info.summary shouldEqual "The server-side HTTP version is not supported"
netOut.expectError(error)
requestsSub.expectCancellation()
netInSub.expectCancellation()
}
"catch illegal response chunks" in new TestSetup {
sendStandardRequest()
sendWireData(
"""HTTP/1.1 200 OK
|Transfer-Encoding: chunked
|
|""")
responsesSub.request(1)
val HttpResponse(_, _, HttpEntity.Chunked(ct, chunks), _) = responses.expectNext()
ct shouldEqual ContentTypes.`application/octet-stream`
val probe = TestSubscriber.manualProbe[ChunkStreamPart]()
chunks.runWith(Sink.fromSubscriber(probe))
val sub = probe.expectSubscription()
sendWireData("3\nABC\n")
sub.request(1)
probe.expectNext(HttpEntity.Chunk("ABC"))
sendWireData("4\nDEFXX")
sub.request(1)
val _@ EntityStreamException(info) = probe.expectError()
info.summary shouldEqual "Illegal chunk termination"
responses.expectComplete()
netOut.expectComplete()
requestsSub.expectCancellation()
netInSub.expectCancellation()
}
"catch a response start truncation" in new TestSetup {
sendStandardRequest()
sendWireData("HTTP/1.1 200 OK")
netInSub.sendComplete()
responsesSub.request(1)
val error @ IllegalResponseException(info) = responses.expectError()
info.summary shouldEqual "Illegal HTTP message start"
netOut.expectError(error)
requestsSub.expectCancellation()
}
}
def isDefinedVia = afterWord("is defined via")
"support response length verification" which isDefinedVia {
import HttpEntity._
class LengthVerificationTest(maxContentLength: Int) extends TestSetup(maxContentLength) {
val entityBase = "0123456789ABCD"
def sendStrictResponseWithLength(bytes: Int) =
sendWireData(
s"""HTTP/1.1 200 OK
|Content-Length: $bytes
|
|${entityBase take bytes}""")
def sendDefaultResponseWithLength(bytes: Int) = {
sendWireData(
s"""HTTP/1.1 200 OK
|Content-Length: $bytes
|
|${entityBase take 3}""")
sendWireData(entityBase.slice(3, 7))
sendWireData(entityBase.slice(7, bytes))
}
def sendChunkedResponseWithLength(bytes: Int) =
sendWireData(
s"""HTTP/1.1 200 OK
|Transfer-Encoding: chunked
|
|3
|${entityBase take 3}
|4
|${entityBase.slice(3, 7)}
|${bytes - 7}
|${entityBase.slice(7, bytes)}
|0
|
|""")
def sendCloseDelimitedResponseWithLength(bytes: Int) = {
sendWireData(
s"""HTTP/1.1 200 OK
|
|${entityBase take 3}""")
sendWireData(entityBase.slice(3, 7))
sendWireData(entityBase.slice(7, bytes))
netInSub.sendComplete()
}
implicit class XResponse(response: HttpResponse) {
val timeout = 500.millis.dilated
def expectStrictEntityWithLength(bytes: Int) =
response shouldEqual HttpResponse(
entity = Strict(ContentTypes.`application/octet-stream`, ByteString(entityBase take bytes)))
def expectEntity[T <: HttpEntity: ClassTag](bytes: Int) =
inside(response) {
case HttpResponse(_, _, entity: T, _) =>
entity.toStrict(100.millis.dilated).awaitResult(timeout).data.utf8String shouldEqual entityBase.take(bytes)
}
def expectSizeErrorInEntityOfType[T <: HttpEntity: ClassTag](limit: Int, actualSize: Option[Long] = None) =
inside(response) {
case HttpResponse(_, _, entity: T, _) =>
def gatherBytes = entity.dataBytes.runFold(ByteString.empty)(_ ++ _).awaitResult(timeout)
(the[RuntimeException] thrownBy gatherBytes).getCause shouldEqual EntityStreamSizeException(limit, actualSize)
}
}
}
"the config setting (strict entity)" in new LengthVerificationTest(maxContentLength = 10) {
sendStandardRequest()
sendStrictResponseWithLength(10)
expectResponse().expectStrictEntityWithLength(10)
// entities that would be strict but have a Content-Length > the configured maximum are delivered
// as single element Default entities!
sendStandardRequest()
sendStrictResponseWithLength(11)
expectResponse().expectSizeErrorInEntityOfType[Default](limit = 10, actualSize = Some(11))
}
"the config setting (default entity)" in new LengthVerificationTest(maxContentLength = 10) {
sendStandardRequest()
sendDefaultResponseWithLength(10)
expectResponse().expectEntity[Default](10)
sendStandardRequest()
sendDefaultResponseWithLength(11)
expectResponse().expectSizeErrorInEntityOfType[Default](limit = 10, actualSize = Some(11))
}
"the config setting (chunked entity)" in new LengthVerificationTest(maxContentLength = 10) {
sendStandardRequest()
sendChunkedResponseWithLength(10)
expectResponse().expectEntity[Chunked](10)
sendStandardRequest()
sendChunkedResponseWithLength(11)
expectResponse().expectSizeErrorInEntityOfType[Chunked](limit = 10)
}
"the config setting (close-delimited entity)" in {
new LengthVerificationTest(maxContentLength = 10) {
sendStandardRequest()
sendCloseDelimitedResponseWithLength(10)
expectResponse().expectEntity[CloseDelimited](10)
}
new LengthVerificationTest(maxContentLength = 10) {
sendStandardRequest()
sendCloseDelimitedResponseWithLength(11)
expectResponse().expectSizeErrorInEntityOfType[CloseDelimited](limit = 10)
}
}
"a smaller programmatically-set limit (strict entity)" in new LengthVerificationTest(maxContentLength = 12) {
sendStandardRequest()
sendStrictResponseWithLength(10)
expectResponse().mapEntity(_ withSizeLimit 10).expectStrictEntityWithLength(10)
// entities that would be strict but have a Content-Length > the configured maximum are delivered
// as single element Default entities!
sendStandardRequest()
sendStrictResponseWithLength(11)
expectResponse().mapEntity(_ withSizeLimit 10)
.expectSizeErrorInEntityOfType[Default](limit = 10, actualSize = Some(11))
}
"a smaller programmatically-set limit (default entity)" in new LengthVerificationTest(maxContentLength = 12) {
sendStandardRequest()
sendDefaultResponseWithLength(10)
expectResponse().mapEntity(_ withSizeLimit 10).expectEntity[Default](10)
sendStandardRequest()
sendDefaultResponseWithLength(11)
expectResponse().mapEntity(_ withSizeLimit 10)
.expectSizeErrorInEntityOfType[Default](limit = 10, actualSize = Some(11))
}
"a smaller programmatically-set limit (chunked entity)" in new LengthVerificationTest(maxContentLength = 12) {
sendStandardRequest()
sendChunkedResponseWithLength(10)
expectResponse().mapEntity(_ withSizeLimit 10).expectEntity[Chunked](10)
sendStandardRequest()
sendChunkedResponseWithLength(11)
expectResponse().mapEntity(_ withSizeLimit 10).expectSizeErrorInEntityOfType[Chunked](limit = 10)
}
"a smaller programmatically-set limit (close-delimited entity)" in {
new LengthVerificationTest(maxContentLength = 12) {
sendStandardRequest()
sendCloseDelimitedResponseWithLength(10)
expectResponse().mapEntity(_ withSizeLimit 10).expectEntity[CloseDelimited](10)
}
new LengthVerificationTest(maxContentLength = 12) {
sendStandardRequest()
sendCloseDelimitedResponseWithLength(11)
expectResponse().mapEntity(_ withSizeLimit 10).expectSizeErrorInEntityOfType[CloseDelimited](limit = 10)
}
}
"a larger programmatically-set limit (strict entity)" in new LengthVerificationTest(maxContentLength = 8) {
// entities that would be strict but have a Content-Length > the configured maximum are delivered
// as single element Default entities!
sendStandardRequest()
sendStrictResponseWithLength(10)
expectResponse().mapEntity(_ withSizeLimit 10).expectEntity[Default](10)
sendStandardRequest()
sendStrictResponseWithLength(11)
expectResponse().mapEntity(_ withSizeLimit 10)
.expectSizeErrorInEntityOfType[Default](limit = 10, actualSize = Some(11))
}
"a larger programmatically-set limit (default entity)" in new LengthVerificationTest(maxContentLength = 8) {
sendStandardRequest()
sendDefaultResponseWithLength(10)
expectResponse().mapEntity(_ withSizeLimit 10).expectEntity[Default](10)
sendStandardRequest()
sendDefaultResponseWithLength(11)
expectResponse().mapEntity(_ withSizeLimit 10)
.expectSizeErrorInEntityOfType[Default](limit = 10, actualSize = Some(11))
}
"a larger programmatically-set limit (chunked entity)" in new LengthVerificationTest(maxContentLength = 8) {
sendStandardRequest()
sendChunkedResponseWithLength(10)
expectResponse().mapEntity(_ withSizeLimit 10).expectEntity[Chunked](10)
sendStandardRequest()
sendChunkedResponseWithLength(11)
expectResponse().mapEntity(_ withSizeLimit 10)
.expectSizeErrorInEntityOfType[Chunked](limit = 10)
}
"a larger programmatically-set limit (close-delimited entity)" in {
new LengthVerificationTest(maxContentLength = 8) {
sendStandardRequest()
sendCloseDelimitedResponseWithLength(10)
expectResponse().mapEntity(_ withSizeLimit 10).expectEntity[CloseDelimited](10)
}
new LengthVerificationTest(maxContentLength = 8) {
sendStandardRequest()
sendCloseDelimitedResponseWithLength(11)
expectResponse().mapEntity(_ withSizeLimit 10).expectSizeErrorInEntityOfType[CloseDelimited](limit = 10)
}
}
}
"support requests with an `Expect: 100-continue` headers" which {
"have a strict entity and receive a `100 Continue` response" in new TestSetup {
requestsSub.sendNext(HttpRequest(POST, headers = List(Expect.`100-continue`), entity = "ABCDEF"))
expectWireData(
"""POST / HTTP/1.1
|Expect: 100-continue
|Host: example.com
|User-Agent: akka-http/test
|Content-Type: text/plain; charset=UTF-8
|Content-Length: 6
|
|""")
netOutSub.request(1)
netOut.expectNoMessage(50.millis)
sendWireData(
"""HTTP/1.1 100 Continue
|
|""")
netOut.expectNext().utf8String shouldEqual "ABCDEF"
sendWireData(
"""HTTP/1.1 200 OK
|Content-Length: 0
|
|""")
expectResponse() shouldEqual HttpResponse()
requestsSub.sendComplete()
netOut.expectComplete()
netInSub.sendComplete()
responses.expectComplete()
}
"have a default entity and receive a `100 Continue` response" in new TestSetup {
val entityParts = List("ABC", "DE", "FGH").map(ByteString(_))
requestsSub.sendNext(HttpRequest(POST, headers = List(Expect.`100-continue`),
entity = HttpEntity(ContentTypes.`application/octet-stream`, 8, Source(entityParts))))
expectWireData(
"""POST / HTTP/1.1
|Expect: 100-continue
|Host: example.com
|User-Agent: akka-http/test
|Content-Type: application/octet-stream
|Content-Length: 8
|
|""")
netOutSub.request(1)
netOut.expectNoMessage(50.millis)
sendWireData(
"""HTTP/1.1 100 Continue
|
|""")
netOut.expectNext().utf8String shouldEqual "ABC"
expectWireData("DE")
expectWireData("FGH")
sendWireData(
"""HTTP/1.1 200 OK
|Content-Length: 0
|
|""")
expectResponse() shouldEqual HttpResponse()
requestsSub.sendComplete()
netOut.expectComplete()
netInSub.sendComplete()
responses.expectComplete()
}
"receive a normal response" in new TestSetup {
requestsSub.sendNext(HttpRequest(POST, headers = List(Expect.`100-continue`), entity = "ABCDEF"))
expectWireData(
"""POST / HTTP/1.1
|Expect: 100-continue
|Host: example.com
|User-Agent: akka-http/test
|Content-Type: text/plain; charset=UTF-8
|Content-Length: 6
|
|""")
netOutSub.request(1)
netOut.expectNoMessage(50.millis)
sendWireData(
"""HTTP/1.1 200 OK
|Content-Length: 0
|
|""")
expectResponse() shouldEqual HttpResponse()
expectWireData("ABCDEF")
requestsSub.sendComplete()
netOut.expectComplete()
netInSub.sendComplete()
responses.expectComplete()
}
"receive an error response" in new TestSetup {
requestsSub.sendNext(HttpRequest(POST, headers = List(Expect.`100-continue`), entity = "ABCDEF"))
requestsSub.sendComplete()
expectWireData(
"""POST / HTTP/1.1
|Expect: 100-continue
|Host: example.com
|User-Agent: akka-http/test
|Content-Type: text/plain; charset=UTF-8
|Content-Length: 6
|
|""")
netOutSub.request(1)
netOut.expectNoMessage(50.millis)
sendWireData(
"""HTTP/1.1 400 Bad Request
|Content-Length: 0
|
|""")
expectResponse() shouldEqual HttpResponse(400)
netOut.expectComplete()
netInSub.sendComplete()
responses.expectComplete()
}
}
"ignore interim 1xx responses" in new TestSetup {
sendStandardRequest()
sendWireData(
"""HTTP/1.1 102 Processing
|Content-Length: 0
|
|""")
sendWireData(
"""HTTP/1.1 102 Processing
|Content-Length: 0
|
|""")
sendWireData(
"""HTTP/1.1 200 OK
|Content-Length: 0
|
|""")
expectResponse() shouldEqual HttpResponse()
requestsSub.sendComplete()
netOut.expectComplete()
netInSub.sendComplete()
responses.expectComplete()
}
"receive Content-Length for HEAD requests" in new TestSetup {
requestsSub.sendNext(HttpRequest(method = HttpMethods.HEAD))
expectWireData(
"""HEAD / HTTP/1.1
|Host: example.com
|User-Agent: akka-http/test
|
|""")
sendWireData(
"""HTTP/1.1 200 OK
|Content-Length: 6
|
|""")
val HttpResponse(StatusCodes.OK, _, HttpEntity.Default(_, contentLength, data), _) = expectResponse()
contentLength shouldEqual 6
val chunks = data.runWith(Sink.seq).awaitResult(3.seconds.dilated)
chunks.length shouldEqual 0
requestsSub.sendComplete()
netOut.expectComplete()
netInSub.sendComplete()
responses.expectComplete()
}
}
class TestSetup(maxResponseContentLength: Int = -1, config: String = "") {
val requests = TestPublisher.manualProbe[HttpRequest]()
val responses = TestSubscriber.manualProbe[HttpResponse]()
def settings = {
val s = ClientConnectionSettings(
ConfigFactory.parseString(config).withFallback(system.settings.config)
).withUserAgentHeader(Some(`User-Agent`(List(ProductVersion("akka-http", "test")))))
if (maxResponseContentLength < 0) s
else s.withParserSettings(s.parserSettings.withMaxContentLength(maxResponseContentLength))
}
val (netOut, netIn) = {
val netOut = TestSubscriber.manualProbe[ByteString]()
val netIn = TestPublisher.manualProbe[ByteString]()
RunnableGraph.fromGraph(GraphDSL.create(OutgoingConnectionBlueprint(Host("example.com"), settings, NoLogging)) { implicit b => client =>
import GraphDSL.Implicits._
Source.fromPublisher(netIn) ~> Flow[ByteString].map(SessionBytes(null, _)) ~> client.in2
client.out1 ~> Flow[SslTlsOutbound].collect { case SendBytes(x) => x } ~> Sink.fromSubscriber(netOut)
Source.fromPublisher(requests) ~> client.in1
client.out2 ~> Sink.fromSubscriber(responses)
ClosedShape
}).run()
netOut -> netIn
}
def wipeDate(string: String) =
string.fastSplit('\n').map {
case s if s.startsWith("Date:") => "Date: XXXX\r"
case s => s
}.mkString("\n")
val netInSub = netIn.expectSubscription()
val netOutSub = netOut.expectSubscription()
val requestsSub = requests.expectSubscription()
val responsesSub = responses.expectSubscription()
requestsSub.expectRequest(16)
netInSub.expectRequest(16)
def sendWireData(data: String): Unit = sendWireData(ByteString(data.stripMarginWithNewline("\r\n"), "ASCII"))
def sendWireData(data: ByteString): Unit = netInSub.sendNext(data)
def expectWireData(s: String) = {
netOutSub.request(1)