OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 // VMOptions=--error_on_bad_type --error_on_bad_override | 4 // VMOptions=--error_on_bad_type --error_on_bad_override |
5 | 5 |
6 import 'dart:async'; | 6 import 'dart:async'; |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'package:observatory/service_io.dart'; | 9 import 'package:observatory/service_io.dart'; |
10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
11 import 'test_helper.dart'; | 11 import 'test_helper.dart'; |
12 | 12 |
13 Future<String> readResponse(HttpClientResponse response) { | 13 Future<String> readResponse(HttpClientResponse response) { |
14 var completer = new Completer(); | 14 var completer = new Completer(); |
15 var contents = new StringBuffer(); | 15 var contents = new StringBuffer(); |
16 response.transform(UTF8.decoder).listen((String data) { | 16 response.transform(UTF8.decoder).listen((String data) { |
17 contents.write(data); | 17 contents.write(data); |
18 }, onDone: () => completer.complete(contents.toString())); | 18 }, onDone: () => completer.complete(contents.toString())); |
19 return completer.future; | 19 return completer.future; |
20 } | 20 } |
21 | 21 |
22 | |
23 var tests = [ | 22 var tests = [ |
24 // Write a file with the ? character in the filename. | 23 // Write a file with the ? character in the filename. |
25 (VM vm) async { | 24 (VM vm) async { |
26 var fsId = 'test'; | 25 var fsId = 'test'; |
27 // NOTE: When using the URI encoding scheme, paths cannot be absolute. | 26 // NOTE: When using the URI encoding scheme, paths cannot be absolute. |
28 var filePath = 'foo/bar.dat'; | 27 var filePath = 'foo/bar.dat'; |
29 var fileUri = Uri.parse(filePath); | 28 var fileUri = Uri.parse(filePath); |
30 var fileUriBase64 = BASE64.encode(UTF8.encode(fileUri.toString())); | 29 var fileUriBase64 = BASE64.encode(UTF8.encode(fileUri.toString())); |
31 var fileContents = [0, 1, 2, 3, 4, 5, 6, 255]; | 30 var fileContents = [0, 1, 2, 3, 4, 5, 6, 255]; |
32 var fileContentsBase64 = BASE64.encode(fileContents); | 31 var fileContentsBase64 = BASE64.encode(fileContents); |
33 | 32 |
34 var filePath2 = 'baz/boo.dat'; | 33 var filePath2 = 'baz/boo.dat'; |
35 var fileUri2 = Uri.parse(filePath2); | 34 var fileUri2 = Uri.parse(filePath2); |
36 | 35 |
37 var result; | 36 var result; |
38 // Create DevFS. | 37 // Create DevFS. |
39 result = await vm.invokeRpcNoUpgrade('_createDevFS', { 'fsName': fsId }); | 38 result = await vm.invokeRpcNoUpgrade('_createDevFS', {'fsName': fsId}); |
40 expect(result['type'], equals('FileSystem')); | 39 expect(result['type'], equals('FileSystem')); |
41 expect(result['name'], equals(fsId)); | 40 expect(result['name'], equals(fsId)); |
42 expect(result['uri'], new isInstanceOf<String>()); | 41 expect(result['uri'], new isInstanceOf<String>()); |
43 | 42 |
44 // Write the file by issuing an HTTP PUT. | 43 // Write the file by issuing an HTTP PUT. |
45 HttpClient client = new HttpClient(); | 44 HttpClient client = new HttpClient(); |
46 HttpClientRequest request = | 45 HttpClientRequest request = |
47 await client.putUrl(Uri.parse(serviceHttpAddress)); | 46 await client.putUrl(Uri.parse(serviceHttpAddress)); |
48 request.headers.add('dev_fs_name', fsId); | 47 request.headers.add('dev_fs_name', fsId); |
49 request.headers.add('dev_fs_uri_b64', fileUriBase64); | 48 request.headers.add('dev_fs_uri_b64', fileUriBase64); |
(...skipping 11 matching lines...) Expand all Loading... |
61 request.write(GZIP.encode(fileContents)); | 60 request.write(GZIP.encode(fileContents)); |
62 response = await request.close(); | 61 response = await request.close(); |
63 responseBody = await readResponse(response); | 62 responseBody = await readResponse(response); |
64 result = JSON.decode(responseBody); | 63 result = JSON.decode(responseBody); |
65 Map error = result['error']['data']; | 64 Map error = result['error']['data']; |
66 expect(error, isNotNull); | 65 expect(error, isNotNull); |
67 expect(error['details'].contains("expects the 'path' parameter"), isTrue); | 66 expect(error['details'].contains("expects the 'path' parameter"), isTrue); |
68 | 67 |
69 // Write the file again but this time with the true file contents. | 68 // Write the file again but this time with the true file contents. |
70 client = new HttpClient(); | 69 client = new HttpClient(); |
71 request = | 70 request = await client.putUrl(Uri.parse(serviceHttpAddress)); |
72 await client.putUrl(Uri.parse(serviceHttpAddress)); | |
73 request.headers.add('dev_fs_name', fsId); | 71 request.headers.add('dev_fs_name', fsId); |
74 request.headers.add('dev_fs_uri_b64', fileUriBase64); | 72 request.headers.add('dev_fs_uri_b64', fileUriBase64); |
75 request.add(GZIP.encode(fileContents)); | 73 request.add(GZIP.encode(fileContents)); |
76 response = await request.close(); | 74 response = await request.close(); |
77 responseBody = await readResponse(response); | 75 responseBody = await readResponse(response); |
78 result = JSON.decode(responseBody); | 76 result = JSON.decode(responseBody); |
79 expect(result['result']['type'], equals('Success')); | 77 expect(result['result']['type'], equals('Success')); |
80 | 78 |
81 // Close the HTTP client. | 79 // Close the HTTP client. |
82 client.close(); | 80 client.close(); |
83 | 81 |
84 // Read the file back. | 82 // Read the file back. |
85 result = await vm.invokeRpcNoUpgrade('_readDevFSFile', { | 83 result = await vm.invokeRpcNoUpgrade('_readDevFSFile', { |
86 'fsName': fsId, | 84 'fsName': fsId, |
87 'uri': fileUri.toString(), | 85 'uri': fileUri.toString(), |
88 }); | 86 }); |
89 expect(result['type'], equals('FSFile')); | 87 expect(result['type'], equals('FSFile')); |
90 expect(result['fileContents'], equals(fileContentsBase64)); | 88 expect(result['fileContents'], equals(fileContentsBase64)); |
91 | 89 |
92 // Write a second file via URI. | 90 // Write a second file via URI. |
93 result = await vm.invokeRpcNoUpgrade('_writeDevFSFile', { | 91 result = await vm.invokeRpcNoUpgrade('_writeDevFSFile', { |
94 'fsName': fsId, | 92 'fsName': fsId, |
95 'uri': fileUri2.toString(), | 93 'uri': fileUri2.toString(), |
96 'fileContents': fileContentsBase64 | 94 'fileContents': fileContentsBase64 |
97 }); | 95 }); |
98 | 96 |
99 // Read the second file back. | 97 // Read the second file back. |
100 result = await vm.invokeRpcNoUpgrade('_readDevFSFile', { | 98 result = await vm.invokeRpcNoUpgrade('_readDevFSFile', { |
101 'fsName': fsId, | 99 'fsName': fsId, |
102 'uri': fileUri2.toString(), | 100 'uri': fileUri2.toString(), |
103 }); | 101 }); |
104 expect(result['type'], equals('FSFile')); | 102 expect(result['type'], equals('FSFile')); |
105 expect(result['fileContents'], equals(fileContentsBase64)); | 103 expect(result['fileContents'], equals(fileContentsBase64)); |
106 | 104 |
107 // Delete DevFS. | 105 // Delete DevFS. |
108 result = await vm.invokeRpcNoUpgrade('_deleteDevFS', { | 106 result = await vm.invokeRpcNoUpgrade('_deleteDevFS', { |
109 'fsName': fsId, | 107 'fsName': fsId, |
110 }); | 108 }); |
111 expect(result['type'], equals('Success')); | 109 expect(result['type'], equals('Success')); |
112 }, | 110 }, |
113 ]; | 111 ]; |
114 | 112 |
115 main(args) async => runVMTests(args, tests); | 113 main(args) async => runVMTests(args, tests); |
OLD | NEW |