@@ -384,4 +384,221 @@ void main() {
384
384
await harness.fromServer.done;
385
385
});
386
386
});
387
+
388
+ group ('Server with server interceptor' , () {
389
+ group ('processes calls if interceptor allows request' , () {
390
+ const expectedRequest = 5 ;
391
+ const expectedResponse = 7 ;
392
+ Future <int > methodHandler (ServiceCall call, Future <int > request) async {
393
+ expect (await request, expectedRequest);
394
+ return expectedResponse;
395
+ }
396
+
397
+ Null interceptor (call, method, requests) {
398
+ if (method.name == 'Unary' ) {
399
+ return null ;
400
+ }
401
+ throw GrpcError .unauthenticated ('Request is unauthenticated' );
402
+ }
403
+
404
+ Future <void > doTest (TestServerInterceptorOnStart ? handler) async {
405
+ harness
406
+ ..serverInterceptor.onStart = handler
407
+ ..service.unaryHandler = methodHandler
408
+ ..runTest ('/Test/Unary' , [expectedRequest], [expectedResponse]);
409
+
410
+ await harness.fromServer.done;
411
+ }
412
+
413
+ test ('with sync interceptor' , () => doTest (interceptor));
414
+ test (
415
+ 'with async interceptor' ,
416
+ () => doTest ((call, method, requests) async =>
417
+ interceptor (call, method, requests)));
418
+ });
419
+
420
+ group ('returns error if interceptor blocks request' , () {
421
+ Null interceptor (call, method, requests) {
422
+ if (method.name == 'Unary' ) {
423
+ throw GrpcError .unauthenticated ('Request is unauthenticated' );
424
+ }
425
+ return null ;
426
+ }
427
+
428
+ Future <void > doTest (TestServerInterceptorOnStart handler) async {
429
+ harness
430
+ ..serverInterceptor.onStart = handler
431
+ ..expectErrorResponse (
432
+ StatusCode .unauthenticated, 'Request is unauthenticated' )
433
+ ..sendRequestHeader ('/Test/Unary' );
434
+
435
+ await harness.fromServer.done;
436
+ }
437
+
438
+ test ('with sync interceptor' , () => doTest (interceptor));
439
+ test (
440
+ 'with async interceptor' ,
441
+ () => doTest ((call, method, request) async =>
442
+ interceptor (call, method, request)));
443
+ });
444
+
445
+ test ("don't fail if interceptor await 2 times" , () async {
446
+ Future <Null > interceptor (call, method, requests) async {
447
+ await Future .value ();
448
+ await Future .value ();
449
+ throw GrpcError .internal ('Reason is unknown' );
450
+ }
451
+
452
+ harness
453
+ ..serverInterceptor.onStart = interceptor
454
+ ..expectErrorResponse (StatusCode .internal, 'Reason is unknown' )
455
+ ..sendRequestHeader ('/Test/Unary' )
456
+ ..sendData (1 );
457
+
458
+ await harness.fromServer.done;
459
+ });
460
+
461
+ group ('serviceInterceptors are invoked' , () {
462
+ const expectedRequest = 5 ;
463
+ const expectedResponse = 7 ;
464
+ Future <int > methodHandler (ServiceCall call, Future <int > request) async {
465
+ expect (await request, expectedRequest);
466
+ return expectedResponse;
467
+ }
468
+
469
+ Future <void > doTest (List <TestServerInterceptor > interceptors) async {
470
+ harness
471
+ // ↓ mutation: Server is already built
472
+ ..serverInterceptors.addAll (interceptors)
473
+ ..service.unaryHandler = methodHandler
474
+ ..runTest ('/Test/Unary' , [expectedRequest], [expectedResponse]);
475
+
476
+ await harness.fromServer.done;
477
+ }
478
+
479
+ test ('single serviceInterceptor is invoked' , () async {
480
+ final invocationsOrderRecords = [];
481
+
482
+ await doTest ([
483
+ TestServerInterceptor (
484
+ onStart: (call, method, requests) {
485
+ invocationsOrderRecords.add ('Start' );
486
+ },
487
+ onData: (call, method, requests, data) {
488
+ invocationsOrderRecords.add ('Data [$data ]' );
489
+ },
490
+ onFinish: (call, method, requests) {
491
+ invocationsOrderRecords.add ('Done' );
492
+ },
493
+ )
494
+ ]);
495
+
496
+ expect (invocationsOrderRecords, equals (['Start' , 'Data [7]' , 'Done' ]));
497
+ });
498
+
499
+ test ('multiple serviceInterceptors are invoked' , () async {
500
+ final invocationsOrderRecords = [];
501
+
502
+ await doTest ([
503
+ TestServerInterceptor (
504
+ onStart: (call, method, requests) {
505
+ invocationsOrderRecords.add ('Start 1' );
506
+ },
507
+ onData: (call, method, requests, data) {
508
+ invocationsOrderRecords.add ('Data 1 [$data ]' );
509
+ },
510
+ onFinish: (call, method, requests) {
511
+ invocationsOrderRecords.add ('Done 1' );
512
+ },
513
+ ),
514
+ TestServerInterceptor (
515
+ onStart: (call, method, requests) {
516
+ invocationsOrderRecords.add ('Start 2' );
517
+ },
518
+ onData: (call, method, requests, data) {
519
+ invocationsOrderRecords.add ('Data 2 [$data ]' );
520
+ },
521
+ onFinish: (call, method, requests) {
522
+ invocationsOrderRecords.add ('Done 2' );
523
+ },
524
+ )
525
+ ]);
526
+
527
+ expect (
528
+ invocationsOrderRecords,
529
+ equals ([
530
+ 'Start 1' ,
531
+ 'Start 2' ,
532
+ 'Data 2 [7]' ,
533
+ 'Data 1 [7]' ,
534
+ 'Done 2' ,
535
+ 'Done 1' ,
536
+ ]));
537
+ });
538
+ });
539
+
540
+ test ('can modify response' , () async {
541
+ const expectedRequest = 5 ;
542
+ const baseResponse = 7 ;
543
+ const expectedResponse = 14 ;
544
+
545
+ final invocationsOrderRecords = [];
546
+
547
+ final interceptors = [
548
+ TestServerInterceptor (
549
+ onStart: (call, method, requests) {
550
+ invocationsOrderRecords.add ('Start 1' );
551
+ },
552
+ onData: (call, method, requests, data) {
553
+ invocationsOrderRecords.add ('Data 1 [$data ]' );
554
+ },
555
+ onFinish: (call, method, requests) {
556
+ invocationsOrderRecords.add ('Done 1' );
557
+ },
558
+ ),
559
+ TestServerInterruptingInterceptor (transform: < R > (value) {
560
+ if (value is int ) {
561
+ return value * 2 as R ;
562
+ }
563
+
564
+ return value;
565
+ }),
566
+ TestServerInterceptor (
567
+ onStart: (call, method, requests) {
568
+ invocationsOrderRecords.add ('Start 2' );
569
+ },
570
+ onData: (call, method, requests, data) {
571
+ invocationsOrderRecords.add ('Data 2 [$data ]' );
572
+ },
573
+ onFinish: (call, method, requests) {
574
+ invocationsOrderRecords.add ('Done 2' );
575
+ },
576
+ )
577
+ ];
578
+
579
+ Future <int > methodHandler (ServiceCall call, Future <int > request) async {
580
+ expect (await request, expectedRequest);
581
+ return baseResponse;
582
+ }
583
+
584
+ harness
585
+ // ↓ mutation: Server is already built
586
+ ..serverInterceptors.addAll (interceptors)
587
+ ..service.unaryHandler = methodHandler
588
+ ..runTest ('/Test/Unary' , [expectedRequest], [expectedResponse]);
589
+
590
+ await harness.fromServer.done;
591
+
592
+ expect (
593
+ invocationsOrderRecords,
594
+ equals ([
595
+ 'Start 1' ,
596
+ 'Start 2' ,
597
+ 'Data 2 [7]' ,
598
+ 'Data 1 [14]' ,
599
+ 'Done 2' ,
600
+ 'Done 1' ,
601
+ ]));
602
+ });
603
+ });
387
604
}
0 commit comments