| 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 FormDataTest; | 5 library FormDataTest; |
| 6 | 6 |
| 7 import '../../pkg/unittest/lib/unittest.dart'; | 7 import '../../pkg/unittest/lib/unittest.dart'; |
| 8 import '../../pkg/unittest/lib/html_config.dart'; | 8 import '../../pkg/unittest/lib/html_individual_config.dart'; |
| 9 import 'dart:html'; | 9 import 'dart:html'; |
| 10 import 'dart:uri'; |
| 10 | 11 |
| 11 void fail(message) { | 12 void fail(message) { |
| 12 guardAsync(() { | 13 guardAsync(() { |
| 13 expect(false, isTrue, reason: message); | 14 expect(false, isTrue, reason: message); |
| 14 }); | 15 }); |
| 15 } | 16 } |
| 16 | 17 |
| 17 void main() { | 18 void main() { |
| 18 // TODO(efortuna): This is a bad test. Revisit when we have tests that can run | 19 // TODO(efortuna): This is a bad test. Revisit when we have tests that can run |
| 19 // both a server and fire up a browser. | 20 // both a server and fire up a browser. |
| 20 useHtmlConfiguration(); | 21 useHtmlIndividualConfiguration(); |
| 21 | 22 |
| 22 var isFormData = predicate((x) => x is FormData, 'is a FormData'); | 23 group('supported', () { |
| 23 | 24 test('supported', () { |
| 24 test('constructorTest1', () { | 25 expect(FormData.supported, isTrue); |
| 25 var form = new FormData(); | 26 }); |
| 26 expect(form, isNotNull); | |
| 27 expect(form, isFormData); | |
| 28 }); | 27 }); |
| 29 | 28 |
| 30 test('constructorTest2', () { | 29 group('functional', () { |
| 31 var form = new FormData(new FormElement()); | 30 test('unsupported throws', () { |
| 32 expect(form, isNotNull); | 31 var expectation = FormData.supported ? returnsNormally : throws; |
| 33 expect(form, isFormData); | 32 expect(() { |
| 34 }); | 33 new FormData(); |
| 34 }, expectation); |
| 35 }); |
| 35 | 36 |
| 36 test('appendTest', () { | 37 var isFormData = predicate((x) => x is FormData, 'is a FormData'); |
| 37 var form = new FormData(); | |
| 38 form.append('test', '1'); | |
| 39 form.append('username', 'Elmo'); | |
| 40 form.append('address', '1 Sesame Street'); | |
| 41 form.append('password', '123456', 'foo'); | |
| 42 expect(form, isNotNull); | |
| 43 }); | |
| 44 | 38 |
| 45 test('appendBlob', () { | 39 if (FormData.supported) { |
| 46 var form = new FormData(); | 40 test('constructorTest1', () { |
| 47 var blob = new Blob( | 41 var form = new FormData(); |
| 48 ['Indescribable... Indestructible! Nothing can stop it!'], | 42 expect(form, isNotNull); |
| 49 'text/plain'); | 43 expect(form, isFormData); |
| 50 form.append('theBlob', blob, 'theBlob.txt'); | 44 }); |
| 51 }); | |
| 52 | 45 |
| 53 test('send', () { | 46 test('constructorTest2', () { |
| 54 var form = new FormData(); | 47 var form = new FormData(new FormElement()); |
| 55 var blobString = 'Indescribable... Indestructible! Nothing can stop it!'; | 48 expect(form, isNotNull); |
| 56 var blob = new Blob( | 49 expect(form, isFormData); |
| 57 [blobString], | 50 }); |
| 58 'text/plain'); | |
| 59 form.append('theBlob', blob, 'theBlob.txt'); | |
| 60 | 51 |
| 61 var xhr = new HttpRequest(); | 52 test('appendTest', () { |
| 62 xhr.open("POST", "http://localhost:${window.location.port}/echo"); | 53 var form = new FormData(); |
| 54 form.append('test', '1'); |
| 55 form.append('username', 'Elmo'); |
| 56 form.append('address', '1 Sesame Street'); |
| 57 form.append('password', '123456', 'foo'); |
| 58 expect(form, isNotNull); |
| 59 }); |
| 63 | 60 |
| 64 xhr.onLoad.listen(expectAsync1((e) { | 61 test('appendBlob', () { |
| 65 expect(xhr.responseText.contains(blobString), true); | 62 var form = new FormData(); |
| 66 })); | 63 var blob = new Blob( |
| 67 xhr.onError.listen((e) { | 64 ['Indescribable... Indestructible! Nothing can stop it!'], |
| 68 fail('$e'); | 65 'text/plain'); |
| 69 }); | 66 form.append('theBlob', blob, 'theBlob.txt'); |
| 70 xhr.send(form); | 67 }); |
| 68 |
| 69 test('send', () { |
| 70 var form = new FormData(); |
| 71 var blobString = |
| 72 'Indescribable... Indestructible! Nothing can stop it!'; |
| 73 var blob = new Blob( |
| 74 [blobString], |
| 75 'text/plain'); |
| 76 form.append('theBlob', blob, 'theBlob.txt'); |
| 77 |
| 78 var xhr = new HttpRequest(); |
| 79 xhr.open('POST', |
| 80 '${window.location.protocol}//${window.location.host}/echo'); |
| 81 |
| 82 xhr.onLoad.listen(expectAsync1((e) { |
| 83 expect(xhr.responseText.contains(blobString), true); |
| 84 })); |
| 85 xhr.onError.listen((e) { |
| 86 fail('$e'); |
| 87 }); |
| 88 xhr.send(form); |
| 89 }); |
| 90 } |
| 71 }); | 91 }); |
| 72 } | 92 } |
| OLD | NEW |