Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(292)

Side by Side Diff: pkg/http_multi_server/test/http_multi_server_test.dart

Issue 311233004: Add an http_multi_server package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/http_multi_server/pubspec.yaml ('k') | pkg/http_server/lib/src/http_multi_server.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2014, 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 library http_multi_server.test;
6
7 import 'dart:async';
8 import 'dart:io';
9
10 import 'package:http/http.dart' as http;
11 import 'package:http_multi_server/http_multi_server.dart';
12 import 'package:http_multi_server/src/utils.dart';
13 import 'package:unittest/unittest.dart';
14
15 void main() {
16 group("with multiple HttpServers", () {
17 var multiServer;
18 var subServer1;
19 var subServer2;
20 var subServer3;
21 setUp(() {
22 return Future.wait([
23 HttpServer.bind("127.0.0.1", 0).then((server) => subServer1 = server),
24 HttpServer.bind("127.0.0.1", 0).then((server) => subServer2 = server),
25 HttpServer.bind("127.0.0.1", 0).then((server) => subServer3 = server)
26 ]).then((servers) => multiServer = new HttpMultiServer(servers));
27 });
28
29 tearDown(() => multiServer.close());
30
31 test("listen listens to all servers", () {
32 multiServer.listen((request) {
33 request.response.write("got request");
34 request.response.close();
35 });
36
37 expect(_read(subServer1), completion(equals("got request")));
38 expect(_read(subServer2), completion(equals("got request")));
39 expect(_read(subServer3), completion(equals("got request")));
40 });
41
42 test("serverHeader= sets the value for all servers", () {
43 multiServer.serverHeader = "http_multi_server test";
44
45 multiServer.listen((request) {
46 request.response.write("got request");
47 request.response.close();
48 });
49
50 expect(_get(subServer1).then((response) {
51 expect(response.headers['server'], equals("http_multi_server test"));
52 }), completes);
53
54 expect(_get(subServer2).then((response) {
55 expect(response.headers['server'], equals("http_multi_server test"));
56 }), completes);
57
58 expect(_get(subServer3).then((response) {
59 expect(response.headers['server'], equals("http_multi_server test"));
60 }), completes);
61 });
62
63 test("connectionsInfo sums the values for all servers", () {
64 var pendingRequests = 0;
65 var awaitingResponseCompleter = new Completer();
66 var sendResponseCompleter = new Completer();
67 multiServer.listen((request) {
68 sendResponseCompleter.future.then((_) {
69 request.response.write("got request");
70 request.response.close();
71 });
72
73 pendingRequests++;
74 if (pendingRequests == 2) awaitingResponseCompleter.complete();
75 });
76
77 // Queue up some requests, then wait on [awaitingResponseCompleter] to
78 // make sure they're in-flight before we check [connectionsInfo].
79 expect(_get(subServer1), completes);
80 expect(_get(subServer2), completes);
81
82 return awaitingResponseCompleter.future.then((_) {
83 var info = multiServer.connectionsInfo();
84 expect(info.total, equals(2));
85 expect(info.active, equals(2));
86 expect(info.idle, equals(0));
87 expect(info.closing, equals(0));
88
89 sendResponseCompleter.complete();
90 });
91 });
92 });
93
94 group("HttpMultiServer.loopback", () {
95 var server;
96 setUp(() {
97 return HttpMultiServer.loopback(0).then((server_) => server = server_);
98 });
99
100 tearDown(() => server.close());
101
102 test("listens on all localhost interfaces", () {
103 server.listen((request) {
104 request.response.write("got request");
105 request.response.close();
106 });
107
108 expect(http.read("http://127.0.0.1:${server.port}/"),
109 completion(equals("got request")));
110
111 return supportsIpV6.then((supportsIpV6) {
112 if (!supportsIpV6) return;
113 expect(http.read("http://[::1]:${server.port}/"),
114 completion(equals("got request")));
115 });
116 });
117 });
118 }
119
120 /// Makes a GET request to the root of [server] and returns the response.
121 Future<http.Response> _get(HttpServer server) => http.get(_urlFor(server));
122
123 /// Makes a GET request to the root of [server] and returns the response body.
124 Future<String> _read(HttpServer server) => http.read(_urlFor(server));
125
126 /// Returns the URL for the root of [server].
127 String _urlFor(HttpServer server) =>
128 "http://${server.address.host}:${server.port}/";
OLDNEW
« no previous file with comments | « pkg/http_multi_server/pubspec.yaml ('k') | pkg/http_server/lib/src/http_multi_server.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698