OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library pub_lish_test; |
| 6 |
| 7 import 'dart:io'; |
| 8 import 'dart:json'; |
| 9 |
| 10 import 'test_pub.dart'; |
| 11 import '../../../pkg/unittest/lib/unittest.dart'; |
| 12 import '../../pub/io.dart'; |
| 13 |
| 14 void handleUploadForm(ScheduledServer server, [Map body]) { |
| 15 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 16 return server.url.transform((url) { |
| 17 expect(request.headers.value('authorization'), |
| 18 equals('Bearer access token')); |
| 19 |
| 20 if (body == null) { |
| 21 body = { |
| 22 'url': url.resolve('/upload').toString(), |
| 23 'fields': { |
| 24 'field1': 'value1', |
| 25 'field2': 'value2' |
| 26 } |
| 27 }; |
| 28 } |
| 29 |
| 30 response.headers.contentType = new ContentType("application", "json"); |
| 31 response.outputStream.writeString(JSON.stringify(body)); |
| 32 response.outputStream.close(); |
| 33 }); |
| 34 }); |
| 35 } |
| 36 |
| 37 void handleUpload(ScheduledServer server) { |
| 38 server.handle('POST', '/upload', (request, response) { |
| 39 // TODO(nweiz): Once a multipart/form-data parser in Dart exists, validate |
| 40 // that the request body is correctly formatted. See issue 6952. |
| 41 return server.url.transform((url) { |
| 42 response.statusCode = 302; |
| 43 response.headers.set('location', url.resolve('/create').toString()); |
| 44 response.outputStream.close(); |
| 45 }); |
| 46 }); |
| 47 } |
| 48 |
| 49 main() { |
| 50 setUp(() => dir(appPath, [libPubspec("test_pkg", "1.0.0")]).scheduleCreate()); |
| 51 |
| 52 test('archives and uploads a package', () { |
| 53 var server = new ScheduledServer(); |
| 54 credentialsFile(server, 'access token').scheduleCreate(); |
| 55 var pub = startPubLish(server); |
| 56 handleUploadForm(server); |
| 57 handleUpload(server); |
| 58 |
| 59 server.handle('GET', '/create', (request, response) { |
| 60 response.outputStream.writeString(JSON.stringify({ |
| 61 'success': {'message': 'Package test_pkg 1.0.0 uploaded!'} |
| 62 })); |
| 63 response.outputStream.close(); |
| 64 }); |
| 65 |
| 66 expectLater(pub.nextLine(), equals('Package test_pkg 1.0.0 uploaded!')); |
| 67 pub.shouldExit(0); |
| 68 |
| 69 run(); |
| 70 }); |
| 71 |
| 72 // TODO(nweiz): Once a multipart/form-data parser in Dart exists, we should |
| 73 // test that "pub lish" chooses the correct files to publish. |
| 74 |
| 75 // TODO(nweiz): Once issue 6813 is fixed, test that OAuth2 authentication |
| 76 // errors cause the client to try re-authenticating. |
| 77 |
| 78 test('upload form provides an error', () { |
| 79 var server = new ScheduledServer(); |
| 80 credentialsFile(server, 'access token').scheduleCreate(); |
| 81 var pub = startPubLish(server); |
| 82 |
| 83 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 84 response.statusCode = 400; |
| 85 response.outputStream.writeString(JSON.stringify({ |
| 86 'error': {'message': 'your request sucked'} |
| 87 })); |
| 88 response.outputStream.close(); |
| 89 }); |
| 90 |
| 91 expectLater(pub.nextErrLine(), equals('your request sucked')); |
| 92 pub.shouldExit(1); |
| 93 |
| 94 run(); |
| 95 }); |
| 96 |
| 97 test('upload form provides invalid JSON', () { |
| 98 var server = new ScheduledServer(); |
| 99 credentialsFile(server, 'access token').scheduleCreate(); |
| 100 var pub = startPubLish(server); |
| 101 |
| 102 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 103 response.outputStream.writeString('{not json'); |
| 104 response.outputStream.close(); |
| 105 }); |
| 106 |
| 107 expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
| 108 expectLater(pub.nextErrLine(), equals('{not json')); |
| 109 pub.shouldExit(1); |
| 110 |
| 111 run(); |
| 112 }); |
| 113 |
| 114 test('upload form is missing url', () { |
| 115 var server = new ScheduledServer(); |
| 116 credentialsFile(server, 'access token').scheduleCreate(); |
| 117 var pub = startPubLish(server); |
| 118 |
| 119 var body = { |
| 120 'fields': { |
| 121 'field1': 'value1', |
| 122 'field2': 'value2' |
| 123 } |
| 124 }; |
| 125 |
| 126 handleUploadForm(server, body); |
| 127 expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
| 128 expectLater(pub.nextErrLine(), equals(JSON.stringify(body))); |
| 129 pub.shouldExit(1); |
| 130 |
| 131 run(); |
| 132 }); |
| 133 |
| 134 test('upload form url is not a string', () { |
| 135 var server = new ScheduledServer(); |
| 136 credentialsFile(server, 'access token').scheduleCreate(); |
| 137 var pub = startPubLish(server); |
| 138 |
| 139 var body = { |
| 140 'url': 12, |
| 141 'fields': { |
| 142 'field1': 'value1', |
| 143 'field2': 'value2' |
| 144 } |
| 145 }; |
| 146 |
| 147 handleUploadForm(server, body); |
| 148 expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
| 149 expectLater(pub.nextErrLine(), equals(JSON.stringify(body))); |
| 150 pub.shouldExit(1); |
| 151 |
| 152 run(); |
| 153 }); |
| 154 |
| 155 test('upload form is missing fields', () { |
| 156 var server = new ScheduledServer(); |
| 157 credentialsFile(server, 'access token').scheduleCreate(); |
| 158 var pub = startPubLish(server); |
| 159 |
| 160 var body = {'url': 'http://example.com/upload'}; |
| 161 handleUploadForm(server, body); |
| 162 expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
| 163 expectLater(pub.nextErrLine(), equals(JSON.stringify(body))); |
| 164 pub.shouldExit(1); |
| 165 |
| 166 run(); |
| 167 }); |
| 168 |
| 169 test('upload form fields is not a map', () { |
| 170 var server = new ScheduledServer(); |
| 171 credentialsFile(server, 'access token').scheduleCreate(); |
| 172 var pub = startPubLish(server); |
| 173 |
| 174 var body = {'url': 'http://example.com/upload', 'fields': 12}; |
| 175 handleUploadForm(server, body); |
| 176 expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
| 177 expectLater(pub.nextErrLine(), equals(JSON.stringify(body))); |
| 178 pub.shouldExit(1); |
| 179 |
| 180 run(); |
| 181 }); |
| 182 |
| 183 test('upload form fields has a non-string value', () { |
| 184 var server = new ScheduledServer(); |
| 185 credentialsFile(server, 'access token').scheduleCreate(); |
| 186 var pub = startPubLish(server); |
| 187 |
| 188 var body = { |
| 189 'url': 'http://example.com/upload', |
| 190 'fields': {'field': 12} |
| 191 }; |
| 192 handleUploadForm(server, body); |
| 193 expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
| 194 expectLater(pub.nextErrLine(), equals(JSON.stringify(body))); |
| 195 pub.shouldExit(1); |
| 196 |
| 197 run(); |
| 198 }); |
| 199 |
| 200 test('cloud storage upload provides an error', () { |
| 201 var server = new ScheduledServer(); |
| 202 credentialsFile(server, 'access token').scheduleCreate(); |
| 203 var pub = startPubLish(server); |
| 204 handleUploadForm(server); |
| 205 |
| 206 server.handle('POST', '/upload', (request, response) { |
| 207 response.statusCode = 400; |
| 208 response.headers.contentType = new ContentType('application', 'xml'); |
| 209 response.outputStream.writeString('<Error><Message>Your request sucked.' |
| 210 '</Message></Error>'); |
| 211 response.outputStream.close(); |
| 212 }); |
| 213 |
| 214 // TODO(nweiz): This should use the server's error message once the client |
| 215 // can parse the XML. |
| 216 expectLater(pub.nextErrLine(), equals('Failed to upload the package.')); |
| 217 pub.shouldExit(1); |
| 218 |
| 219 run(); |
| 220 }); |
| 221 |
| 222 test("cloud storage upload doesn't redirect", () { |
| 223 var server = new ScheduledServer(); |
| 224 credentialsFile(server, 'access token').scheduleCreate(); |
| 225 var pub = startPubLish(server); |
| 226 handleUploadForm(server); |
| 227 |
| 228 server.handle('POST', '/upload', (request, response) { |
| 229 // don't set the location header |
| 230 response.outputStream.close(); |
| 231 }); |
| 232 |
| 233 expectLater(pub.nextErrLine(), equals('Failed to upload the package.')); |
| 234 pub.shouldExit(1); |
| 235 |
| 236 run(); |
| 237 }); |
| 238 |
| 239 test('package creation provides an error', () { |
| 240 var server = new ScheduledServer(); |
| 241 credentialsFile(server, 'access token').scheduleCreate(); |
| 242 var pub = startPubLish(server); |
| 243 handleUploadForm(server); |
| 244 handleUpload(server); |
| 245 |
| 246 server.handle('GET', '/create', (request, response) { |
| 247 response.outputStream.writeString(JSON.stringify({ |
| 248 'error': {'message': 'Your package was too boring.'} |
| 249 })); |
| 250 response.outputStream.close(); |
| 251 }); |
| 252 |
| 253 expectLater(pub.nextErrLine(), equals('Your package was too boring.')); |
| 254 pub.shouldExit(1); |
| 255 |
| 256 run(); |
| 257 }); |
| 258 |
| 259 test('package creation provides invalid JSON', () { |
| 260 var server = new ScheduledServer(); |
| 261 credentialsFile(server, 'access token').scheduleCreate(); |
| 262 var pub = startPubLish(server); |
| 263 handleUploadForm(server); |
| 264 handleUpload(server); |
| 265 |
| 266 server.handle('GET', '/create', (request, response) { |
| 267 response.outputStream.writeString('{not json'); |
| 268 response.outputStream.close(); |
| 269 }); |
| 270 |
| 271 expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
| 272 expectLater(pub.nextErrLine(), equals('{not json')); |
| 273 pub.shouldExit(1); |
| 274 |
| 275 run(); |
| 276 }); |
| 277 |
| 278 test('package creation provides a malformed error', () { |
| 279 var server = new ScheduledServer(); |
| 280 credentialsFile(server, 'access token').scheduleCreate(); |
| 281 var pub = startPubLish(server); |
| 282 handleUploadForm(server); |
| 283 handleUpload(server); |
| 284 |
| 285 var body = {'error': 'Your package was too boring.'}; |
| 286 server.handle('GET', '/create', (request, response) { |
| 287 response.statusCode = 400; |
| 288 response.outputStream.writeString(JSON.stringify(body)); |
| 289 response.outputStream.close(); |
| 290 }); |
| 291 |
| 292 expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
| 293 expectLater(pub.nextErrLine(), equals(JSON.stringify(body))); |
| 294 pub.shouldExit(1); |
| 295 |
| 296 run(); |
| 297 }); |
| 298 |
| 299 test('package creation provides a malformed success', () { |
| 300 var server = new ScheduledServer(); |
| 301 credentialsFile(server, 'access token').scheduleCreate(); |
| 302 var pub = startPubLish(server); |
| 303 handleUploadForm(server); |
| 304 handleUpload(server); |
| 305 |
| 306 var body = {'success': 'Your package was awesome.'}; |
| 307 server.handle('GET', '/create', (request, response) { |
| 308 response.outputStream.writeString(JSON.stringify(body)); |
| 309 response.outputStream.close(); |
| 310 }); |
| 311 |
| 312 expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
| 313 expectLater(pub.nextErrLine(), equals(JSON.stringify(body))); |
| 314 pub.shouldExit(1); |
| 315 |
| 316 run(); |
| 317 }); |
| 318 } |
OLD | NEW |