-
Notifications
You must be signed in to change notification settings - Fork 3
/
http-router.t
1848 lines (1682 loc) · 68.9 KB
/
http-router.t
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
use Cro;
use Cro::HTTP::Request;
use Cro::HTTP::Router;
use Test;
sub body-text(Cro::HTTP::Response $r) {
$r.body-byte-stream.list.map(*.decode('utf-8')).join
}
{
my $app = route -> { }
ok $app ~~ Cro::Transform, 'Route block with no routes gives back a Cro::Transform';
my $source = Supplier.new;
my $responses = $app.transformer($source.Supply).Channel;
$source.emit(Cro::HTTP::Request.new(:method<GET>, :target</>));
given $responses.receive -> $r {
ok $r ~~ Cro::HTTP::Response, 'Empty route set gives a response';
is $r.status, '404', 'Status code from empty route set is 404';
}
}
throws-like { request }, X::Cro::HTTP::Router::OnlyInHandler, what => 'request',
'Can only use request term inside of a handler';
throws-like { response }, X::Cro::HTTP::Router::OnlyInHandler, what => 'response',
'Can only use response term inside of a handler';
{
my $app = route {
get -> {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body('Hello, world'.encode('ascii'));
}
get -> 'about' {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body('We are the awesome'.encode('ascii'));
}
get -> 'company', 'careers' {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body('No jobs, kthxbai'.encode('ascii'));
}
}
ok $app ~~ Cro::Transform, 'Route block with routes gives back a Cro::Transform';
my $source = Supplier.new;
my $responses = $app.transformer($source.Supply).Channel;
$source.emit(Cro::HTTP::Request.new(:method<GET>, :target</>));
given $responses.receive -> $r {
ok $r ~~ Cro::HTTP::Response, 'Route set routes / correctly';
is $r.status, 200, 'Got 200 response';
is $r.header('Content-type'), 'text/html', 'Got expected header';
is-deeply body-text($r), 'Hello, world', 'Got expected body';
}
$source.emit(Cro::HTTP::Request.new(:method<GET>, :target</about>));
given $responses.receive -> $r {
ok $r ~~ Cro::HTTP::Response, 'Route set routes /about correctly';
is $r.status, 200, 'Got 200 response';
is $r.header('Content-type'), 'text/html', 'Got expected header';
is-deeply body-text($r), 'We are the awesome', 'Got expected body';
}
$source.emit(Cro::HTTP::Request.new(:method<GET>, :target</company/careers>));
given $responses.receive -> $r {
ok $r ~~ Cro::HTTP::Response, 'Route set routes /company/careers correctly';
is $r.status, 200, 'Got 200 response';
is $r.header('Content-type'), 'text/html', 'Got expected header';
is-deeply body-text($r), 'No jobs, kthxbai', 'Got expected body';
}
$source.emit(Cro::HTTP::Request.new(:method<GET>, :target</wat>));
given $responses.receive -> $r {
ok $r ~~ Cro::HTTP::Response, 'No matching route gets a HTTP response';
is $r.status, '404', 'Status code when no matching route is 404';
}
}
{
my $app = route {
get -> 'product' {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body('A GET request'.encode('ascii'));
}
post -> 'product' {
response.status = 201;
response.append-header('Content-type', 'text/html');
response.set-body('A POST request'.encode('ascii'));
}
put -> 'product' {
response.status = 204;
}
delete -> 'product' {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body('A DELETE request'.encode('ascii'));
}
patch -> 'product' {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body('A PATCH request'.encode('ascii'));
}
}
my $source = Supplier.new;
my $responses = $app.transformer($source.Supply).Channel;
$source.emit(Cro::HTTP::Request.new(:method<GET>, :target</product>));
given $responses.receive -> $r {
ok $r ~~ Cro::HTTP::Response, 'Route set routes GET';
is $r.status, 200, 'Got 200 response';
is $r.header('Content-type'), 'text/html', 'Got expected header';
is-deeply body-text($r), 'A GET request', 'Got expected body';
}
$source.emit(Cro::HTTP::Request.new(:method<POST>, :target</product>));
given $responses.receive -> $r {
ok $r ~~ Cro::HTTP::Response, 'Route set routes POST';
is $r.status, 201, 'Got 201 response';
is $r.header('Content-type'), 'text/html', 'Got expected header';
is-deeply body-text($r), 'A POST request', 'Got expected body';
}
$source.emit(Cro::HTTP::Request.new(:method<PUT>, :target</product>));
given $responses.receive -> $r {
ok $r ~~ Cro::HTTP::Response, 'Route set routes PUT';
is $r.status, 204, 'Got 204 response';
}
$source.emit(Cro::HTTP::Request.new(:method<DELETE>, :target</product>));
given $responses.receive -> $r {
ok $r ~~ Cro::HTTP::Response, 'Route set routes DELETE';
is $r.status, 200, 'Got 200 response';
is $r.header('Content-type'), 'text/html', 'Got expected header';
is-deeply body-text($r), 'A DELETE request', 'Got expected body';
}
$source.emit(Cro::HTTP::Request.new(:method<PATCH>, :target</product>));
given $responses.receive -> $r {
ok $r ~~ Cro::HTTP::Response, 'Route set routes PATCH';
is $r.status, 200, 'Got 200 response';
is $r.header('Content-type'), 'text/html', 'Got expected header';
is-deeply body-text($r), 'A PATCH request', 'Got expected body';
}
}
{
my $app = route {
get -> 'product', $uuid {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("product $uuid".encode('ascii'));
}
get -> 'category', Any $uuid {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("category $uuid".encode('ascii'));
}
get -> 'user', Str $uuid {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("user $uuid".encode('ascii'));
}
get -> 'user', 'posts' {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("user posts".encode('ascii'));
}
get -> 'product', Str $uuid, 'reviews' {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("reviews $uuid".encode('ascii'));
}
get -> 'product', Str $uuid, 'reviews', Int $page {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("reviews $uuid page $page".encode('ascii'));
}
get -> 'tree', *@path {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body(@path.join(",").encode('ascii'));
}
get -> 'orders', 'history', Int $page? {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("order history ({$page // 'no page'})".encode('ascii'));
}
get -> 'category', 'tree', $leval-a = 'none-a', $level-b = 'none-b' {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("category tree $leval-a $level-b".encode('ascii'));
}
}
my $source = Supplier.new;
my $responses = $app.transformer($source.Supply).Channel;
my @cases =
'/product/123.456', 'product 123.456',
'Mu variable at end handled correctly',
'/category/124.556', 'category 124.556',
'Any variable at end handled correctly',
'/user/128.856', 'user 128.856',
'Str variable at end handled correctly',
'/user/posts', 'user posts',
'Longest literal prefix wins',
'/product/123.456/reviews', 'reviews 123.456',
'Str variable in middle of literals handled correctly',
'/product/123.456/reviews/21', 'reviews 123.456 page 21',
'Having both Str and Int variables handled correctly',
'/product/123.456/reviews/-21', 'reviews 123.456 page -21',
'Int may have a sign',
'/tree', '',
'Slurpy handled correctly (empty case)',
'/tree/foo', 'foo',
'Slurpy handled correctly (one segment case)',
'/tree/bar/baz', 'bar,baz',
'Slurpy handled correctly (two segment case)',
'/tree/gee/wizz/fizz', 'gee,wizz,fizz',
'Slurpy handled correctly (three segment case)',
'/orders/history', 'order history (no page)',
'Optional segment handled correctly (no argument)',
'/orders/history/2', 'order history (2)',
'Optional segment handled correctly (argument)',
'/category/tree', 'category tree none-a none-b',
'Two optional segments handled correctly (none passed)',
'/category/tree/foo', 'category tree foo none-b',
'Two optional segments handled correctly (one passed)',
'/category/tree/bar/baz', 'category tree bar baz',
'Two optional segments handled correctly (two passed)';
for @cases -> $target, $expected-output, $desc {
$source.emit(Cro::HTTP::Request.new(:method<GET>, :$target));
given $responses.receive -> $r {
is-deeply body-text($r), $expected-output, $desc;
}
}
}
{
my $app = route {
get -> 'search', :$min-price is query = 0, :$max-price is query = Inf {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("search $min-price .. $max-price".encode('ascii'));
}
get -> 'headertest1', :$x-custom1 is header = 'x', :$x-custom3 is header = 'x' {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("headertest1 $x-custom1 $x-custom3".encode('ascii'));
}
get -> 'headertest2', :$x-custom2 is header = 'x', :$x-custom1 is header = 'x' {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("headertest2 $x-custom1 $x-custom2".encode('ascii'));
}
get -> 'headertest3', :$X-CUSTOM1 is header = 'x', :$X-cusTom2 is header = 'x' {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("headertest3 $X-CUSTOM1 $X-cusTom2".encode('ascii'));
}
get -> 'reqquery', :$field1! is query {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("reqquery field1".encode('ascii'));
}
get -> 'reqquery', :$field2! is query {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("reqquery field2".encode('ascii'));
}
get -> 'reqquery-list', :@params {
response.status = 200;
response.append-header('Content-type', 'text/html');
my $res = @params.map({ "$_.key()=$_.value()" }).join(',');
response.set-body("reqquery with $res".encode('ascii'));
}
get -> 'reqquery-hash', :%params {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("reqquery with %params<field1> and %params<field2>".encode('ascii'));
}
get -> 'reqheader', :$unknown! is header {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("reqheader unknown".encode('ascii'));
}
get -> 'reqheader', :$x-custom1! is header {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("reqheader x-custom1".encode('ascii'));
}
get -> 'reqheader-list', :@headers is header {
response.status = 200;
response.append-header('Content-type', 'text/html');
my @sorted-headers = @headers.sort({$^a.name cmp $^b.name});
response.set-body(@sorted-headers.map({ "$_.name() = $_.value()" }).join(",").encode('ascii'));
}
get -> 'reqheader-hash', :%headers is header {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("reqheader with a %headers<X-Custom1> and %headers<X-Custom2>".encode('ascii'));
}
get -> 'reqintarg', Int :$page! is query {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("reqintarg $page".encode('ascii'));
}
get -> 'optintarg', Int :$page is query = 1 {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("optintarg $page".encode('ascii'));
}
get -> 'optintarg_uint', UInt :$page {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("optuintarg $page".encode('ascii'));
}
}
my $source = Supplier.new;
my $responses = $app.transformer($source.Supply).Channel;
my @cases =
'/search', 'search 0 .. Inf',
'Two query string parameters, neither passed (explicit is query)',
'/search?min-price=50', 'search 50 .. Inf',
'Two query string parameters, first passed (explicit is query)',
'/search?max-price=100', 'search 0 .. 100',
'Two query string parameters, second passed (explicit is query)',
'/search?max-price=60&min-price=20', 'search 20 .. 60',
'Two query string parameters, both passed (explicit is query)',
'/headertest1', 'headertest1 c1 x',
'Two header parameters, one for a non-present header',
'/headertest2', 'headertest2 c1 c2',
'Two header parameters, both present',
'/headertest3', 'headertest3 c1 c2',
'Header parameters are case-insensitive',
'/reqquery?field1=x', 'reqquery field1',
'Required query parameter selects correct route (1)',
'/reqquery?field2=x', 'reqquery field2',
'Required query parameter selects correct route (2)',
'/reqquery?field1=x&field2=x', 'reqquery field1',
'First winning route with required query items wins',
'/reqquery-list?field1=x&field2=y', 'reqquery with field1=x,field2=y',
'Route with named array of query parameters works',
'/reqquery-hash?field1=x&field2=y', 'reqquery with x and y',
'Route with named array of headers works',
'/reqheader', 'reqheader x-custom1',
'Correct route picked when there are required headers',
'/reqheader-list', 'X-Custom1 = c1,X-Custom2 = c2',
'Route with named array of headers works',
'/reqheader-hash', 'reqheader with a c1 and c2',
'Route with named hash of headers works',
'/reqintarg?page=42', 'reqintarg 42',
'Route with required Int named arg for query parameter works',
'/optintarg?page=100', 'optintarg 100',
'Route with optional Int named arg for query parameter works when passed',
'/optintarg', 'optintarg 1',
'Route with optional Int named arg for query parameter works when not passed',
'/optintarg_uint?page=1', 'optuintarg 1',
'Route with optional UInt named arg for query parameter works when passed',
'/optintarg_uint?page=-1', '',
'Route with optional UInt named arg for query parameter doesn\'t match negative values';
for @cases -> $target, $expected-output, $desc {
my $req = Cro::HTTP::Request.new(:method<GET>, :$target);
$req.append-header('X-Custom1', 'c1');
$req.append-header('X-Custom2', 'c2');
$source.emit($req);
given $responses.receive -> $r {
is-deeply body-text($r), $expected-output, $desc;
}
}
}
{
my $app = route {
get -> 'id_int8', int8 $id {
content 'text/plain', "Correct, int8, $id";
}
get -> 'id_int8', int8 $id {
content 'text/plain', "Correct, int8, $id";
}
get -> 'id_uint8', uint8 $id {
content 'text/plain', "Correct, uint8, $id";
}
get -> 'id_int16', int16 $id {
content 'text/plain', "Correct, int16, $id";
}
get -> 'id_uint16', uint16 $id {
content 'text/plain', "Correct, uint16, $id";
}
get -> 'id_int32', int32 $id {
content 'text/plain', "Correct, int32, $id";
}
get -> 'id_uint32', uint32 $id {
content 'text/plain', "Correct, uint32, $id";
}
get -> 'id_int64', int64 $id {
content 'text/plain', "Correct, int64, $id";
}
get -> 'id_uint64', uint64 $id {
content 'text/plain', "Correct, uint64, $id";
}
}
my $source = Supplier.new;
my $responses = $app.transformer($source.Supply).Channel;
my @cases =
# int8
'/id_int8/1', 'Correct, int8, 1', 'Route for positional int8 works',
'/id_int8/-129', '', 'Lower border for positional int8 route works',
'/id_int8/128', '', 'Upper border for positional int8 route works',
# uint8
'/id_uint8/1', 'Correct, uint8, 1', 'Route for positional uint8 works',
'/id_uint8/-1', '', 'Lower border for positional uint8 route works',
'/id_uint8/256', '', 'Upper border for positional uint8 route works',
# int16
'/id_int16/1', 'Correct, int16, 1', 'Route for positional int16 works',
'/id_int16/-32769', '', 'Lower border for positional int16 route works',
'/id_int16/32768', '', 'Upper border for positional int16 route works',
# uint16
'/id_uint16/1', 'Correct, uint16, 1', 'Route for positional uint16 works',
'/id_uint16/-1', '', 'Lower border for positional uint16 route works',
'/id_uint16/65536', '', 'Upper border for positional uint16 route works',
# int32
'/id_int32/1', 'Correct, int32, 1', 'Route for positional int32 works',
'/id_int32/-2147483649', '', 'Lower border for positional int32 route works',
'/id_int32/2147483648', '', 'Upper border for positional int32 route works',
# uint32
'/id_uint32/1', 'Correct, uint32, 1', 'Route for positional uint32 works',
'/id_uint32/-1', '', 'Lower border for positional uint32 route works',
'/id_uint32/4294967296', '', 'Upper border for positional uint32 route works',
# int64
'/id_int64/1', 'Correct, int64, 1', 'Route for positional int64 works',
'/id_int64/-9223372036854775809', '', 'Lower border for positional int64 route works',
'/id_int64/9223372036854775808', '', 'Upper border for positional int64 route works',
# uint64
'/id_uint64/1', 'Correct, uint64, 1', 'Route for positional uint64 works',
'/id_uint64/-1', '', 'Lower border for positional uint64 route works',
'/id_uint64/18446744073709551616', '', 'Upper border for positional uint64 route works';
for @cases -> $target, $expected-output, $desc {
my $req = Cro::HTTP::Request.new(:method<GET>, :$target);
$source.emit($req);
given $responses.receive -> $r {
if $expected-output eq '' {
is $r.status, 404;
}
is-deeply body-text($r), $expected-output, $desc;
}
}
}
{
my $app = route {
get -> 'id_int8', int8 :$id {
content 'text/plain', "Correct, int8, $id";
}
get -> 'id_uint8', uint8 :$id {
content 'text/plain', "Correct, uint8, $id";
}
get -> 'id_int16', int16 :$id {
content 'text/plain', "Correct, int16, $id";
}
get -> 'id_uint16', uint16 :$id {
content 'text/plain', "Correct, uint16, $id";
}
get -> 'id_int32', int32 :$id {
content 'text/plain', "Correct, int32, $id";
}
get -> 'id_uint32', uint32 :$id {
content 'text/plain', "Correct, uint32, $id";
}
get -> 'id_int64', int64 :$id {
content 'text/plain', "Correct, int64, $id";
}
get -> 'id_uint64', uint64 :$id {
content 'text/plain', "Correct, uint64, $id";
}
}
my $source = Supplier.new;
my $responses = $app.transformer($source.Supply).Channel;
my @cases =
# int8
'/id_int8', 'Correct, int8, 0', 'Route with optional named int8 works',
'/id_int8?id=1', 'Correct, int8, 1', 'Route for named int8 works',
'/id_int8?id=-129', '', 'Lower border for named int8 route works',
'/id_int8?id=128', '', 'Upper border for named int8 route works',
# uint8
'/id_uint8', 'Correct, uint8, 0', 'Route with optional named uint8 works',
'/id_uint8?id=1', 'Correct, uint8, 1', 'Route for named uint8 works',
'/id_uint8?id=-1', '', 'Lower border for named uint8 route works',
'/id_uint8?id=256', '', 'Upper border for named uint8 route works',
# int16
'/id_int16', 'Correct, int16, 0', 'Route with optional named int16 works',
'/id_int16?id=1', 'Correct, int16, 1', 'Route for named int16 works',
'/id_int16?id=-32769', '', 'Lower border for named int16 route works',
'/id_int16?id=32768', '', 'Upper border for named int16 route works',
# uint16
'/id_uint16', 'Correct, uint16, 0', 'Route with optional named uint16 works',
'/id_uint16?id=1', 'Correct, uint16, 1', 'Route for named uint16 works',
'/id_uint16?id=-1', '', 'Lower border for named uint16 route works',
'/id_uint16?id=65536', '', 'Upper border for named uint16 route works',
# int32
'/id_int32', 'Correct, int32, 0', 'Route with optional named int32 works',
'/id_int32?id=1', 'Correct, int32, 1', 'Route for named int32 works',
'/id_int32?id=-2147483649', '', 'Lower border for named int32 route works',
'/id_int32?id=2147483648', '', 'Upper border for named int32 route works',
# uint32
'/id_uint32', 'Correct, uint32, 0', 'Route with optional named uint32 works',
'/id_uint32?id=1', 'Correct, uint32, 1', 'Route for named uint32 works',
'/id_uint32?id=-1', '', 'Lower border for named uint32 route works',
'/id_uint32?id=4294967296', '', 'Upper border for named uint32 route works',
# int64
'/id_int64', 'Correct, int64, 0', 'Route with optional named int64 works',
'/id_int64?id=1', 'Correct, int64, 1', 'Route for named int64 works',
'/id_int64?id=-9223372036854775809', '', 'Lower border for named int64 route works',
'/id_int64?id=9223372036854775808', '', 'Upper border for named int64 route works',
# uint64
'/id_uint64', 'Correct, uint64, 0', 'Route with optional named uint64 works',
'/id_uint64?id=1', 'Correct, uint64, 1', 'Route for named uint64 works',
'/id_uint64?id=-1', '', 'Lower border for named uint64 route works',
'/id_uint64?id=18446744073709551616', '', 'Upper border for named uint64 route works';
for @cases -> $target, $expected-output, $desc {
my $req = Cro::HTTP::Request.new(:method<GET>, :$target);
$source.emit($req);
given $responses.receive -> $r {
if $expected-output eq '' {
is $r.status, 400;
}
is-deeply body-text($r), $expected-output, $desc;
}
}
}
{
my subset UUIDv4 of Str where /^
<[0..9a..f]> ** 12
4 <[0..9a..f]> ** 3
<[89ab]> <[0..9a..f]> ** 15
$/;
my subset Percent of Int where 1..100;
my $app = route {
get -> 'product', UUIDv4 $id {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("product $id".encode('ascii'));
}
get -> 'chart', Percent $complete {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("percent $complete".encode('ascii'));
}
get -> 'tag', $tag where /^\w+$/ {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("tag $tag".encode('ascii'));
}
get -> 'advent', Int $day where * <= 24 {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("advent $day".encode('ascii'));
}
}
my $source = Supplier.new;
my $responses = $app.transformer($source.Supply).Channel;
my @good-cases =
'/product/673c748325a3411d871ccf969751f0de', 'product 673c748325a3411d871ccf969751f0de',
'Segment constrained by Str-base subset type matches when it should',
'/chart/50', 'percent 50',
'Segment constrained by Int-base subset type matches when it should',
'/tag/pizza', 'tag pizza',
'Segment constrained by where clause matches when it should',
'/advent/13', 'advent 13',
'Segment of type Int constrained by where clause matches when it should';
for @good-cases -> $target, $expected-output, $desc {
my $req = Cro::HTTP::Request.new(:method<GET>, :$target);
$source.emit($req);
given $responses.receive -> $r {
is-deeply body-text($r), $expected-output, $desc;
}
}
my @bad-cases =
'/product/not-a-uuid', 404, 'Non-matching segment gives 404 error (subset, Str)',
'/percent/1000', 404, 'Non-matching segment gives 404 error (subset, Int)',
'/tag/not-valid', 404, 'Non-matching segment gives 404 error (where, Str)',
'/advent/25', 404, 'Non-matching segment gives 404 error (where, Int)';
for @bad-cases -> $target, $expected-status, $desc {
my $req = Cro::HTTP::Request.new(:method<GET>, :$target);
$source.emit($req);
given $responses.receive -> $r {
is $r.status, $expected-status, $desc;
}
}
}
{
my subset UUIDv4 of Str where /^
<[0..9a..f]> ** 12
4 <[0..9a..f]> ** 3
<[89ab]> <[0..9a..f]> ** 15
$/;
my subset Percent of Int where 1..100;
my $app = route {
get -> 'orders', UUIDv4 :$id! is query {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("order $id".encode('ascii'));
}
my constant DEFAULT_SESSION = '6419c8383038446bb3e95d0302dd4942';
get -> 'home', UUIDv4 :$session is query = DEFAULT_SESSION {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("home $session".encode('ascii'));
}
get -> 'chart', Percent :$percent! is query {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("chart $percent".encode('ascii'));
}
get -> 'loan', Percent :$deposit is query = 15 {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("loan $deposit".encode('ascii'));
}
get -> 'tag', :$tag! is query where /^\w+$/ {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("tag $tag".encode('ascii'));
}
get -> 'advent', Int :$day! is query where * <= 24 {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body("advent $day".encode('ascii'));
}
};
my $source = Supplier.new;
my $responses = $app.transformer($source.Supply).Channel;
my @good-cases =
'/orders?id=673c748325a3411d871ccf969751f0de', 'order 673c748325a3411d871ccf969751f0de',
'Required unpack constrained by Str-base subset type works',
'/home?session=673c748325a3411d871ccf969751f0de', 'home 673c748325a3411d871ccf969751f0de',
'Optional unpack constrained by Str-base subset type works (provided)',
'/home', 'home 6419c8383038446bb3e95d0302dd4942',
'Optional unpack constrained by Str-base subset type works (not provided)',
'/chart?percent=50', 'chart 50',
'Required unpack constrained by Int-base subset type works',
'/loan?deposit=10', 'loan 10',
'Optional unpack constrained by Int-base subset type works (provided)',
'/loan', 'loan 15',
'Optional unpack constrained by Int-base subset type works (not provided)',
'/tag?tag=soup', 'tag soup',
'Required unpack untyped with where constraint works',
'/advent?day=10', 'advent 10',
'Required unpack of type Int with where constraint works';
for @good-cases -> $target, $expected-output, $desc {
my $req = Cro::HTTP::Request.new(:method<GET>, :$target);
$source.emit($req);
given $responses.receive -> $r {
is-deeply body-text($r), $expected-output, $desc;
}
}
my @bad-cases =
'/orders', 400, 'Missing unpack gives 400 error (subset, Str)',
'/orders?id=lol', 400, 'Non-matching unpack gives 400 error (subset, Str)',
'/home?session=lol', 400, 'Non-matching optional unpack gives 400 error (subset, Str)',
'/chart', 400, 'Missing unpack gives 400 error (subset, Int)',
'/chart?percent=200', 400, 'Non-matching unpack gives 400 error (subset, Int)',
'/loan?deposit=1000', 400, 'Non-matching optional unpack gives 400 error (subset, Int)',
'/tag', 400, 'Missing unpack gives 400 error (where, Str)',
'/tag?tag=abc-def', 400, 'Non-matching unpack gives 400 error (where, Str)',
'/advent', 400, 'Missing unpack gives 400 error (where, Int)',
'/tag?day=26', 400, 'Non-matching unpack gives 400 error (where, Int)';
for @bad-cases -> $target, $expected-status, $desc {
my $req = Cro::HTTP::Request.new(:method<GET>, :$target);
$source.emit($req);
given $responses.receive -> $r {
is $r.status, $expected-status, $desc;
}
}
}
{
my $app = route {
get -> {
response.status = 200;
response.append-header('Content-type', 'text/html');
response.set-body('Hello, world'.encode('ascii'));
}
}
my $source = Supplier.new;
my $responses = $app.transformer($source.Supply).Channel;
for <PUT POST DELETE> -> $method {
my $req = Cro::HTTP::Request.new(:$method, :target</>);
$source.emit($req);
given $responses.receive -> $r {
is $r.status, 405, 'URL that matches on segments but not method is 405';
}
}
}
{
my $app = route {
get -> 'blob-body' {
content 'application/octet-stream', Blob.new(103, 114, 114);
}
get -> 'str-body-no-accept-charset' {
content 'text/html', '<strong>Bears!</strong>';
}
get -> 'headers' {
header 'Link', '/honey; rel=food';
header 'Link: /den; rel=hibernate';
content 'application/json', '{ "bear": "grr" }';
}
get -> 'reminders', :$task! is query {
created '/reminders/1';
content 'text/plain', $task;
}
get -> 'shopping-list', :$product! is query {
created '/shopping-list/1', 'text/plain', $product;
}
get -> 'latin-1' {
content 'text/plain', 'wow not utf-8', :enc<ISO-8859-1>;
}
}
my $source = Supplier.new;
my $responses = $app.transformer($source.Supply).Channel;
{
my $req = Cro::HTTP::Request.new(:method<GET>, :target</blob-body>);
$source.emit($req);
given $responses.receive -> $r {
is $r.status, 200, 'Simple binary content response has 200 status';
is $r.header('Content-type'), 'application/octet-stream',
'Correct content-type set';
is body-text($r), 'grr', 'Got expected body';
}
}
{
my $req = Cro::HTTP::Request.new(:method<GET>, :target</str-body-no-accept-charset>);
$source.emit($req);
given $responses.receive -> $r {
is $r.status, 200, 'Simple text content response has 200 status';
is $r.header('Content-type'), 'text/html; charset=utf-8',
'Correct content-type set including charset';
is body-text($r), '<strong>Bears!</strong>', 'Got expected body';
}
}
{
my $req = Cro::HTTP::Request.new(:method<GET>, :target</headers>);
$source.emit($req);
given $responses.receive -> $r {
is $r.status, 200, 'Simple JSON content response has 200 status';
my @link = $r.headers.grep(*.name.lc eq 'link');
is @link.elems, 2, 'Got two Link headers';
is @link.grep(*.value eq '/honey; rel=food').elems, 1,
'Got expected link header value (1)';
is @link.grep(*.value eq '/den; rel=hibernate').elems, 1,
'Got expected link header value (2)';
is $r.header('Content-type'), 'application/json; charset=utf-8',
'Correct content-type set including charset';
is body-text($r), '{ "bear": "grr" }', 'Got expected body';
}
}
{
my $req = Cro::HTTP::Request.new(:method<GET>,
:target</reminders?task=shave>);
$source.emit($req);
given $responses.receive -> $r {
is $r.status, 201, 'created + content response has 201 status';
is $r.header('Location'), '/reminders/1', 'Location header is set';
is $r.header('Content-type'), 'text/plain; charset=utf-8',
'Correct content-type set including charset';
is body-text($r), 'shave', 'Got expected body';
}
}
{
my $req = Cro::HTTP::Request.new(:method<GET>,
:target</shopping-list?product=beef>);
$source.emit($req);
given $responses.receive -> $r {
is $r.status, 201, 'created response has 201 status';
is $r.header('Location'), '/shopping-list/1', 'Location header is set';
is $r.header('Content-type'), 'text/plain; charset=utf-8',
'Correct content-type set including charset';
is body-text($r), 'beef', 'Got expected body';
}
}
{
my $req = Cro::HTTP::Request.new(:method<GET>, :target</latin-1>);
$source.emit($req);
given $responses.receive -> $r {
is $r.status, 200, 'Str content with :enc<ISO-8859-1> has 200 response';
is $r.header('Content-type'), 'text/plain; charset=ISO-8859-1',
'Correct content-type with charset=ISO-8859-1';
is body-text($r), 'wow not utf-8', 'Got expected body';
}
}
}
{
my $app = route {
get -> 'not-found-1', $id {
if $id == 1 {
content 'text/plain', 'found ok';
}
else {
not-found;
content 'text/plain', '404 not found';
}
}
get -> 'not-found-2', $id {
if $id == 1 {
content 'text/plain', 'found ok!';
}
else {
not-found 'text/plain', '404 not found!';
}
}
get -> 'bad-request-1', $id {
if $id == 1 {
content 'text/plain', 'request ok';
}
else {
bad-request;
content 'text/plain', '400 bad request';
}
}
get -> 'bad-request-2', $id {
if $id == 1 {
content 'text/plain', 'request ok!';
}
else {
bad-request 'text/plain', '400 bad request!';
}
}
get -> 'forbidden-1', $id {
if $id == 1 {
content 'text/plain', 'request allowed';
}
else {
forbidden;
content 'text/plain', '403 forbidden';
}
}
get -> 'forbidden-2', $id {
if $id == 1 {
content 'text/plain', 'request allowed!';
}
else {
forbidden 'text/plain', '403 forbidden!';
}
}
get -> 'conflict-1', $id {
if $id == 1 {
content 'text/plain', 'request timely';
}
else {
conflict;
content 'text/plain', '409 conflict';
}
}
get -> 'conflict-2', $id {
if $id == 1 {
content 'text/plain', 'request timely!';
}
else {
conflict 'text/plain', '409 conflict!';
}
}
}
my $source = Supplier.new;
my $responses = $app.transformer($source.Supply).Channel;
my @cases =
'/not-found-1/1', 200, 'found ok', 'not found sanity (1)',
'/not-found-1/6', 404, '404 not found', 'not found (1)',
'/not-found-2/1', 200, 'found ok!', 'not found sanity (2)',
'/not-found-2/6', 404, '404 not found!', 'not found (2)',
'/bad-request-1/1', 200, 'request ok', 'bad request sanity (1)',
'/bad-request-1/6', 400, '400 bad request', 'bad request (1)',
'/bad-request-2/1', 200, 'request ok!', 'bad request sanity (2)',
'/bad-request-2/6', 400, '400 bad request!', 'bad request (2)',
'/forbidden-1/1', 200, 'request allowed', 'forbidden sanity (1)',
'/forbidden-1/6', 403, '403 forbidden', 'forbidden (1)',
'/forbidden-2/1', 200, 'request allowed!', 'forbidden sanity (2)',
'/forbidden-2/6', 403, '403 forbidden!', 'forbidden (2)',
'/conflict-1/1', 200, 'request timely', 'conflict sanity (1)',
'/conflict-1/6', 409, '409 conflict', 'conflict (1)',
'/conflict-2/1', 200, 'request timely!', 'conflict sanity (2)',
'/conflict-2/6', 409, '409 conflict!', 'conflict (2)';
for @cases -> $target, $status, $body, $desc {
my $req = Cro::HTTP::Request.new(:method<GET>, :$target);
$source.emit($req);
given $responses.receive -> $r {
is $r.status, $status, "Error routine $desc - status";
is $r.header('Content-type'), 'text/plain; charset=utf-8',
"Error routine $desc - content type";
is body-text($r), $body, "Error routine $desc - body";
}
}
}
{
my $app = route {
get -> 'redir', 'temp-1' {
redirect '/to/temp-1';
content 'text/html', '<a href="/to/temp-1">Click here</a>';
}
get -> 'redir', 'temp-2' {
redirect '/to/temp-2', 'text/html',
'<a href="/to/temp-2">Click here</a>'
}
get -> 'redir', 'temp-3' {
redirect :temporary, '/to/temp-3';
content 'text/html', '<a href="/to/temp-3">Click here</a>';
}
get -> 'redir', 'temp-4' {
redirect :temporary, '/to/temp-4', 'text/html',
'<a href="/to/temp-4">Click here</a>'
}
get -> 'redir', 'perm-1' {
redirect :permanent, '/to/perm-1';
content 'text/html', '<a href="/to/perm-1">Click here</a>';
}
get -> 'redir', 'perm-2' {
redirect :permanent, '/to/perm-2', 'text/html',
'<a href="/to/perm-2">Click here</a>'
}
get -> 'redir', 'see-other-1' {
redirect :see-other, '/to/see-other-1';
content 'text/html', '<a href="/to/see-other-1">Click here</a>';
}
get -> 'redir', 'see-other-2' {