/
serve_integration_test.dart
219 lines (188 loc) · 7.24 KB
/
serve_integration_test.dart
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
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
@TestOn('vm')
import 'dart:convert';
import 'dart:io'
show
HttpClient,
HttpClientRequest,
HttpClientResponse,
HttpHeaders,
HttpStatus;
import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'common/utils.dart';
void main() {
late HttpClient httpClient;
setUpAll(() {
// Configure the client so it is possible to download a large number
// of files simultaneously. This prevents getting SocketException on
// too many connections.
httpClient = HttpClient()
..maxConnectionsPerHost = 200
..idleTimeout = Duration(seconds: 30)
..connectionTimeout = Duration(seconds: 30);
});
tearDownAll(() {
httpClient.close();
});
group('basic serve', () {
setUpAll(() async {
// These tests depend on running `test` while a `serve` is ongoing.
await startServer(ensureCleanBuild: true);
});
tearDownAll(() async {
await stopServer(cleanUp: true);
});
test('Doesn\'t compile submodules into the root module', () {
expect(
readGeneratedFileAsString('_test/test/hello_world_test.sound.ddc.js'),
isNot(contains('Hello World!')));
});
test('Can run passing tests with --pub-serve', () async {
await expectTestsPass(usePrecompiled: false);
}, skip: 'TODO: Get non-custom html tests passing with pub serve');
test('Serves a directory list when it fails to fallback on index.html',
() async {
var request =
await httpClient.get('localhost', 8080, 'dir_without_index/');
var firstResponse = await request.close();
expect(firstResponse.statusCode, HttpStatus.notFound);
expect(await utf8.decodeStream(firstResponse.cast<List<int>>()),
contains('dir_without_index/hello.txt'));
});
group('File changes', () {
setUp(() async {
ensureCleanGitClient();
});
test('ddc errors can be fixed', () async {
var path = p.join('test', 'common', 'message.dart');
var error = nextStdOutLine('Error compiling dartdevc module:'
'_test|test/common/message.sound.ddc.js');
var nextBuild = nextFailedBuild;
await replaceAllInFile(path, "'Hello World!';", '1;');
await error;
await nextBuild;
nextBuild = nextSuccessfulBuild;
await replaceAllInFile(path, '1;', "'Hello World!';");
await nextBuild;
await expectTestsPass();
});
test('build errors can be fixed', () async {
var path = p.join('lib', 'expected.fail');
var nextBuild = nextFailedBuild;
await createFile(path, 'some error');
await nextBuild;
nextBuild = nextSuccessfulBuild;
await deleteFile(path);
await nextBuild;
});
test('can hit the server and get cached results', () async {
var firstRequest =
await httpClient.get('localhost', 8080, 'main.dart.js');
var firstResponse = await firstRequest.close();
expect(firstResponse.statusCode, HttpStatus.ok);
var etag = firstResponse.headers[HttpHeaders.etagHeader];
expect(etag, isNotNull);
var cachedRequest =
await httpClient.get('localhost', 8080, 'main.dart.js');
cachedRequest.headers.add(HttpHeaders.ifNoneMatchHeader, etag!);
var cachedResponse = await cachedRequest.close();
expect(cachedResponse.statusCode, HttpStatus.notModified);
});
group('regression tests', () {
test('can get changes to files not read during build', () async {
var firstRequest =
await httpClient.get('localhost', 8080, 'index.html');
var firstResponse = await firstRequest.close();
expect(firstResponse.statusCode, HttpStatus.ok);
var etag = firstResponse.headers[HttpHeaders.etagHeader];
expect(etag, isNotNull);
var cachedRequest =
await httpClient.get('localhost', 8080, 'index.html')
..headers.add(HttpHeaders.ifNoneMatchHeader, etag!);
var cachedResponse = await cachedRequest.close();
expect(cachedResponse.statusCode, HttpStatus.notModified);
var nextBuild = nextSuccessfulBuild;
await replaceAllInFile(
'web/index.html', 'integration tests', 'modified example');
await nextBuild;
var changedRequest =
await httpClient.get('localhost', 8080, 'index.html')
..headers.add(HttpHeaders.ifNoneMatchHeader, etag);
var changedResponse = await changedRequest.close();
expect(changedResponse.statusCode, HttpStatus.ok);
var newEtag = changedResponse.headers[HttpHeaders.etagHeader];
expect(newEtag, isNot(etag));
});
});
});
});
test('can serve a single app with custom environment defines', () async {
await startServer(buildArgs: [
'web',
'--build-filter',
'web/sub/main.dart.js',
'--define',
'build_web_compilers:ddc=environment={"message": "goodbye"}',
]);
addTearDown(() async {
await stopServer();
});
var response =
await (await httpClient.get('localhost', 8080, 'sub/main.dart.js'))
.close();
expect(response.statusCode, HttpStatus.ok);
var badResponse =
await (await httpClient.get('localhost', 8080, 'main.dart.js')).close();
expect(badResponse.statusCode, HttpStatus.notFound);
var ddcFileResponse =
await (await httpClient.get('localhost', 8080, 'main.sound.ddc.js'))
.close();
expect(await utf8.decodeStream(ddcFileResponse), contains('"goodbye"'));
});
test('should serve files in parallel', () async {
await startServer(buildArgs: [
'web',
'--build-filter',
'web/sub/main.dart.js',
'--verbose',
'--define',
'build_web_compilers:ddc=generate-full-dill=true',
]);
addTearDown(() async {
await stopServer();
});
Future<void> read(String path) async {
HttpClientRequest? request;
HttpClientResponse? response;
try {
request = await httpClient.get('localhost', 8080, path);
response = await request.close();
expect(
response.statusCode,
HttpStatus.ok,
reason: '$path ${response.reasonPhrase}',
);
} catch (e, s) {
fail('Error reading $path: $e:$s');
} finally {
request?.abort();
await response?.drain().catchError((_) {});
}
}
const n = 1000;
var futures = [
for (var i = 0; i < n; i++) read('main.sound.ddc.js'),
for (var i = 0; i < n; i++) read('main.sound.ddc.js.map'),
for (var i = 0; i < n; i++) read('main.sound.ddc.dill'),
for (var i = 0; i < n; i++) read('main.sound.ddc.full.dill'),
for (var i = 0; i < n; i++) read('sub/message.sound.ddc.js'),
for (var i = 0; i < n; i++) read('sub/message.sound.ddc.js.map'),
for (var i = 0; i < n; i++) read('sub/message.sound.ddc.dill'),
for (var i = 0; i < n; i++) read('sub/message.sound.ddc.full.dill'),
];
await Future.wait(futures);
});
}