OLD | NEW |
(Empty) | |
| 1 library googleapis.acceleratedmobilepageurl.v1.test; |
| 2 |
| 3 import "dart:core" as core; |
| 4 import "dart:collection" as collection; |
| 5 import "dart:async" as async; |
| 6 import "dart:convert" as convert; |
| 7 |
| 8 import 'package:http/http.dart' as http; |
| 9 import 'package:http/testing.dart' as http_testing; |
| 10 import 'package:unittest/unittest.dart' as unittest; |
| 11 |
| 12 import 'package:googleapis/acceleratedmobilepageurl/v1.dart' as api; |
| 13 |
| 14 class HttpServerMock extends http.BaseClient { |
| 15 core.Function _callback; |
| 16 core.bool _expectJson; |
| 17 |
| 18 void register(core.Function callback, core.bool expectJson) { |
| 19 _callback = callback; |
| 20 _expectJson = expectJson; |
| 21 } |
| 22 |
| 23 async.Future<http.StreamedResponse> send(http.BaseRequest request) { |
| 24 if (_expectJson) { |
| 25 return request.finalize() |
| 26 .transform(convert.UTF8.decoder) |
| 27 .join('') |
| 28 .then((core.String jsonString) { |
| 29 if (jsonString.isEmpty) { |
| 30 return _callback(request, null); |
| 31 } else { |
| 32 return _callback(request, convert.JSON.decode(jsonString)); |
| 33 } |
| 34 }); |
| 35 } else { |
| 36 var stream = request.finalize(); |
| 37 if (stream == null) { |
| 38 return _callback(request, []); |
| 39 } else { |
| 40 return stream.toBytes().then((data) { |
| 41 return _callback(request, data); |
| 42 }); |
| 43 } |
| 44 } |
| 45 } |
| 46 } |
| 47 |
| 48 http.StreamedResponse stringResponse( |
| 49 core.int status, core.Map headers, core.String body) { |
| 50 var stream = new async.Stream.fromIterable([convert.UTF8.encode(body)]); |
| 51 return new http.StreamedResponse(stream, status, headers: headers); |
| 52 } |
| 53 |
| 54 core.int buildCounterAmpUrl = 0; |
| 55 buildAmpUrl() { |
| 56 var o = new api.AmpUrl(); |
| 57 buildCounterAmpUrl++; |
| 58 if (buildCounterAmpUrl < 3) { |
| 59 o.ampUrl = "foo"; |
| 60 o.cdnAmpUrl = "foo"; |
| 61 o.originalUrl = "foo"; |
| 62 } |
| 63 buildCounterAmpUrl--; |
| 64 return o; |
| 65 } |
| 66 |
| 67 checkAmpUrl(api.AmpUrl o) { |
| 68 buildCounterAmpUrl++; |
| 69 if (buildCounterAmpUrl < 3) { |
| 70 unittest.expect(o.ampUrl, unittest.equals('foo')); |
| 71 unittest.expect(o.cdnAmpUrl, unittest.equals('foo')); |
| 72 unittest.expect(o.originalUrl, unittest.equals('foo')); |
| 73 } |
| 74 buildCounterAmpUrl--; |
| 75 } |
| 76 |
| 77 core.int buildCounterAmpUrlError = 0; |
| 78 buildAmpUrlError() { |
| 79 var o = new api.AmpUrlError(); |
| 80 buildCounterAmpUrlError++; |
| 81 if (buildCounterAmpUrlError < 3) { |
| 82 o.errorCode = "foo"; |
| 83 o.errorMessage = "foo"; |
| 84 o.originalUrl = "foo"; |
| 85 } |
| 86 buildCounterAmpUrlError--; |
| 87 return o; |
| 88 } |
| 89 |
| 90 checkAmpUrlError(api.AmpUrlError o) { |
| 91 buildCounterAmpUrlError++; |
| 92 if (buildCounterAmpUrlError < 3) { |
| 93 unittest.expect(o.errorCode, unittest.equals('foo')); |
| 94 unittest.expect(o.errorMessage, unittest.equals('foo')); |
| 95 unittest.expect(o.originalUrl, unittest.equals('foo')); |
| 96 } |
| 97 buildCounterAmpUrlError--; |
| 98 } |
| 99 |
| 100 buildUnnamed1207() { |
| 101 var o = new core.List<core.String>(); |
| 102 o.add("foo"); |
| 103 o.add("foo"); |
| 104 return o; |
| 105 } |
| 106 |
| 107 checkUnnamed1207(core.List<core.String> o) { |
| 108 unittest.expect(o, unittest.hasLength(2)); |
| 109 unittest.expect(o[0], unittest.equals('foo')); |
| 110 unittest.expect(o[1], unittest.equals('foo')); |
| 111 } |
| 112 |
| 113 core.int buildCounterBatchGetAmpUrlsRequest = 0; |
| 114 buildBatchGetAmpUrlsRequest() { |
| 115 var o = new api.BatchGetAmpUrlsRequest(); |
| 116 buildCounterBatchGetAmpUrlsRequest++; |
| 117 if (buildCounterBatchGetAmpUrlsRequest < 3) { |
| 118 o.urls = buildUnnamed1207(); |
| 119 } |
| 120 buildCounterBatchGetAmpUrlsRequest--; |
| 121 return o; |
| 122 } |
| 123 |
| 124 checkBatchGetAmpUrlsRequest(api.BatchGetAmpUrlsRequest o) { |
| 125 buildCounterBatchGetAmpUrlsRequest++; |
| 126 if (buildCounterBatchGetAmpUrlsRequest < 3) { |
| 127 checkUnnamed1207(o.urls); |
| 128 } |
| 129 buildCounterBatchGetAmpUrlsRequest--; |
| 130 } |
| 131 |
| 132 buildUnnamed1208() { |
| 133 var o = new core.List<api.AmpUrl>(); |
| 134 o.add(buildAmpUrl()); |
| 135 o.add(buildAmpUrl()); |
| 136 return o; |
| 137 } |
| 138 |
| 139 checkUnnamed1208(core.List<api.AmpUrl> o) { |
| 140 unittest.expect(o, unittest.hasLength(2)); |
| 141 checkAmpUrl(o[0]); |
| 142 checkAmpUrl(o[1]); |
| 143 } |
| 144 |
| 145 buildUnnamed1209() { |
| 146 var o = new core.List<api.AmpUrlError>(); |
| 147 o.add(buildAmpUrlError()); |
| 148 o.add(buildAmpUrlError()); |
| 149 return o; |
| 150 } |
| 151 |
| 152 checkUnnamed1209(core.List<api.AmpUrlError> o) { |
| 153 unittest.expect(o, unittest.hasLength(2)); |
| 154 checkAmpUrlError(o[0]); |
| 155 checkAmpUrlError(o[1]); |
| 156 } |
| 157 |
| 158 core.int buildCounterBatchGetAmpUrlsResponse = 0; |
| 159 buildBatchGetAmpUrlsResponse() { |
| 160 var o = new api.BatchGetAmpUrlsResponse(); |
| 161 buildCounterBatchGetAmpUrlsResponse++; |
| 162 if (buildCounterBatchGetAmpUrlsResponse < 3) { |
| 163 o.ampUrls = buildUnnamed1208(); |
| 164 o.urlErrors = buildUnnamed1209(); |
| 165 } |
| 166 buildCounterBatchGetAmpUrlsResponse--; |
| 167 return o; |
| 168 } |
| 169 |
| 170 checkBatchGetAmpUrlsResponse(api.BatchGetAmpUrlsResponse o) { |
| 171 buildCounterBatchGetAmpUrlsResponse++; |
| 172 if (buildCounterBatchGetAmpUrlsResponse < 3) { |
| 173 checkUnnamed1208(o.ampUrls); |
| 174 checkUnnamed1209(o.urlErrors); |
| 175 } |
| 176 buildCounterBatchGetAmpUrlsResponse--; |
| 177 } |
| 178 |
| 179 |
| 180 main() { |
| 181 unittest.group("obj-schema-AmpUrl", () { |
| 182 unittest.test("to-json--from-json", () { |
| 183 var o = buildAmpUrl(); |
| 184 var od = new api.AmpUrl.fromJson(o.toJson()); |
| 185 checkAmpUrl(od); |
| 186 }); |
| 187 }); |
| 188 |
| 189 |
| 190 unittest.group("obj-schema-AmpUrlError", () { |
| 191 unittest.test("to-json--from-json", () { |
| 192 var o = buildAmpUrlError(); |
| 193 var od = new api.AmpUrlError.fromJson(o.toJson()); |
| 194 checkAmpUrlError(od); |
| 195 }); |
| 196 }); |
| 197 |
| 198 |
| 199 unittest.group("obj-schema-BatchGetAmpUrlsRequest", () { |
| 200 unittest.test("to-json--from-json", () { |
| 201 var o = buildBatchGetAmpUrlsRequest(); |
| 202 var od = new api.BatchGetAmpUrlsRequest.fromJson(o.toJson()); |
| 203 checkBatchGetAmpUrlsRequest(od); |
| 204 }); |
| 205 }); |
| 206 |
| 207 |
| 208 unittest.group("obj-schema-BatchGetAmpUrlsResponse", () { |
| 209 unittest.test("to-json--from-json", () { |
| 210 var o = buildBatchGetAmpUrlsResponse(); |
| 211 var od = new api.BatchGetAmpUrlsResponse.fromJson(o.toJson()); |
| 212 checkBatchGetAmpUrlsResponse(od); |
| 213 }); |
| 214 }); |
| 215 |
| 216 |
| 217 unittest.group("resource-AmpUrlsResourceApi", () { |
| 218 unittest.test("method--batchGet", () { |
| 219 |
| 220 var mock = new HttpServerMock(); |
| 221 api.AmpUrlsResourceApi res = new api.AcceleratedmobilepageurlApi(mock).amp
Urls; |
| 222 var arg_request = buildBatchGetAmpUrlsRequest(); |
| 223 mock.register(unittest.expectAsync((http.BaseRequest req, json) { |
| 224 var obj = new api.BatchGetAmpUrlsRequest.fromJson(json); |
| 225 checkBatchGetAmpUrlsRequest(obj); |
| 226 |
| 227 var path = (req.url).path; |
| 228 var pathOffset = 0; |
| 229 var index; |
| 230 var subPart; |
| 231 unittest.expect(path.substring(pathOffset, pathOffset + 1), unittest.equ
als("/")); |
| 232 pathOffset += 1; |
| 233 unittest.expect(path.substring(pathOffset, pathOffset + 19), unittest.eq
uals("v1/ampUrls:batchGet")); |
| 234 pathOffset += 19; |
| 235 |
| 236 var query = (req.url).query; |
| 237 var queryOffset = 0; |
| 238 var queryMap = {}; |
| 239 addQueryParam(n, v) => queryMap.putIfAbsent(n, () => []).add(v); |
| 240 parseBool(n) { |
| 241 if (n == "true") return true; |
| 242 if (n == "false") return false; |
| 243 if (n == null) return null; |
| 244 throw new core.ArgumentError("Invalid boolean: $n"); |
| 245 } |
| 246 if (query.length > 0) { |
| 247 for (var part in query.split("&")) { |
| 248 var keyvalue = part.split("="); |
| 249 addQueryParam(core.Uri.decodeQueryComponent(keyvalue[0]), core.Uri.d
ecodeQueryComponent(keyvalue[1])); |
| 250 } |
| 251 } |
| 252 |
| 253 |
| 254 var h = { |
| 255 "content-type" : "application/json; charset=utf-8", |
| 256 }; |
| 257 var resp = convert.JSON.encode(buildBatchGetAmpUrlsResponse()); |
| 258 return new async.Future.value(stringResponse(200, h, resp)); |
| 259 }), true); |
| 260 res.batchGet(arg_request).then(unittest.expectAsync(((api.BatchGetAmpUrlsR
esponse response) { |
| 261 checkBatchGetAmpUrlsResponse(response); |
| 262 }))); |
| 263 }); |
| 264 |
| 265 }); |
| 266 |
| 267 |
| 268 } |
| 269 |
OLD | NEW |