OLD | NEW |
1 library googleapis.playcustomapp.v1.test; | 1 library googleapis.playcustomapp.v1.test; |
2 | 2 |
3 import "dart:core" as core; | 3 import "dart:core" as core; |
4 import "dart:collection" as collection; | |
5 import "dart:async" as async; | 4 import "dart:async" as async; |
6 import "dart:convert" as convert; | 5 import "dart:convert" as convert; |
7 | 6 |
8 import 'package:http/http.dart' as http; | 7 import 'package:http/http.dart' as http; |
9 import 'package:http/testing.dart' as http_testing; | |
10 import 'package:test/test.dart' as unittest; | 8 import 'package:test/test.dart' as unittest; |
11 | 9 |
12 import 'package:googleapis/playcustomapp/v1.dart' as api; | 10 import 'package:googleapis/playcustomapp/v1.dart' as api; |
13 | 11 |
14 class HttpServerMock extends http.BaseClient { | 12 class HttpServerMock extends http.BaseClient { |
15 core.Function _callback; | 13 core.Function _callback; |
16 core.bool _expectJson; | 14 core.bool _expectJson; |
17 | 15 |
18 void register(core.Function callback, core.bool expectJson) { | 16 void register(core.Function callback, core.bool expectJson) { |
19 _callback = callback; | 17 _callback = callback; |
20 _expectJson = expectJson; | 18 _expectJson = expectJson; |
21 } | 19 } |
22 | 20 |
23 async.Future<http.StreamedResponse> send(http.BaseRequest request) { | 21 async.Future<http.StreamedResponse> send(http.BaseRequest request) { |
24 if (_expectJson) { | 22 if (_expectJson) { |
25 return request.finalize() | 23 return request |
| 24 .finalize() |
26 .transform(convert.UTF8.decoder) | 25 .transform(convert.UTF8.decoder) |
27 .join('') | 26 .join('') |
28 .then((core.String jsonString) { | 27 .then((core.String jsonString) { |
29 if (jsonString.isEmpty) { | 28 if (jsonString.isEmpty) { |
30 return _callback(request, null); | 29 return _callback(request, null); |
31 } else { | 30 } else { |
32 return _callback(request, convert.JSON.decode(jsonString)); | 31 return _callback(request, convert.JSON.decode(jsonString)); |
33 } | 32 } |
34 }); | 33 }); |
35 } else { | 34 } else { |
36 var stream = request.finalize(); | 35 var stream = request.finalize(); |
37 if (stream == null) { | 36 if (stream == null) { |
38 return _callback(request, []); | 37 return _callback(request, []); |
39 } else { | 38 } else { |
40 return stream.toBytes().then((data) { | 39 return stream.toBytes().then((data) { |
41 return _callback(request, data); | 40 return _callback(request, data); |
42 }); | 41 }); |
43 } | 42 } |
44 } | 43 } |
45 } | 44 } |
46 } | 45 } |
47 | 46 |
48 http.StreamedResponse stringResponse( | 47 http.StreamedResponse stringResponse(core.int status, |
49 core.int status, core.Map<core.String, core.String> headers, core.String bod
y) { | 48 core.Map<core.String, core.String> headers, core.String body) { |
50 var stream = new async.Stream.fromIterable([convert.UTF8.encode(body)]); | 49 var stream = new async.Stream.fromIterable([convert.UTF8.encode(body)]); |
51 return new http.StreamedResponse(stream, status, headers: headers); | 50 return new http.StreamedResponse(stream, status, headers: headers); |
52 } | 51 } |
53 | 52 |
54 core.int buildCounterCustomApp = 0; | 53 core.int buildCounterCustomApp = 0; |
55 buildCustomApp() { | 54 buildCustomApp() { |
56 var o = new api.CustomApp(); | 55 var o = new api.CustomApp(); |
57 buildCounterCustomApp++; | 56 buildCounterCustomApp++; |
58 if (buildCounterCustomApp < 3) { | 57 if (buildCounterCustomApp < 3) { |
59 o.languageCode = "foo"; | 58 o.languageCode = "foo"; |
60 o.title = "foo"; | 59 o.title = "foo"; |
61 } | 60 } |
62 buildCounterCustomApp--; | 61 buildCounterCustomApp--; |
63 return o; | 62 return o; |
64 } | 63 } |
65 | 64 |
66 checkCustomApp(api.CustomApp o) { | 65 checkCustomApp(api.CustomApp o) { |
67 buildCounterCustomApp++; | 66 buildCounterCustomApp++; |
68 if (buildCounterCustomApp < 3) { | 67 if (buildCounterCustomApp < 3) { |
69 unittest.expect(o.languageCode, unittest.equals('foo')); | 68 unittest.expect(o.languageCode, unittest.equals('foo')); |
70 unittest.expect(o.title, unittest.equals('foo')); | 69 unittest.expect(o.title, unittest.equals('foo')); |
71 } | 70 } |
72 buildCounterCustomApp--; | 71 buildCounterCustomApp--; |
73 } | 72 } |
74 | 73 |
75 | |
76 main() { | 74 main() { |
77 unittest.group("obj-schema-CustomApp", () { | 75 unittest.group("obj-schema-CustomApp", () { |
78 unittest.test("to-json--from-json", () { | 76 unittest.test("to-json--from-json", () { |
79 var o = buildCustomApp(); | 77 var o = buildCustomApp(); |
80 var od = new api.CustomApp.fromJson(o.toJson()); | 78 var od = new api.CustomApp.fromJson(o.toJson()); |
81 checkCustomApp(od); | 79 checkCustomApp(od); |
82 }); | 80 }); |
83 }); | 81 }); |
84 | 82 |
85 | |
86 unittest.group("resource-AccountsCustomAppsResourceApi", () { | 83 unittest.group("resource-AccountsCustomAppsResourceApi", () { |
87 unittest.test("method--create", () { | 84 unittest.test("method--create", () { |
88 // TODO: Implement tests for media upload; | 85 // TODO: Implement tests for media upload; |
89 // TODO: Implement tests for media download; | 86 // TODO: Implement tests for media download; |
90 | 87 |
91 var mock = new HttpServerMock(); | 88 var mock = new HttpServerMock(); |
92 api.AccountsCustomAppsResourceApi res = new api.PlaycustomappApi(mock).acc
ounts.customApps; | 89 api.AccountsCustomAppsResourceApi res = |
| 90 new api.PlaycustomappApi(mock).accounts.customApps; |
93 var arg_request = buildCustomApp(); | 91 var arg_request = buildCustomApp(); |
94 var arg_account = "foo"; | 92 var arg_account = "foo"; |
95 mock.register(unittest.expectAsync2((http.BaseRequest req, json) { | 93 mock.register(unittest.expectAsync2((http.BaseRequest req, json) { |
96 var obj = new api.CustomApp.fromJson(json); | 94 var obj = new api.CustomApp.fromJson(json); |
97 checkCustomApp(obj); | 95 checkCustomApp(obj); |
98 | 96 |
99 var path = (req.url).path; | 97 var path = (req.url).path; |
100 var pathOffset = 0; | 98 var pathOffset = 0; |
101 var index; | 99 var index; |
102 var subPart; | 100 var subPart; |
103 unittest.expect(path.substring(pathOffset, pathOffset + 1), unittest.equ
als("/")); | 101 unittest.expect( |
| 102 path.substring(pathOffset, pathOffset + 1), unittest.equals("/")); |
104 pathOffset += 1; | 103 pathOffset += 1; |
105 | 104 |
106 var query = (req.url).query; | 105 var query = (req.url).query; |
107 var queryOffset = 0; | 106 var queryOffset = 0; |
108 var queryMap = {}; | 107 var queryMap = {}; |
109 addQueryParam(n, v) => queryMap.putIfAbsent(n, () => []).add(v); | 108 addQueryParam(n, v) => queryMap.putIfAbsent(n, () => []).add(v); |
110 parseBool(n) { | 109 parseBool(n) { |
111 if (n == "true") return true; | 110 if (n == "true") return true; |
112 if (n == "false") return false; | 111 if (n == "false") return false; |
113 if (n == null) return null; | 112 if (n == null) return null; |
114 throw new core.ArgumentError("Invalid boolean: $n"); | 113 throw new core.ArgumentError("Invalid boolean: $n"); |
115 } | 114 } |
| 115 |
116 if (query.length > 0) { | 116 if (query.length > 0) { |
117 for (var part in query.split("&")) { | 117 for (var part in query.split("&")) { |
118 var keyvalue = part.split("="); | 118 var keyvalue = part.split("="); |
119 addQueryParam(core.Uri.decodeQueryComponent(keyvalue[0]), core.Uri.d
ecodeQueryComponent(keyvalue[1])); | 119 addQueryParam(core.Uri.decodeQueryComponent(keyvalue[0]), |
| 120 core.Uri.decodeQueryComponent(keyvalue[1])); |
120 } | 121 } |
121 } | 122 } |
122 | 123 |
123 | |
124 var h = { | 124 var h = { |
125 "content-type" : "application/json; charset=utf-8", | 125 "content-type": "application/json; charset=utf-8", |
126 }; | 126 }; |
127 var resp = convert.JSON.encode(buildCustomApp()); | 127 var resp = convert.JSON.encode(buildCustomApp()); |
128 return new async.Future.value(stringResponse(200, h, resp)); | 128 return new async.Future.value(stringResponse(200, h, resp)); |
129 }), true); | 129 }), true); |
130 res.create(arg_request, arg_account).then(unittest.expectAsync1(((api.Cust
omApp response) { | 130 res |
| 131 .create(arg_request, arg_account) |
| 132 .then(unittest.expectAsync1(((api.CustomApp response) { |
131 checkCustomApp(response); | 133 checkCustomApp(response); |
132 }))); | 134 }))); |
133 }); | 135 }); |
134 | |
135 }); | 136 }); |
136 | |
137 | |
138 } | 137 } |
139 | |
OLD | NEW |