diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2020-08-05 22:45:32 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-08-05 22:45:32 +0000 |
commit | a5e4f4d2969520f4563ec0d66cbe469c49ee38bc (patch) | |
tree | 8a0d6fd78eb610653f12ded770595c80f1e89a61 /catapult/third_party/polymer/components/iron-ajax/test/iron-request.html | |
parent | 5e1447373f2b6aaefa0e866c5907a291e7071a3d (diff) | |
parent | 5338649baa46daab43dd19d939fc1f016d400a94 (diff) | |
download | chromium-trace-a5e4f4d2969520f4563ec0d66cbe469c49ee38bc.tar.gz |
Merge changes Ie9f01eed,I3acb8a0d am: fb769a1607 am: 316160b179 am: 3fd616f492 am: 5338649baa
Original change: https://android-review.googlesource.com/c/platform/external/chromium-trace/+/1392438
Change-Id: I01f6e9f32cc3210fbc5f6f645a77b74316ecfb3b
Diffstat (limited to 'catapult/third_party/polymer/components/iron-ajax/test/iron-request.html')
-rw-r--r-- | catapult/third_party/polymer/components/iron-ajax/test/iron-request.html | 368 |
1 files changed, 368 insertions, 0 deletions
diff --git a/catapult/third_party/polymer/components/iron-ajax/test/iron-request.html b/catapult/third_party/polymer/components/iron-ajax/test/iron-request.html new file mode 100644 index 00000000..6161bb5f --- /dev/null +++ b/catapult/third_party/polymer/components/iron-ajax/test/iron-request.html @@ -0,0 +1,368 @@ +<!doctype html> +<!-- +@license +Copyright (c) 2015 The Polymer Project Authors. All rights reserved. +This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt +The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt +The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt +Code distributed by Google as part of the polymer project is also +subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt +--> +<html> +<head> + <title>iron-request</title> + + <script src="../../webcomponentsjs/webcomponents-lite.js"></script> + <script src="../../web-component-tester/browser.js"></script> + + <link rel="import" href="../../polymer/polymer.html"> + <link rel="import" href="../../promise-polyfill/promise-polyfill.html"> + <link rel="import" href="../iron-request.html"> +</head> +<body> + <test-fixture id="TrivialRequest"> + <template> + <iron-request></iron-request> + </template> + </test-fixture> + <script> + suite('<iron-request>', function() { + var jsonResponseHeaders; + var successfulRequestOptions; + var request; + var server; + + setup(function() { + jsonResponseHeaders = { + 'Content-Type': 'application/json' + }; + server = sinon.fakeServer.create(); + server.respondWith('GET', '/responds_to_get_with_json', [ + 200, + jsonResponseHeaders, + '{"success":true}' + ]); + + server.respondWith('GET', '/responds_to_get_with_prefixed_json', [ + 200, + jsonResponseHeaders, + '])}while(1);</x>{"success":true}' + ]); + + server.respondWith('GET', '/responds_to_get_with_500', [ + 500, + {}, + '' + ]); + + server.respondWith('GET', '/responds_to_get_with_100', [ + 100, + {}, + '' + ]); + + server.respondWith('GET', '/responds_to_get_with_0', [ + 0, + jsonResponseHeaders, + '{"success":true}' + ]); + + + request = fixture('TrivialRequest'); + successfulRequestOptions = { + url: '/responds_to_get_with_json' + }; + + synchronousSuccessfulRequestOptions = { + url: '/responds_to_get_with_json', + async: false, + timeout: 100 + }; + + asynchronousSuccessfulRequestOptions = { + url: '/responds_to_get_with_json', + async: true, + timeout: 100 + }; + }); + + teardown(function() { + server.restore(); + }); + + suite('basic usage', function() { + test('creates network requests, requiring only `url`', function() { + request.send(successfulRequestOptions); + + server.respond(); + + expect(request.response).to.be.ok; + }); + + test('timeout not set if synchronous', function() { + request.send(synchronousSuccessfulRequestOptions); + + expect(request.xhr.async).to.be.eql(false); + expect(request.xhr.timeout).to.be.eql(undefined); + }); + + test('timeout set if asynchronous', function() { + request.send(asynchronousSuccessfulRequestOptions); + + expect(request.xhr.async).to.be.eql(true); + expect(request.xhr.timeout).to.be.eql(100); + }); + + test('sets async to true by default', function() { + request.send(successfulRequestOptions); + expect(request.xhr.async).to.be.eql(true); + }); + + test('can be aborted', function() { + request.send(successfulRequestOptions); + + request.abort(); + + server.respond(); + + return request.completes.then(function() { + throw new Error('Request did not abort appropriately!'); + }).catch(function(e) { + expect(request.response).to.not.be.ok; + }); + }); + + test('can be aborted with request element', function() { + var options = { + url: successfulRequestOptions.url, + rejectWithRequest: true + }; + request.send(options); + + request.abort(); + + server.respond(); + + return request.completes.then(function() { + throw new Error('Request did not abort appropriately!'); + }).catch(function(e) { + expect(e.error).to.be.instanceof(Error); + expect(e.request).to.deep.equal(request); + }); + }); + + test('default responseType is text', function() { + request.send(successfulRequestOptions); + server.respond(); + + return request.completes.then(function() { + expect(request.response).to.be.an('string') + }); + }); + + test('default responseType of text is not applied, when async is false', function() { + var options = Object.create(successfulRequestOptions); + options.async = false; + + request.send(options); + server.respond(); + + return request.completes.then(function() { + expect(request.xhr.responseType).to.be.empty; + }); + }); + + test('responseType can be configured via handleAs option', function() { + var options = Object.create(successfulRequestOptions); + options.handleAs = 'json'; + + request.send(options); + expect(server.requests.length).to.be.equal(1); + expect(server.requests[0].requestHeaders['accept']).to.be.equal( + 'application/json'); + server.respond(); + + return request.completes.then(function() { + expect(request.response).to.be.an('object'); + }); + }); + + test('setting jsonPrefix correctly strips it from the response', function() { + var options = { + url: '/responds_to_get_with_prefixed_json', + handleAs: 'json', + jsonPrefix: '])}while(1);</x>' + }; + + request.send(options); + expect(server.requests.length).to.be.equal(1); + expect(server.requests[0].requestHeaders['accept']).to.be.equal( + 'application/json'); + server.respond(); + + return request.completes.then(function() { + expect(request.response).to.deep.eq({success: true}); + }); + }); + + test('responseType cannot be configured via handleAs option, when async is false', function() { + var options = Object.create(successfulRequestOptions); + options.handleAs = 'json'; + options.async = false; + + request.send(options); + expect(server.requests.length).to.be.equal(1); + expect(server.requests[0].requestHeaders['accept']).to.be.equal( + 'application/json'); + server.respond(); + + return request.completes.then(function() { + expect(request.response).to.be.a('string'); + }); + }); + + test('headers are sent up', function() { + var options = Object.create(successfulRequestOptions); + options.headers = { + 'foo': 'bar', + 'accept': 'this should override the default' + }; + request.send(options); + expect(server.requests.length).to.be.equal(1); + var fakeXhr = server.requests[0] + expect(fakeXhr.requestHeaders['foo']).to.be.equal( + 'bar'); + expect(fakeXhr.requestHeaders['accept']).to.be.equal( + 'this should override the default'); + }); + + test('headers are deduped by lowercasing', function() { + var options = Object.create(successfulRequestOptions); + options.headers = { + 'foo': 'bar', + 'Foo': 'bar', + 'fOo': 'bar', + 'Accept': 'this should also override the default' + }; + request.send(options); + expect(server.requests.length).to.be.equal(1); + var fakeXhr = server.requests[0] + expect(Object.keys(fakeXhr.requestHeaders).length).to.be.equal(2); + expect(fakeXhr.requestHeaders['foo']).to.be.equal( + 'bar'); + expect(fakeXhr.requestHeaders['accept']).to.be.equal( + 'this should also override the default'); + }); + }); + + suite('special cases', function() { + test('treats status code 0 as success, though the outcome is ambiguous', function() { + // Note: file:// status code will probably be 0 no matter what happened. + request.send({ + url: '/responds_to_get_with_0' + }); + + server.respond(); + + expect(request.succeeded).to.be.equal(true); + }); + + test('special form characters', function() { + var testCases = [ + { + test: null, + answer: '' + }, + { + test: undefined, + answer: '' + }, + { + test: NaN, + answer: 'NaN' + }, + { + test: new String('\n\r\n\r'), + answer: '%0D%0A%0D%0A%0D' // \r\n\r\n\r + }, + { + test: 0, + answer: '0' + }, + { + test: new String('hello world'), + answer: 'hello+world' + } + ]; + + var testCase; + for (var i = 0; i < testCases.length; i++) { + testCase = testCases[i]; + var encoded = request._wwwFormUrlEncodePiece(testCase.test); + + expect(encoded).to.be.equal(testCase.answer); + } + }); + }); + + suite('errors', function() { + test('treats status codes between 1 and 199 as errors', function() { + request.send({ + url: '/responds_to_get_with_100' + }); + + server.respond(); + + expect(request.succeeded).to.be.equal(false); + }); + + test('treats status codes between 300 and ∞ as errors', function() { + request.send({ + url: '/responds_to_get_with_500' + }); + + server.respond(); + + expect(request.succeeded).to.be.equal(false); + }); + }); + + suite('status codes', function() { + test('status and statusText is set after a ambiguous request', function() { + request.send({ + url: '/responds_to_get_with_0' + }); + + server.respond(); + + expect(request.status).to.be.equal(0); + expect(request.statusText).to.be.equal(''); + }); + + test('status and statusText is set after a request that succeeded', function() { + request.send({ + url: '/responds_to_get_with_json' + }); + + server.respond(); + + expect(request.status).to.be.equal(200); + expect(request.statusText).to.be.equal('OK'); + }); + + test('status and statusText is set after a request that failed', function() { + request.send({ + url: '/responds_to_get_with_500' + }); + + server.respond(); + + expect(request.status).to.be.equal(500); + expect(request.statusText).to.be.equal('Internal Server Error'); + }); + }); + }); + </script> + +</body> +</html> |