3 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
4 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7 Code distributed by Google as part of the polymer project is also
8 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
12 <title>core-ajax
</title>
14 <script src=
"../../webcomponentsjs/webcomponents.js"></script>
15 <script src=
"../../web-component-tester/browser.js"></script>
17 <link rel=
"import" href=
"../core-ajax.html">
21 <core-ajax></core-ajax>
24 suite('core-ajax', function() {
25 var xhr
, requests
, ajax
;
26 suiteSetup(function() {
27 xhr
= sinon
.useFakeXMLHttpRequest();
28 ajax
= document
.querySelector("core-ajax");
29 xhr
.onCreate = function (xhr
) {
32 // Reset the core-ajax element before each test.
36 ajax
.handleAs
= 'text';
42 suite('handleAs', function() {
43 suite('text', function(){
45 "Content-Type": "text/plain"
50 ajax
.handleAs
= 'text';
51 ajax
.url
= "http://example.com/text"
57 requests
[0].respond(200, headers
, "test text");
62 test('Raw text should pass through', function(){
63 assert
.equal(ajax
.response
, "test text")
66 suite('xml', function(){
68 "Content-Type": "text/xml"
73 ajax
.handleAs
= 'xml';
74 ajax
.url
= "http://example.com/xml"
80 requests
[0].respond(200, headers
,
84 "<subject>Reminder</subject>" +
85 "<body><q>Feed me!</q></body>" +
91 test('XML should be returned with queryable structure', function(){
92 var q
= ajax
.response
.querySelector("note body q");
93 assert
.equal(q
.childNodes
[0].textContent
, "Feed me!");
94 var to
= ajax
.response
.querySelector("to");
95 assert
.equal(to
.childNodes
[0].textContent
, "AJ");
97 suite('json', function(){
99 "Content-Type": "text/json"
101 setup(function(done
){
104 ajax
.handleAs
= 'json';
105 ajax
.url
= "http://example.com/json"
111 requests
[0].respond(200, headers
,
112 '{"object" : {"list" : [2, 3, {"key": "value"}]}}');
117 test('JSON should be returned as an Object', function(){
118 var r
= ajax
.response
;
119 assert
.equal(r
.object
.list
[1], 3);
120 assert
.equal(r
.object
.list
[2].key
, "value");
123 suite('arraybuffer', function(){
125 "Content-Type": "text/plain"
127 setup(function(done
){
130 ajax
.handleAs
= 'arraybuffer';
131 ajax
.url
= "http://example.com/data"
137 var buf
= new ArrayBuffer(8*4);
138 var resp
= new Int32Array(buf
);
141 requests
[0].response
= buf
;
142 requests
[0].respond(200, headers
, 'blahblahblah');
147 test('arraybuffer response should be passed through', function(){
148 var r
= ajax
.response
;
149 var ints
= new Int32Array(r
);
150 assert
.equal(ints
[3], 12);
151 assert
.equal(ints
[6], 21);
154 suite('blob', function(){});
155 suite('document', function(){});
157 suite('auto', function() {
158 suiteSetup(function(){
159 ajax
.url
= "http://example.com/"
162 test('url change should trigger request', function(done
){
165 ajax
.url
= "http://example.com/auto";
170 assert
.equal(requests
.length
, 1);
175 test('params change should trigger request', function(done
){
178 ajax
.params
= {param
: "value"};
183 assert
.equal(requests
.length
, 1);
188 test('body change should trigger request', function(done
){
191 ajax
.method
= "POST";
192 ajax
.body
= "bodystuff";
197 assert
.equal(requests
.length
, 1);
203 suite('events', function(){
205 "Content-Type": "text/plain"
207 var body
= "somebodytext";
209 setup(function(done
){
217 ajax
.handleAs
= 'text';
218 ajax
.url
= "http://example.com/text"
226 suite('core-response', function(){
227 test('core-response should be fired on success', function(done
){
228 window
.addEventListener('core-response', function(response
, xhr
){
231 requests
[0].respond(200, headers
, body
);
232 assert
.isTrue(responded
);
235 test('core-response should not be fired on failure', function(done
){
236 window
.addEventListener('core-response', function(response
, xhr
){
239 requests
[0].respond(404, headers
, body
);
240 assert
.isFalse(responded
);
244 suite('core-error', function(){
245 test('core-error should be fired on failure', function(done
){
246 window
.addEventListener('core-error', function(response
, xhr
){
249 requests
[0].respond(404, headers
, body
);
250 assert
.isTrue(responded
);
253 test('core-error should not be fired on success', function(done
){
254 var responded
= false;
255 window
.addEventListener('core-error', function(response
, xhr
){
258 requests
[0].respond(200, headers
, body
);
259 assert
.isFalse(responded
);
263 suite('core-complete', function(){
264 test('core-complete should be fired on success', function(done
){
265 window
.addEventListener('core-complete', function(response
, xhr
){
268 requests
[0].respond(200, headers
, body
);
269 assert
.isTrue(responded
);
272 test('core-complete should be fired on failure', function(done
){
273 var responded
= false;
274 window
.addEventListener('core-complete', function(response
, xhr
){
277 requests
[0].respond(404, headers
, body
);
278 assert
.isTrue(responded
);