OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library pub_lish_test; | 5 library pub_lish_test; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import 'dart:json'; | 8 import 'dart:json'; |
9 | 9 |
10 import 'test_pub.dart'; | 10 import 'test_pub.dart'; |
11 import '../../../pkg/unittest/lib/unittest.dart'; | 11 import '../../../pkg/unittest/lib/unittest.dart'; |
12 import '../../pub/io.dart'; | 12 import '../../pub/io.dart'; |
13 | 13 |
14 void handleUploadForm(ScheduledServer server, [Map body]) { | 14 void handleUploadForm(ScheduledServer server, [Map body]) { |
15 server.handle('GET', '/packages/versions/new.json', (request, response) { | 15 server.handle('GET', '/packages/versions/new.json', (request, response) { |
16 return server.url.chain((url) { | 16 return server.url.transform((url) { |
17 expect(request.headers.value('authorization'), | 17 expect(request.headers.value('authorization'), |
18 equals('Bearer access token')); | 18 equals('Bearer access token')); |
19 | 19 |
20 if (body == null) { | 20 if (body == null) { |
21 body = { | 21 body = { |
22 'url': url.resolve('/upload').toString(), | 22 'url': url.resolve('/upload').toString(), |
23 'fields': { | 23 'fields': { |
24 'field1': 'value1', | 24 'field1': 'value1', |
25 'field2': 'value2' | 25 'field2': 'value2' |
26 } | 26 } |
27 }; | 27 }; |
28 } | 28 } |
29 | 29 |
30 response.headers.contentType = new ContentType("application", "json"); | 30 response.headers.contentType = new ContentType("application", "json"); |
31 response.outputStream.writeString(JSON.stringify(body)); | 31 response.outputStream.writeString(JSON.stringify(body)); |
32 return closeHttpResponse(request, response); | 32 response.outputStream.close(); |
33 }); | 33 }); |
34 }); | 34 }); |
35 } | 35 } |
36 | 36 |
37 void handleUpload(ScheduledServer server) { | 37 void handleUpload(ScheduledServer server) { |
38 server.handle('POST', '/upload', (request, response) { | 38 server.handle('POST', '/upload', (request, response) { |
39 // TODO(nweiz): Once a multipart/form-data parser in Dart exists, validate | 39 // TODO(nweiz): Once a multipart/form-data parser in Dart exists, validate |
40 // that the request body is correctly formatted. See issue 6952. | 40 // that the request body is correctly formatted. See issue 6952. |
41 return server.url.chain((url) { | 41 return server.url.transform((url) { |
42 response.statusCode = 302; | 42 response.statusCode = 302; |
43 response.headers.set('location', url.resolve('/create').toString()); | 43 response.headers.set('location', url.resolve('/create').toString()); |
44 return closeHttpResponse(request, response); | 44 response.outputStream.close(); |
45 }); | 45 }); |
46 }); | 46 }); |
47 } | 47 } |
48 | 48 |
49 main() { | 49 main() { |
50 setUp(() { | 50 setUp(() { |
51 dir(appPath, [ | 51 dir(appPath, [ |
52 libPubspec("test_pkg", "1.0.0"), | 52 libPubspec("test_pkg", "1.0.0"), |
53 file("LICENSE", "Eh, do what you want.") | 53 file("LICENSE", "Eh, do what you want.") |
54 ]).scheduleCreate(); | 54 ]).scheduleCreate(); |
55 }); | 55 }); |
56 | 56 |
57 test('archives and uploads a package', () { | 57 test('archives and uploads a package', () { |
58 var server = new ScheduledServer(); | 58 var server = new ScheduledServer(); |
59 credentialsFile(server, 'access token').scheduleCreate(); | 59 credentialsFile(server, 'access token').scheduleCreate(); |
60 var pub = startPubLish(server); | 60 var pub = startPubLish(server); |
61 handleUploadForm(server); | 61 handleUploadForm(server); |
62 handleUpload(server); | 62 handleUpload(server); |
63 | 63 |
64 server.handle('GET', '/create', (request, response) { | 64 server.handle('GET', '/create', (request, response) { |
65 response.outputStream.writeString(JSON.stringify({ | 65 response.outputStream.writeString(JSON.stringify({ |
66 'success': {'message': 'Package test_pkg 1.0.0 uploaded!'} | 66 'success': {'message': 'Package test_pkg 1.0.0 uploaded!'} |
67 })); | 67 })); |
68 return closeHttpResponse(request, response); | 68 response.outputStream.close(); |
69 }); | 69 }); |
70 | 70 |
71 expectLater(pub.nextLine(), equals('Package test_pkg 1.0.0 uploaded!')); | 71 expectLater(pub.nextLine(), equals('Package test_pkg 1.0.0 uploaded!')); |
72 pub.shouldExit(0); | 72 pub.shouldExit(0); |
73 | 73 |
74 run(); | 74 run(); |
75 }); | 75 }); |
76 | 76 |
77 // TODO(nweiz): Once a multipart/form-data parser in Dart exists, we should | 77 // TODO(nweiz): Once a multipart/form-data parser in Dart exists, we should |
78 // test that "pub lish" chooses the correct files to publish. | 78 // test that "pub lish" chooses the correct files to publish. |
79 | 79 |
80 test('credentials are invalid', () { | 80 test('credentials are invalid', () { |
81 var server = new ScheduledServer(); | 81 var server = new ScheduledServer(); |
82 credentialsFile(server, 'access token').scheduleCreate(); | 82 credentialsFile(server, 'access token').scheduleCreate(); |
83 var pub = startPubLish(server); | 83 var pub = startPubLish(server); |
84 | 84 |
85 server.handle('GET', '/packages/versions/new.json', (request, response) { | 85 server.handle('GET', '/packages/versions/new.json', (request, response) { |
86 response.statusCode = 401; | 86 response.statusCode = 401; |
87 response.headers.set('www-authenticate', 'Bearer error="invalid_token",' | 87 response.headers.set('www-authenticate', 'Bearer error="invalid_token",' |
88 ' error_description="your token sucks"'); | 88 ' error_description="your token sucks"'); |
89 response.outputStream.writeString(JSON.stringify({ | 89 response.outputStream.writeString(JSON.stringify({ |
90 'error': {'message': 'your token sucks'} | 90 'error': {'message': 'your token sucks'} |
91 })); | 91 })); |
92 return closeHttpResponse(request, response); | 92 response.outputStream.close(); |
93 }); | 93 }); |
94 | 94 |
95 expectLater(pub.nextErrLine(), equals('OAuth2 authorization failed (your ' | 95 expectLater(pub.nextErrLine(), equals('OAuth2 authorization failed (your ' |
96 'token sucks).')); | 96 'token sucks).')); |
97 expectLater(pub.nextLine(), equals('Pub needs your authorization to upload ' | 97 expectLater(pub.nextLine(), equals('Pub needs your authorization to upload ' |
98 'packages on your behalf.')); | 98 'packages on your behalf.')); |
99 pub.kill(); | 99 pub.kill(); |
100 | 100 |
101 run(); | 101 run(); |
102 }); | 102 }); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 credentialsFile(server, 'access token').scheduleCreate(); | 143 credentialsFile(server, 'access token').scheduleCreate(); |
144 var pub = startPubLish(server); | 144 var pub = startPubLish(server); |
145 pub.writeLine("y"); | 145 pub.writeLine("y"); |
146 handleUploadForm(server); | 146 handleUploadForm(server); |
147 handleUpload(server); | 147 handleUpload(server); |
148 | 148 |
149 server.handle('GET', '/create', (request, response) { | 149 server.handle('GET', '/create', (request, response) { |
150 response.outputStream.writeString(JSON.stringify({ | 150 response.outputStream.writeString(JSON.stringify({ |
151 'success': {'message': 'Package test_pkg 1.0.0 uploaded!'} | 151 'success': {'message': 'Package test_pkg 1.0.0 uploaded!'} |
152 })); | 152 })); |
153 return closeHttpResponse(request, response); | 153 response.outputStream.close(); |
154 }); | 154 }); |
155 | 155 |
156 pub.shouldExit(0); | 156 pub.shouldExit(0); |
157 expectLater(pub.remainingStdout(), | 157 expectLater(pub.remainingStdout(), |
158 contains('Package test_pkg 1.0.0 uploaded!')); | 158 contains('Package test_pkg 1.0.0 uploaded!')); |
159 | 159 |
160 run(); | 160 run(); |
161 }); | 161 }); |
162 | 162 |
163 test('upload form provides an error', () { | 163 test('upload form provides an error', () { |
164 var server = new ScheduledServer(); | 164 var server = new ScheduledServer(); |
165 credentialsFile(server, 'access token').scheduleCreate(); | 165 credentialsFile(server, 'access token').scheduleCreate(); |
166 var pub = startPubLish(server); | 166 var pub = startPubLish(server); |
167 | 167 |
168 server.handle('GET', '/packages/versions/new.json', (request, response) { | 168 server.handle('GET', '/packages/versions/new.json', (request, response) { |
169 response.statusCode = 400; | 169 response.statusCode = 400; |
170 response.outputStream.writeString(JSON.stringify({ | 170 response.outputStream.writeString(JSON.stringify({ |
171 'error': {'message': 'your request sucked'} | 171 'error': {'message': 'your request sucked'} |
172 })); | 172 })); |
173 return closeHttpResponse(request, response); | 173 response.outputStream.close(); |
174 }); | 174 }); |
175 | 175 |
176 expectLater(pub.nextErrLine(), equals('your request sucked')); | 176 expectLater(pub.nextErrLine(), equals('your request sucked')); |
177 pub.shouldExit(1); | 177 pub.shouldExit(1); |
178 | 178 |
179 run(); | 179 run(); |
180 }); | 180 }); |
181 | 181 |
182 test('upload form provides invalid JSON', () { | 182 test('upload form provides invalid JSON', () { |
183 var server = new ScheduledServer(); | 183 var server = new ScheduledServer(); |
184 credentialsFile(server, 'access token').scheduleCreate(); | 184 credentialsFile(server, 'access token').scheduleCreate(); |
185 var pub = startPubLish(server); | 185 var pub = startPubLish(server); |
186 | 186 |
187 server.handle('GET', '/packages/versions/new.json', (request, response) { | 187 server.handle('GET', '/packages/versions/new.json', (request, response) { |
188 response.outputStream.writeString('{not json'); | 188 response.outputStream.writeString('{not json'); |
189 return closeHttpResponse(request, response); | 189 response.outputStream.close(); |
190 }); | 190 }); |
191 | 191 |
192 expectLater(pub.nextErrLine(), equals('Invalid server response:')); | 192 expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
193 expectLater(pub.nextErrLine(), equals('{not json')); | 193 expectLater(pub.nextErrLine(), equals('{not json')); |
194 pub.shouldExit(1); | 194 pub.shouldExit(1); |
195 | 195 |
196 run(); | 196 run(); |
197 }); | 197 }); |
198 | 198 |
199 test('upload form is missing url', () { | 199 test('upload form is missing url', () { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 }); | 305 }); |
306 | 306 |
307 test("cloud storage upload doesn't redirect", () { | 307 test("cloud storage upload doesn't redirect", () { |
308 var server = new ScheduledServer(); | 308 var server = new ScheduledServer(); |
309 credentialsFile(server, 'access token').scheduleCreate(); | 309 credentialsFile(server, 'access token').scheduleCreate(); |
310 var pub = startPubLish(server); | 310 var pub = startPubLish(server); |
311 handleUploadForm(server); | 311 handleUploadForm(server); |
312 | 312 |
313 server.handle('POST', '/upload', (request, response) { | 313 server.handle('POST', '/upload', (request, response) { |
314 // don't set the location header | 314 // don't set the location header |
315 return closeHttpResponse(request, response); | 315 response.outputStream.close(); |
316 }); | 316 }); |
317 | 317 |
318 expectLater(pub.nextErrLine(), equals('Failed to upload the package.')); | 318 expectLater(pub.nextErrLine(), equals('Failed to upload the package.')); |
319 pub.shouldExit(1); | 319 pub.shouldExit(1); |
320 | 320 |
321 run(); | 321 run(); |
322 }); | 322 }); |
323 | 323 |
324 test('package creation provides an error', () { | 324 test('package creation provides an error', () { |
325 var server = new ScheduledServer(); | 325 var server = new ScheduledServer(); |
326 credentialsFile(server, 'access token').scheduleCreate(); | 326 credentialsFile(server, 'access token').scheduleCreate(); |
327 var pub = startPubLish(server); | 327 var pub = startPubLish(server); |
328 handleUploadForm(server); | 328 handleUploadForm(server); |
329 handleUpload(server); | 329 handleUpload(server); |
330 | 330 |
331 server.handle('GET', '/create', (request, response) { | 331 server.handle('GET', '/create', (request, response) { |
332 response.statusCode = 400; | 332 response.statusCode = 400; |
333 response.outputStream.writeString(JSON.stringify({ | 333 response.outputStream.writeString(JSON.stringify({ |
334 'error': {'message': 'Your package was too boring.'} | 334 'error': {'message': 'Your package was too boring.'} |
335 })); | 335 })); |
336 return closeHttpResponse(request, response); | 336 response.outputStream.close(); |
337 }); | 337 }); |
338 | 338 |
339 expectLater(pub.nextErrLine(), equals('Your package was too boring.')); | 339 expectLater(pub.nextErrLine(), equals('Your package was too boring.')); |
340 pub.shouldExit(1); | 340 pub.shouldExit(1); |
341 | 341 |
342 run(); | 342 run(); |
343 }); | 343 }); |
344 | 344 |
345 test('package creation provides invalid JSON', () { | 345 test('package creation provides invalid JSON', () { |
346 var server = new ScheduledServer(); | 346 var server = new ScheduledServer(); |
347 credentialsFile(server, 'access token').scheduleCreate(); | 347 credentialsFile(server, 'access token').scheduleCreate(); |
348 var pub = startPubLish(server); | 348 var pub = startPubLish(server); |
349 handleUploadForm(server); | 349 handleUploadForm(server); |
350 handleUpload(server); | 350 handleUpload(server); |
351 | 351 |
352 server.handle('GET', '/create', (request, response) { | 352 server.handle('GET', '/create', (request, response) { |
353 response.outputStream.writeString('{not json'); | 353 response.outputStream.writeString('{not json'); |
354 return closeHttpResponse(request, response); | 354 response.outputStream.close(); |
355 }); | 355 }); |
356 | 356 |
357 expectLater(pub.nextErrLine(), equals('Invalid server response:')); | 357 expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
358 expectLater(pub.nextErrLine(), equals('{not json')); | 358 expectLater(pub.nextErrLine(), equals('{not json')); |
359 pub.shouldExit(1); | 359 pub.shouldExit(1); |
360 | 360 |
361 run(); | 361 run(); |
362 }); | 362 }); |
363 | 363 |
364 test('package creation provides a malformed error', () { | 364 test('package creation provides a malformed error', () { |
365 var server = new ScheduledServer(); | 365 var server = new ScheduledServer(); |
366 credentialsFile(server, 'access token').scheduleCreate(); | 366 credentialsFile(server, 'access token').scheduleCreate(); |
367 var pub = startPubLish(server); | 367 var pub = startPubLish(server); |
368 handleUploadForm(server); | 368 handleUploadForm(server); |
369 handleUpload(server); | 369 handleUpload(server); |
370 | 370 |
371 var body = {'error': 'Your package was too boring.'}; | 371 var body = {'error': 'Your package was too boring.'}; |
372 server.handle('GET', '/create', (request, response) { | 372 server.handle('GET', '/create', (request, response) { |
373 response.statusCode = 400; | 373 response.statusCode = 400; |
374 response.outputStream.writeString(JSON.stringify(body)); | 374 response.outputStream.writeString(JSON.stringify(body)); |
375 return closeHttpResponse(request, response); | 375 response.outputStream.close(); |
376 }); | 376 }); |
377 | 377 |
378 expectLater(pub.nextErrLine(), equals('Invalid server response:')); | 378 expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
379 expectLater(pub.nextErrLine(), equals(JSON.stringify(body))); | 379 expectLater(pub.nextErrLine(), equals(JSON.stringify(body))); |
380 pub.shouldExit(1); | 380 pub.shouldExit(1); |
381 | 381 |
382 run(); | 382 run(); |
383 }); | 383 }); |
384 | 384 |
385 test('package creation provides a malformed success', () { | 385 test('package creation provides a malformed success', () { |
386 var server = new ScheduledServer(); | 386 var server = new ScheduledServer(); |
387 credentialsFile(server, 'access token').scheduleCreate(); | 387 credentialsFile(server, 'access token').scheduleCreate(); |
388 var pub = startPubLish(server); | 388 var pub = startPubLish(server); |
389 handleUploadForm(server); | 389 handleUploadForm(server); |
390 handleUpload(server); | 390 handleUpload(server); |
391 | 391 |
392 var body = {'success': 'Your package was awesome.'}; | 392 var body = {'success': 'Your package was awesome.'}; |
393 server.handle('GET', '/create', (request, response) { | 393 server.handle('GET', '/create', (request, response) { |
394 response.outputStream.writeString(JSON.stringify(body)); | 394 response.outputStream.writeString(JSON.stringify(body)); |
395 return closeHttpResponse(request, response); | 395 response.outputStream.close(); |
396 }); | 396 }); |
397 | 397 |
398 expectLater(pub.nextErrLine(), equals('Invalid server response:')); | 398 expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
399 expectLater(pub.nextErrLine(), equals(JSON.stringify(body))); | 399 expectLater(pub.nextErrLine(), equals(JSON.stringify(body))); |
400 pub.shouldExit(1); | 400 pub.shouldExit(1); |
401 | 401 |
402 run(); | 402 run(); |
403 }); | 403 }); |
404 } | 404 } |
OLD | NEW |