-
Notifications
You must be signed in to change notification settings - Fork 3
/
http-middleware.t
877 lines (748 loc) · 29.5 KB
/
http-middleware.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
use Cro::TCP;
use Cro::HTTP::Client;
use Cro::HTTP::Middleware;
use Cro::HTTP::Router;
use Cro::HTTP::Server;
use Cro::Service;
use Cro::Transform;
use Test;
constant TEST_PORT = 31315;
my $url = "http://localhost:{TEST_PORT}";
# Application
my $application = route {
get -> {
content 'text/html', "<strong>Hello from Cro!</strong>";
}
get -> 'echo' {
request-body -> $body {
content 'text/plain', "$body";
}
}
get -> 'index.shtml' {
content 'text/html', "Correct Answer";
}
get -> 'index.SHTML' {
content 'text/html', "Incorrect Answer";
}
my atomicint $i = 0;
get -> 'counter' {
content 'text/plain', (++⚛$i).Str;
}
};
subtest {
# Request middleware written as a transform
my class LowerCase does Cro::Transform {
method consumes() { Cro::HTTP::Request }
method produces() { Cro::HTTP::Request }
method transformer(Supply $pipeline --> Supply) {
supply {
whenever $pipeline -> $request {
$request.target = $request.target.lc;
emit $request;
}
}
}
}
# Response middleware written as a transform
my class StrictTransportSecurity does Cro::Transform {
has Duration:D $.max-age is required;
method consumes() { Cro::HTTP::Response }
method produces() { Cro::HTTP::Response }
method transformer(Supply $pipeline --> Supply) {
supply {
whenever $pipeline -> $response {
$response.append-header:
'Strict-Transport-Security',
"max-age=$!max-age";
emit $response;
}
}
}
}
{
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $application,
after => StrictTransportSecurity.new(max-age => Duration.new(60))
);
$service.start;
given await Cro::HTTP::Client.get("$url") -> $resp {
is $resp.header('Strict-Transport-Security'), "max-age=60", 'Header was set';
}
LEAVE $service.stop();
}
{
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $application,
before => LowerCase
);
$service.start;
given await Cro::HTTP::Client.get("$url/index.SHTML") -> $resp {
is await($resp.body-text), 'Correct Answer', 'Target was processed';
}
LEAVE $service.stop();
}
{
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $application,
before => LowerCase,
after => StrictTransportSecurity.new(max-age => Duration.new(60))
);
$service.start;
given await Cro::HTTP::Client.get("$url/index.SHTML") -> $resp {
is $resp.header('Strict-Transport-Security'), "max-age=60", 'after works with before';
is await($resp.body-text), 'Correct Answer', 'before works with after';
}
LEAVE $service.stop();
}
}, 'Request and response middleware written using a transform';
subtest {
# Request middleware written using Cro::HTTP::Middleware::Request
my class LowerCase does Cro::HTTP::Middleware::Request {
method process(Supply $requests --> Supply) {
supply whenever $requests -> $request {
$request.target = $request.target.lc;
emit $request;
}
}
}
# Response middleware written using Cro::HTTP::Middleware::Response
my class StrictTransportSecurity does Cro::HTTP::Middleware::Response {
has Duration:D $.max-age is required;
method process(Supply $responses --> Supply) {
supply whenever $responses -> $response {
$response.append-header:
'Strict-Transport-Security',
"max-age=$!max-age";
emit $response;
}
}
}
{
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $application,
after => StrictTransportSecurity.new(max-age => Duration.new(60))
);
$service.start;
given await Cro::HTTP::Client.get("$url") -> $resp {
is $resp.header('Strict-Transport-Security'), "max-age=60", 'Header was set';
}
LEAVE $service.stop();
}
{
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $application,
before => LowerCase
);
$service.start;
given await Cro::HTTP::Client.get("$url/index.SHTML") -> $resp {
is await($resp.body-text), 'Correct Answer', 'Target was processed';
}
LEAVE $service.stop();
}
{
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $application,
before => LowerCase,
after => StrictTransportSecurity.new(max-age => Duration.new(60))
);
$service.start;
given await Cro::HTTP::Client.get("$url/index.SHTML") -> $resp {
is $resp.header('Strict-Transport-Security'), "max-age=60", 'after works with before';
is await($resp.body-text), 'Correct Answer', 'before works with after';
}
LEAVE $service.stop();
}
{
my $mw-app = route {
before-matched LowerCase;
after-matched StrictTransportSecurity.new(max-age => Duration.new(60));
delegate <*> => $application;
}
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $mw-app
);
$service.start;
LEAVE $service.stop();
given await Cro::HTTP::Client.get("$url/index.SHTML") -> $resp {
is await($resp.body-text), 'Correct Answer',
'Request middleware works with before-matched in route block';
is $resp.header('Strict-Transport-Security'), "max-age=60",
'Response middleware works with after-matched in route block';
}
}
}, 'Request and response middleware written using a Cro::HTTP::Middleware roles';
subtest {
my class ForbiddenWithoutAuthHeader does Cro::HTTP::Middleware::Conditional {
method process(Supply $requests) {
supply whenever $requests -> $request {
emit $request.has-header('Authorization')
?? $request
!! Cro::HTTP::Response.new(:$request, :403status);
}
}
}
{
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $application,
before => ForbiddenWithoutAuthHeader
);
$service.start;
LEAVE $service.stop();
throws-like { await Cro::HTTP::Client.get("$url") },
X::Cro::HTTP::Error::Client,
response => { .status == 403 },
'Got 403 response from middleware when no auth header';
my %headers = Authorization => 'Bearer Polarer';
given await Cro::HTTP::Client.get("$url", :%headers) -> $resp {
is $resp.status, 200, 'Got 200 normal response with an auth header';
}
}
{
my $mw-app = route {
before-matched ForbiddenWithoutAuthHeader;
delegate <*> => $application;
}
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $mw-app
);
$service.start;
LEAVE $service.stop();
throws-like { await Cro::HTTP::Client.get("$url") },
X::Cro::HTTP::Error::Client,
response => { .status == 403 },
'Got 403 response from middleware when no auth header (before-matched in router)';
my %headers = Authorization => 'Bearer Polarer';
given await Cro::HTTP::Client.get("$url", :%headers) -> $resp {
is $resp.status, 200, 'Got 200 normal response with an auth header (before-matched in router)';
}
}
{
my $mw-app = route {
before-matched ForbiddenWithoutAuthHeader;
include $application;
}
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $mw-app
);
$service.start;
LEAVE $service.stop();
throws-like { await Cro::HTTP::Client.get("$url") },
X::Cro::HTTP::Error::Client,
response => { .status == 403 },
'Got 403 response from middleware when no auth header (before-matched + include in router)';
my %headers = Authorization => 'Bearer Polarer';
given await Cro::HTTP::Client.get("$url", :%headers) -> $resp {
is $resp.status, 200,
'Got 200 normal response with an auth header (before-matched + include in router)';
}
}
}, 'Conditional response middleware using Cro::HTTP::Middleware::Conditional';
subtest {
my class OverlySimpleCache does Cro::HTTP::Middleware::RequestResponse {
has $!cached-blob;
method process-requests(Supply $requests) {
supply whenever $requests -> $request {
if $!cached-blob {
given Cro::HTTP::Response.new(:$request, :200status) {
.set-body-byte-stream: supply emit $!cached-blob;
.emit;
}
}
else {
emit $request;
}
}
}
method process-responses(Supply $responses) {
supply whenever $responses -> $response {
whenever $response.body-blob -> $!cached-blob {
$response.set-body-byte-stream: supply emit $!cached-blob;
$response.append-header: 'X-Uncached', 'true';
emit $response;
}
}
}
}
{
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $application,
before => OverlySimpleCache.new
);
$service.start;
LEAVE $service.stop();
given await Cro::HTTP::Client.get("$url/counter") -> $resp {
is $resp.status, 200, 'Got 200 response on first request';
ok $resp.has-header('X-Uncached'), 'Response part added header';
is await($resp.body-text), '1', 'Expected body';
}
given await Cro::HTTP::Client.get("$url/counter") -> $resp {
is $resp.status, 200, 'Got 200 response on second request';
nok $resp.has-header('X-Uncached'), 'Response part did not run on early response';
is await($resp.body-text), '1', 'Got cached body';
}
}
{
my $mw-app = route {
before-matched OverlySimpleCache.new;
delegate <*> => $application;
}
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $mw-app
);
$service.start;
LEAVE $service.stop();
given await Cro::HTTP::Client.get("$url/counter") -> $resp {
is $resp.status, 200, 'Got 200 response on first request (before-matched in router)';
ok $resp.has-header('X-Uncached'), 'Response part added header (before-matched in router)';
is await($resp.body-text), '2', 'Expected body (before-matched in router)';
}
given await Cro::HTTP::Client.get("$url/counter") -> $resp {
is $resp.status, 200, 'Got 200 response on second request (before-matched in router)';
nok $resp.has-header('X-Uncached'),
'Response part did not run on early response (before-matched in router)';
is await($resp.body-text), '2', 'Got cached body (before-matched in router)';
}
}
{
my $mw-app = route {
before-matched OverlySimpleCache.new;
include $application;
}
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $mw-app
);
$service.start;
LEAVE $service.stop();
given await Cro::HTTP::Client.get("$url/counter") -> $resp {
is $resp.status, 200,
'Got 200 response on first request (before-matched + include in router)';
ok $resp.has-header('X-Uncached'),
'Response part added header (before-matched + include in router)';
is await($resp.body-text), '3', 'Expected body (before-matched + include in router)';
}
given await Cro::HTTP::Client.get("$url/counter") -> $resp {
is $resp.status, 200,
'Got 200 response on second request (before-matched + include in router)';
nok $resp.has-header('X-Uncached'),
'Response part did not run on early response (before-matched + include in router)';
is await($resp.body-text), '3',
'Got cached body (before-matched + include in router)';
}
}
}, 'Request/response middleware using Cro::HTTP::Middleware::RequestResponse';
subtest {
my class UpperCase does Cro::Transform {
method consumes() { Cro::TCP::Message }
method produces() { Cro::TCP::Message }
method transformer(Supply $pipeline --> Supply) {
supply {
whenever $pipeline -> $message {
emit Cro::TCP::Message.new(data =>
$message.data.decode.subst('hello', 'HELLO').encode('latin-1'));
}
}
}
}
{
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $application,
before-parse => UpperCase);
$service.start;
given await Cro::HTTP::Client.get("$url/echo",
content-type => 'text/plain',
body => "hello WORLD") -> $resp {
is await($resp.body-text), 'HELLO WORLD', 'before-parse works';
}
LEAVE $service.stop();
}
{
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $application,
after-serialize => UpperCase);
$service.start;
given await Cro::HTTP::Client.get("$url/echo",
content-type => 'text/plain',
body => "hello WORLD") -> $resp {
is await($resp.body-text), 'HELLO WORLD', 'after-serialize works';
}
LEAVE $service.stop();
}
}, 'Byte-level middleware, before/after request is parsed';
subtest {
my class PreHeaderMiddleware does Cro::Transform {
has $.value;
method consumes() { Cro::HTTP::Request }
method produces() { Cro::HTTP::Request }
method transformer(Supply $pipeline --> Supply) {
supply {
whenever $pipeline {
.append-header('Custom-header', $!value);
.emit;
}
}
}
}
my class PostHeaderMiddleware does Cro::Transform {
has $.value;
method consumes() { Cro::HTTP::Response }
method produces() { Cro::HTTP::Response }
method transformer(Supply $pipeline --> Supply) {
supply {
whenever $pipeline {
.append-header('Post-Custom', $!value);
.emit;
}
}
}
}
my class TestDelegate does Cro::Transform {
method consumes() { Cro::HTTP::Request }
method produces() { Cro::HTTP::Response }
method transformer(Supply $in --> Supply) {
supply whenever $in -> $request {
my $resp = Cro::HTTP::Response.new(:$request, :200status);
if $request.has-header('Custom-header') {
$resp.set-body("Correct Answer");
} else {
$resp.set-body("Incorrect Answer")
}
emit $resp;
}
}
}
my $inner-redef = route {
before-matched PreHeaderMiddleware.new(:value<Rock>);
after-matched PostHeaderMiddleware.new(:value<Roll>);
get -> 'home-redef' {
if request.header('Custom-header') eq 'foo,Rock' {
content 'text/html', "Correct Answer";
} else {
content 'text/html', "Incorrect Answer";
}
}
}
my $inner = route {
get -> 'home' {
if request.header('Custom-header') eq 'foo' {
content 'text/html', "Correct Answer";
} else {
content 'text/html', "Incorrect Answer";
}
}
}
# Application with built-in middleware
my $app = route {
before-matched PreHeaderMiddleware.new(:value<foo>);
after-matched PostHeaderMiddleware.new(:value<bar>);
get -> {
if request.header('Custom-header') eq 'foo' {
content 'text/html', "Correct Answer";
} else {
content 'text/html', "Incorrect Answer";
}
}
delegate d => TestDelegate.new;
include $inner;
include $inner-redef;
};
{
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $app
);
$service.start;
given await Cro::HTTP::Client.get("$url/") -> $resp {
is $resp.header('Post-Custom'), 'bar', 'per-route after-matched middleware for regular request works';
is await($resp.body-text), 'Correct Answer', 'per-route before-matched middleware for regular request works';
}
given await Cro::HTTP::Client.get("$url/d") -> $resp {
is $resp.header('Post-Custom'), 'bar', 'per-route after-matched middleware for delegated request works';
is await($resp.body-text), 'Correct Answer', 'per-route before-matched middleware for delegated request works';
}
given await Cro::HTTP::Client.get("$url/home") -> $resp {
is $resp.header('Post-Custom'), 'bar', 'per-route after-matched middleware for includee works';
is await($resp.body-text), 'Correct Answer', 'per-route before-matched middleware for includee works';
}
given await Cro::HTTP::Client.get("$url/home-redef") -> $resp {
is $resp.header('Post-Custom'), 'Roll,bar', 'per-route after-matched middleware for includee works';
is await($resp.body-text), 'Correct Answer', 'per-route before-matched middleware for includee works';
}
LEAVE $service.stop();
}
my $block-app = route {
after-matched {
header 'Strict-transport-security', 'max-age=31536000; includeSubDomains';
}
before-matched {
.append-header('Custom-header', 'Foo');
}
get -> {
if request.header('Custom-header') eq 'Foo' {
content 'text/html', 'Correct Answer';
} else {
content 'text/html', 'Incorrect answer';
}
}
}
{
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $block-app
);
$service.start;
given await Cro::HTTP::Client.get("$url/") -> $resp {
is await($resp.body-text), 'Correct Answer', 'per-route block before-matched middleware works';
is $resp.header('Strict-transport-security'), 'max-age=31536000; includeSubDomains',
'per-route block after-matched middleware works';
}
LEAVE $service.stop();
}
dies-ok {
my $block-app = route {
after-matched PreHeaderMiddleware.new(:value<foo>);
get -> {
content 'text/html', 'Dies';
}
}
}, 'Cannot use wrong typed Transformer as a middleware';
}, 'Interaction of middleware written as Cro::Transform with HTTP router';
subtest {
my $mw-app = route {
before-matched {
forbidden unless .has-header('Authorization');
}
get -> {
content 'text/html', "<strong>Hello from Cro!</strong>";
}
}
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $mw-app
);
$service.start;
LEAVE $service.stop();
throws-like { await Cro::HTTP::Client.get("$url") },
X::Cro::HTTP::Error::Client,
response => { .status == 403 },
'Block form of before-matched in router can produce an early response';
my %headers = Authorization => 'Bearer Polarer';
given await Cro::HTTP::Client.get("$url", :%headers) -> $resp {
is $resp.status, 200,
'Block form of before-matched not producing a response also works';
}
}, 'Conditional response in block form of before-matched in router';
{
my class LowerCase does Cro::HTTP::Middleware::Request {
method process(Supply $requests --> Supply) {
supply whenever $requests -> $request {
$request.target = $request.target.lc;
emit $request;
}
}
}
my class StrictTransportSecurity does Cro::HTTP::Middleware::Response {
has Duration:D $.max-age is required;
method process(Supply $responses --> Supply) {
supply whenever $responses -> $response {
$response.append-header:
'Strict-Transport-Security',
"max-age=$!max-age";
emit $response;
}
}
}
my $mw-app = route {
delegate <*> => $application;
after-matched StrictTransportSecurity.new(max-age => Duration.new(60));
before-matched LowerCase;
}
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $mw-app
);
$service.start;
LEAVE $service.stop();
given await Cro::HTTP::Client.get("$url/index.SHTML") -> $resp {
is await($resp.body-text), 'Correct Answer',
'before-matched applies even to delegate done before it';
is $resp.header('Strict-Transport-Security'), "max-age=60",
'after-matched applies even to delegate done after it';
}
}
{
my $block-app = route {
get -> {
if request.header('Custom-header') eq 'Foo' {
content 'text/html', 'Correct Answer';
} else {
content 'text/html', 'Incorrect answer';
}
}
after-matched {
header 'Strict-transport-security', 'max-age=31536000; includeSubDomains';
}
before-matched {
.append-header('Custom-header', 'Foo');
}
}
{
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $block-app
);
$service.start;
given await Cro::HTTP::Client.get("$url/") -> $resp {
is await($resp.body-text), 'Correct Answer', 'before-matched applies even to a route before it';
is $resp.header('Strict-transport-security'), 'max-age=31536000; includeSubDomains',
'after-matched middleware applies even to a route before it';
}
LEAVE $service.stop();
}
}
{
my $before-p = Promise.new;
my $before-m-p = Promise.new;
my $after-p = Promise.new;
my $after-m-p = Promise.new;
my $app = route {
before-matched { $before-m-p.keep }
before { $before-p.keep }
after { $after-p.keep }
after-matched { $after-m-p.keep }
}
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $app
);
$service.start;
LEAVE $service.stop();
dies-ok { await Cro::HTTP::Client.get("$url") }, 'Dies when no matched rule';
ok $before-p.status ~~ Kept, 'before block was executed';
ok $before-m-p.status !~~ Kept, 'before-matched block was not executed';
ok $after-p.status ~~ Kept, 'after block was executed';
ok $after-m-p.status !~~ Kept, 'after-matched block was not executed';
}
{
{
my $app = route {
before { $_.target = $_.target.lc }
after { $_.append-header('Strict-Transport-Security', "max-age=60") }
get -> 'home' { content 'text/html', "Home" }
}
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $app
);
$service.start;
LEAVE $service.stop();
given await Cro::HTTP::Client.get("$url/HOME") -> $resp {
is $resp.header('Strict-transport-security'), 'max-age=60', "before and after is run from block definition";
}
}
{
my class LowerCase does Cro::HTTP::Middleware::Request {
method process(Supply $requests --> Supply) {
supply whenever $requests -> $request {
$request.target = $request.target.lc;
emit $request;
}
}
}
my class StrictTransportSecurity does Cro::HTTP::Middleware::Response {
has Duration:D $.max-age is required;
method process(Supply $responses --> Supply) {
supply whenever $responses -> $response {
$response.append-header('Strict-Transport-Security', "max-age=$!max-age");
emit $response;
}
}
}
my $app = route {
before LowerCase.new;
after StrictTransportSecurity.new(max-age => Duration.new(12341234));
get -> 'home' { content 'text/html', "Home" }
}
my Cro::Service $service = Cro::HTTP::Server.new(
:host('localhost'), :port(TEST_PORT), application => $app
);
$service.start;
LEAVE $service.stop();
given await Cro::HTTP::Client.get("$url/HOME") -> $resp {
is $resp.header('Strict-transport-security'), 'max-age=12341234', "before and after is run from class definition";
}
}
{
my class Middle does Cro::HTTP::Middleware::RequestResponse {
method process-requests(Supply $requests) {
supply whenever $requests -> $r {
$r.target = $r.target.lc;
emit $r;
}
}
method process-responses(Supply $responses) {
supply whenever $responses -> $r {
$r.append-header('Strict-Transport-Security', "max-age=6420");
emit $r;
}
}
}
my $app = route {
before Middle.new;
get -> 'home' { content 'text/html', "Home" }
}
my Cro::Service $service = Cro::HTTP::Server.new(:host('localhost'), :port(TEST_PORT), application => $app);
$service.start;
LEAVE $service.stop();
given await Cro::HTTP::Client.get("$url/HOME") -> $resp {
is $resp.header('Strict-transport-security'), 'max-age=6420', "before and after is run from RequestResponse(Pair) definition";
}
}
}
{
my class MySession does Cro::HTTP::Auth {
has $.is-logged-in;
has $.is-admin;
}
subset LoggedIn of MySession where *.is-logged-in;
subset Admin of MySession where *.is-admin;
my class Fake::Auth::Middleware does Cro::HTTP::Middleware::Request {
has $.second is rw = False;
method process(Supply $requests --> Supply) {
supply whenever $requests -> $request {
$request.auth = $!second ?? MySession.new(:is-admin) !! MySession.new(:is-logged-in);
$!second = True;
emit $request;
}
}
}
my $middle = Fake::Auth::Middleware.new;
my $app = route {
before $middle;
after { redirect '/401' if .status == 401 }
get -> LoggedIn $user, 'foo' {
content 'text/plain', 'user';
}
get -> Admin $user, 'foo' {
content 'text/plain', 'admin';
}
get -> Admin $user, 'private' {
content 'text/plain', 'Top secret info';
}
get -> '401' {
content 'text/plain', 'Too bad';
}
}
my Cro::Service $service = Cro::HTTP::Server.new(:host('localhost'), :port(TEST_PORT), application => $app);
$service.start;
LEAVE $service.stop();
given await Cro::HTTP::Client.get("$url/foo") -> $resp {
is (await $resp.body-text), 'user', 'Auth middleware is applied';
}
given await Cro::HTTP::Client.get("$url/foo") -> $resp {
is (await $resp.body-text), 'admin', 'Auth middleware is applied 2';
}
$middle.second = False;
given await Cro::HTTP::Client.get("$url/private") -> $resp {
is (await $resp.body-text), 'Too bad', 'After middleware is applied';
}
}
{
my $inner = route { before { $_.target = $_.target.lc } }
throws-like { route { include $inner } },
X::AdHoc, message => /'delegate'/, 'Better exception message when user tries to include route with before/after';
lives-ok { route { delegate <*> => $inner } }, 'delegate does not cause an exception';
}
done-testing;