OLD | NEW |
1 library googleapis.webfonts.v1.test; | 1 library googleapis.webfonts.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/webfonts/v1.dart' as api; | 10 import 'package:googleapis/webfonts/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 buildUnnamed1690() { | 53 buildUnnamed1686() { |
55 var o = new core.Map<core.String, core.String>(); | 54 var o = new core.Map<core.String, core.String>(); |
56 o["x"] = "foo"; | 55 o["x"] = "foo"; |
57 o["y"] = "foo"; | 56 o["y"] = "foo"; |
58 return o; | 57 return o; |
59 } | 58 } |
60 | 59 |
61 checkUnnamed1690(core.Map<core.String, core.String> o) { | 60 checkUnnamed1686(core.Map<core.String, core.String> o) { |
62 unittest.expect(o, unittest.hasLength(2)); | 61 unittest.expect(o, unittest.hasLength(2)); |
63 unittest.expect(o["x"], unittest.equals('foo')); | 62 unittest.expect(o["x"], unittest.equals('foo')); |
64 unittest.expect(o["y"], unittest.equals('foo')); | 63 unittest.expect(o["y"], unittest.equals('foo')); |
65 } | 64 } |
66 | 65 |
67 buildUnnamed1691() { | 66 buildUnnamed1687() { |
68 var o = new core.List<core.String>(); | 67 var o = new core.List<core.String>(); |
69 o.add("foo"); | 68 o.add("foo"); |
70 o.add("foo"); | 69 o.add("foo"); |
71 return o; | 70 return o; |
72 } | 71 } |
73 | 72 |
74 checkUnnamed1691(core.List<core.String> o) { | 73 checkUnnamed1687(core.List<core.String> o) { |
75 unittest.expect(o, unittest.hasLength(2)); | 74 unittest.expect(o, unittest.hasLength(2)); |
76 unittest.expect(o[0], unittest.equals('foo')); | 75 unittest.expect(o[0], unittest.equals('foo')); |
77 unittest.expect(o[1], unittest.equals('foo')); | 76 unittest.expect(o[1], unittest.equals('foo')); |
78 } | 77 } |
79 | 78 |
80 buildUnnamed1692() { | 79 buildUnnamed1688() { |
81 var o = new core.List<core.String>(); | 80 var o = new core.List<core.String>(); |
82 o.add("foo"); | 81 o.add("foo"); |
83 o.add("foo"); | 82 o.add("foo"); |
84 return o; | 83 return o; |
85 } | 84 } |
86 | 85 |
87 checkUnnamed1692(core.List<core.String> o) { | 86 checkUnnamed1688(core.List<core.String> o) { |
88 unittest.expect(o, unittest.hasLength(2)); | 87 unittest.expect(o, unittest.hasLength(2)); |
89 unittest.expect(o[0], unittest.equals('foo')); | 88 unittest.expect(o[0], unittest.equals('foo')); |
90 unittest.expect(o[1], unittest.equals('foo')); | 89 unittest.expect(o[1], unittest.equals('foo')); |
91 } | 90 } |
92 | 91 |
93 core.int buildCounterWebfont = 0; | 92 core.int buildCounterWebfont = 0; |
94 buildWebfont() { | 93 buildWebfont() { |
95 var o = new api.Webfont(); | 94 var o = new api.Webfont(); |
96 buildCounterWebfont++; | 95 buildCounterWebfont++; |
97 if (buildCounterWebfont < 3) { | 96 if (buildCounterWebfont < 3) { |
98 o.category = "foo"; | 97 o.category = "foo"; |
99 o.family = "foo"; | 98 o.family = "foo"; |
100 o.files = buildUnnamed1690(); | 99 o.files = buildUnnamed1686(); |
101 o.kind = "foo"; | 100 o.kind = "foo"; |
102 o.lastModified = core.DateTime.parse("2002-02-27T14:01:02Z"); | 101 o.lastModified = core.DateTime.parse("2002-02-27T14:01:02Z"); |
103 o.subsets = buildUnnamed1691(); | 102 o.subsets = buildUnnamed1687(); |
104 o.variants = buildUnnamed1692(); | 103 o.variants = buildUnnamed1688(); |
105 o.version = "foo"; | 104 o.version = "foo"; |
106 } | 105 } |
107 buildCounterWebfont--; | 106 buildCounterWebfont--; |
108 return o; | 107 return o; |
109 } | 108 } |
110 | 109 |
111 checkWebfont(api.Webfont o) { | 110 checkWebfont(api.Webfont o) { |
112 buildCounterWebfont++; | 111 buildCounterWebfont++; |
113 if (buildCounterWebfont < 3) { | 112 if (buildCounterWebfont < 3) { |
114 unittest.expect(o.category, unittest.equals('foo')); | 113 unittest.expect(o.category, unittest.equals('foo')); |
115 unittest.expect(o.family, unittest.equals('foo')); | 114 unittest.expect(o.family, unittest.equals('foo')); |
116 checkUnnamed1690(o.files); | 115 checkUnnamed1686(o.files); |
117 unittest.expect(o.kind, unittest.equals('foo')); | 116 unittest.expect(o.kind, unittest.equals('foo')); |
118 unittest.expect(o.lastModified, unittest.equals(core.DateTime.parse("2002-02
-27T00:00:00"))); | 117 unittest.expect(o.lastModified, |
119 checkUnnamed1691(o.subsets); | 118 unittest.equals(core.DateTime.parse("2002-02-27T00:00:00"))); |
120 checkUnnamed1692(o.variants); | 119 checkUnnamed1687(o.subsets); |
| 120 checkUnnamed1688(o.variants); |
121 unittest.expect(o.version, unittest.equals('foo')); | 121 unittest.expect(o.version, unittest.equals('foo')); |
122 } | 122 } |
123 buildCounterWebfont--; | 123 buildCounterWebfont--; |
124 } | 124 } |
125 | 125 |
126 buildUnnamed1693() { | 126 buildUnnamed1689() { |
127 var o = new core.List<api.Webfont>(); | 127 var o = new core.List<api.Webfont>(); |
128 o.add(buildWebfont()); | 128 o.add(buildWebfont()); |
129 o.add(buildWebfont()); | 129 o.add(buildWebfont()); |
130 return o; | 130 return o; |
131 } | 131 } |
132 | 132 |
133 checkUnnamed1693(core.List<api.Webfont> o) { | 133 checkUnnamed1689(core.List<api.Webfont> o) { |
134 unittest.expect(o, unittest.hasLength(2)); | 134 unittest.expect(o, unittest.hasLength(2)); |
135 checkWebfont(o[0]); | 135 checkWebfont(o[0]); |
136 checkWebfont(o[1]); | 136 checkWebfont(o[1]); |
137 } | 137 } |
138 | 138 |
139 core.int buildCounterWebfontList = 0; | 139 core.int buildCounterWebfontList = 0; |
140 buildWebfontList() { | 140 buildWebfontList() { |
141 var o = new api.WebfontList(); | 141 var o = new api.WebfontList(); |
142 buildCounterWebfontList++; | 142 buildCounterWebfontList++; |
143 if (buildCounterWebfontList < 3) { | 143 if (buildCounterWebfontList < 3) { |
144 o.items = buildUnnamed1693(); | 144 o.items = buildUnnamed1689(); |
145 o.kind = "foo"; | 145 o.kind = "foo"; |
146 } | 146 } |
147 buildCounterWebfontList--; | 147 buildCounterWebfontList--; |
148 return o; | 148 return o; |
149 } | 149 } |
150 | 150 |
151 checkWebfontList(api.WebfontList o) { | 151 checkWebfontList(api.WebfontList o) { |
152 buildCounterWebfontList++; | 152 buildCounterWebfontList++; |
153 if (buildCounterWebfontList < 3) { | 153 if (buildCounterWebfontList < 3) { |
154 checkUnnamed1693(o.items); | 154 checkUnnamed1689(o.items); |
155 unittest.expect(o.kind, unittest.equals('foo')); | 155 unittest.expect(o.kind, unittest.equals('foo')); |
156 } | 156 } |
157 buildCounterWebfontList--; | 157 buildCounterWebfontList--; |
158 } | 158 } |
159 | 159 |
160 | |
161 main() { | 160 main() { |
162 unittest.group("obj-schema-Webfont", () { | 161 unittest.group("obj-schema-Webfont", () { |
163 unittest.test("to-json--from-json", () { | 162 unittest.test("to-json--from-json", () { |
164 var o = buildWebfont(); | 163 var o = buildWebfont(); |
165 var od = new api.Webfont.fromJson(o.toJson()); | 164 var od = new api.Webfont.fromJson(o.toJson()); |
166 checkWebfont(od); | 165 checkWebfont(od); |
167 }); | 166 }); |
168 }); | 167 }); |
169 | 168 |
170 | |
171 unittest.group("obj-schema-WebfontList", () { | 169 unittest.group("obj-schema-WebfontList", () { |
172 unittest.test("to-json--from-json", () { | 170 unittest.test("to-json--from-json", () { |
173 var o = buildWebfontList(); | 171 var o = buildWebfontList(); |
174 var od = new api.WebfontList.fromJson(o.toJson()); | 172 var od = new api.WebfontList.fromJson(o.toJson()); |
175 checkWebfontList(od); | 173 checkWebfontList(od); |
176 }); | 174 }); |
177 }); | 175 }); |
178 | 176 |
179 | |
180 unittest.group("resource-WebfontsResourceApi", () { | 177 unittest.group("resource-WebfontsResourceApi", () { |
181 unittest.test("method--list", () { | 178 unittest.test("method--list", () { |
182 | |
183 var mock = new HttpServerMock(); | 179 var mock = new HttpServerMock(); |
184 api.WebfontsResourceApi res = new api.WebfontsApi(mock).webfonts; | 180 api.WebfontsResourceApi res = new api.WebfontsApi(mock).webfonts; |
185 var arg_sort = "foo"; | 181 var arg_sort = "foo"; |
186 mock.register(unittest.expectAsync2((http.BaseRequest req, json) { | 182 mock.register(unittest.expectAsync2((http.BaseRequest req, json) { |
187 var path = (req.url).path; | 183 var path = (req.url).path; |
188 var pathOffset = 0; | 184 var pathOffset = 0; |
189 var index; | 185 var index; |
190 var subPart; | 186 var subPart; |
191 unittest.expect(path.substring(pathOffset, pathOffset + 1), unittest.equ
als("/")); | 187 unittest.expect( |
| 188 path.substring(pathOffset, pathOffset + 1), unittest.equals("/")); |
192 pathOffset += 1; | 189 pathOffset += 1; |
193 unittest.expect(path.substring(pathOffset, pathOffset + 12), unittest.eq
uals("webfonts/v1/")); | 190 unittest.expect(path.substring(pathOffset, pathOffset + 12), |
| 191 unittest.equals("webfonts/v1/")); |
194 pathOffset += 12; | 192 pathOffset += 12; |
195 unittest.expect(path.substring(pathOffset, pathOffset + 8), unittest.equ
als("webfonts")); | 193 unittest.expect(path.substring(pathOffset, pathOffset + 8), |
| 194 unittest.equals("webfonts")); |
196 pathOffset += 8; | 195 pathOffset += 8; |
197 | 196 |
198 var query = (req.url).query; | 197 var query = (req.url).query; |
199 var queryOffset = 0; | 198 var queryOffset = 0; |
200 var queryMap = {}; | 199 var queryMap = {}; |
201 addQueryParam(n, v) => queryMap.putIfAbsent(n, () => []).add(v); | 200 addQueryParam(n, v) => queryMap.putIfAbsent(n, () => []).add(v); |
202 parseBool(n) { | 201 parseBool(n) { |
203 if (n == "true") return true; | 202 if (n == "true") return true; |
204 if (n == "false") return false; | 203 if (n == "false") return false; |
205 if (n == null) return null; | 204 if (n == null) return null; |
206 throw new core.ArgumentError("Invalid boolean: $n"); | 205 throw new core.ArgumentError("Invalid boolean: $n"); |
207 } | 206 } |
| 207 |
208 if (query.length > 0) { | 208 if (query.length > 0) { |
209 for (var part in query.split("&")) { | 209 for (var part in query.split("&")) { |
210 var keyvalue = part.split("="); | 210 var keyvalue = part.split("="); |
211 addQueryParam(core.Uri.decodeQueryComponent(keyvalue[0]), core.Uri.d
ecodeQueryComponent(keyvalue[1])); | 211 addQueryParam(core.Uri.decodeQueryComponent(keyvalue[0]), |
| 212 core.Uri.decodeQueryComponent(keyvalue[1])); |
212 } | 213 } |
213 } | 214 } |
214 unittest.expect(queryMap["sort"].first, unittest.equals(arg_sort)); | 215 unittest.expect(queryMap["sort"].first, unittest.equals(arg_sort)); |
215 | 216 |
216 | |
217 var h = { | 217 var h = { |
218 "content-type" : "application/json; charset=utf-8", | 218 "content-type": "application/json; charset=utf-8", |
219 }; | 219 }; |
220 var resp = convert.JSON.encode(buildWebfontList()); | 220 var resp = convert.JSON.encode(buildWebfontList()); |
221 return new async.Future.value(stringResponse(200, h, resp)); | 221 return new async.Future.value(stringResponse(200, h, resp)); |
222 }), true); | 222 }), true); |
223 res.list(sort: arg_sort).then(unittest.expectAsync1(((api.WebfontList resp
onse) { | 223 res |
| 224 .list(sort: arg_sort) |
| 225 .then(unittest.expectAsync1(((api.WebfontList response) { |
224 checkWebfontList(response); | 226 checkWebfontList(response); |
225 }))); | 227 }))); |
226 }); | 228 }); |
227 | |
228 }); | 229 }); |
229 | |
230 | |
231 } | 230 } |
232 | |
OLD | NEW |