|
| 1 | +// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:http/http.dart'; |
| 6 | +import 'package:test/test.dart'; |
| 7 | +// \TODO REMOVE |
| 8 | +import 'package:http/src/pipeline.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + test('compose middleware with Pipeline', () async { |
| 12 | + int accessLocation = 0; |
| 13 | + |
| 14 | + var middlewareA = createMiddleware(requestHandler: (request) async { |
| 15 | + expect(accessLocation, 0); |
| 16 | + accessLocation = 1; |
| 17 | + return request; |
| 18 | + }, responseHandler: (response) async { |
| 19 | + expect(accessLocation, 4); |
| 20 | + accessLocation = 5; |
| 21 | + return response; |
| 22 | + }); |
| 23 | + |
| 24 | + var middlewareB = createMiddleware(requestHandler: (request) async { |
| 25 | + expect(accessLocation, 1); |
| 26 | + accessLocation = 2; |
| 27 | + return request; |
| 28 | + }, responseHandler: (response) async { |
| 29 | + expect(accessLocation, 3); |
| 30 | + accessLocation = 4; |
| 31 | + return response; |
| 32 | + }); |
| 33 | + |
| 34 | + var client = const Pipeline() |
| 35 | + .addMiddleware(middlewareA) |
| 36 | + .addMiddleware(middlewareB) |
| 37 | + .addClient(new Client.handler((request) async { |
| 38 | + expect(accessLocation, 2); |
| 39 | + accessLocation = 3; |
| 40 | + return new Response(Uri.parse('dart:http'), 200); |
| 41 | + })); |
| 42 | + |
| 43 | + var response = await client.get(Uri.parse('dart:http')); |
| 44 | + |
| 45 | + expect(response, isNotNull); |
| 46 | + expect(accessLocation, 5); |
| 47 | + }); |
| 48 | + |
| 49 | + test('Pipeline can be used as middleware', () async { |
| 50 | + int accessLocation = 0; |
| 51 | + |
| 52 | + var middlewareA = createMiddleware(requestHandler: (request) async { |
| 53 | + expect(accessLocation, 0); |
| 54 | + accessLocation = 1; |
| 55 | + return request; |
| 56 | + }, responseHandler: (response) async { |
| 57 | + expect(accessLocation, 4); |
| 58 | + accessLocation = 5; |
| 59 | + return response; |
| 60 | + }); |
| 61 | + |
| 62 | + var middlewareB = createMiddleware(requestHandler: (request) async { |
| 63 | + expect(accessLocation, 1); |
| 64 | + accessLocation = 2; |
| 65 | + return request; |
| 66 | + }, responseHandler: (response) async { |
| 67 | + expect(accessLocation, 3); |
| 68 | + accessLocation = 4; |
| 69 | + return response; |
| 70 | + }); |
| 71 | + |
| 72 | + var innerPipeline = |
| 73 | + const Pipeline().addMiddleware(middlewareA).addMiddleware(middlewareB); |
| 74 | + |
| 75 | + var client = const Pipeline() |
| 76 | + .addMiddleware(innerPipeline.middleware) |
| 77 | + .addClient(new Client.handler((request) async { |
| 78 | + expect(accessLocation, 2); |
| 79 | + accessLocation = 3; |
| 80 | + return new Response(Uri.parse('dart:http'), 200); |
| 81 | + })); |
| 82 | + |
| 83 | + var response = await client.get(Uri.parse('dart:http')); |
| 84 | + |
| 85 | + expect(response, isNotNull); |
| 86 | + expect(accessLocation, 5); |
| 87 | + }); |
| 88 | + |
| 89 | + test('Pipeline calls close on all middleware', () { |
| 90 | + int accessLocation = 0; |
| 91 | + |
| 92 | + var middlewareA = createMiddleware(onClose: () { |
| 93 | + expect(accessLocation, 0); |
| 94 | + accessLocation = 1; |
| 95 | + }); |
| 96 | + |
| 97 | + var middlewareB = createMiddleware(onClose: () { |
| 98 | + expect(accessLocation, 1); |
| 99 | + accessLocation = 2; |
| 100 | + }); |
| 101 | + |
| 102 | + var client = const Pipeline() |
| 103 | + .addMiddleware(middlewareA) |
| 104 | + .addMiddleware(middlewareB) |
| 105 | + .addClient(new Client.handler((request) async => null, onClose: () { |
| 106 | + expect(accessLocation, 2); |
| 107 | + accessLocation = 3; |
| 108 | + })); |
| 109 | + |
| 110 | + client.close(); |
| 111 | + expect(accessLocation, 3); |
| 112 | + }); |
| 113 | +} |
0 commit comments