Blame view

bower_components/iron-ajax/test/iron-request.html 8.63 KB
73bcce88   luigser   COMPONENTS
1
2
3
4
5
6
7
8
9
10
11
12
13
  <!doctype html>
  <!--
  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>
  
e619a3b0   Luigi Serra   Controllet cross ...
14
    <script src="../../webcomponentsjs/webcomponents.js"></script>
73bcce88   luigser   COMPONENTS
15
    <script src="../../web-component-tester/browser.js"></script>
73bcce88   luigser   COMPONENTS
16
17
  
    <link rel="import" href="../../polymer/polymer.html">
73bcce88   luigser   COMPONENTS
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    <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}'
          ]);
  
a1a3bc73   Luigi Serra   graphs updates
44
45
46
47
48
49
          server.respondWith('GET', '/responds_to_get_with_prefixed_json', [
            200,
            jsonResponseHeaders,
            '])}while(1);</x>{"success":true}'
          ]);
  
73bcce88   luigser   COMPONENTS
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
          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'
          };
        });
  
        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('sets async to true by default', function () {
            request.send(successfulRequestOptions);
            expect(request.xhr.async).to.be.eql(true);
          });
  
e619a3b0   Luigi Serra   Controllet cross ...
93
          test('can be aborted', function () {
73bcce88   luigser   COMPONENTS
94
95
96
97
98
99
            request.send(successfulRequestOptions);
  
            request.abort();
  
            server.respond();
  
e619a3b0   Luigi Serra   Controllet cross ...
100
101
            return request.completes.then(function () {
              throw new Error('Request did not abort appropriately!');
73bcce88   luigser   COMPONENTS
102
103
            }).catch(function (e) {
              expect(request.response).to.not.be.ok;
73bcce88   luigser   COMPONENTS
104
105
106
            });
          });
  
e619a3b0   Luigi Serra   Controllet cross ...
107
          test('default responseType is text', function () {
73bcce88   luigser   COMPONENTS
108
109
110
            request.send(successfulRequestOptions);
            server.respond();
  
e619a3b0   Luigi Serra   Controllet cross ...
111
            return request.completes.then(function() {
73bcce88   luigser   COMPONENTS
112
              expect(request.response).to.be.an('string')
73bcce88   luigser   COMPONENTS
113
            });
73bcce88   luigser   COMPONENTS
114
115
          });
  
eb240478   Luigi Serra   public room cards...
116
117
118
119
120
121
122
123
124
125
126
127
          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;
            });
          });
  
e619a3b0   Luigi Serra   Controllet cross ...
128
          test('responseType can be configured via handleAs option', function () {
73bcce88   luigser   COMPONENTS
129
130
131
132
            var options = Object.create(successfulRequestOptions);
            options.handleAs = 'json';
  
            request.send(options);
f748e9cf   Luigi Serra   new controllet an...
133
134
135
            expect(server.requests.length).to.be.equal(1);
            expect(server.requests[0].requestHeaders['accept']).to.be.equal(
                'application/json');
73bcce88   luigser   COMPONENTS
136
137
            server.respond();
  
e619a3b0   Luigi Serra   Controllet cross ...
138
            return request.completes.then(function() {
73bcce88   luigser   COMPONENTS
139
              expect(request.response).to.be.an('object');
73bcce88   luigser   COMPONENTS
140
            });
73bcce88   luigser   COMPONENTS
141
142
          });
  
a1a3bc73   Luigi Serra   graphs updates
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
          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});
            });
          });
  
eb240478   Luigi Serra   public room cards...
161
162
163
164
165
166
          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);
f748e9cf   Luigi Serra   new controllet an...
167
168
169
            expect(server.requests.length).to.be.equal(1);
            expect(server.requests[0].requestHeaders['accept']).to.be.equal(
                'application/json');
eb240478   Luigi Serra   public room cards...
170
171
172
173
174
175
176
            server.respond();
  
            return request.completes.then(function() {
              expect(request.response).to.be.a('string');
            });
          });
  
a1a3bc73   Luigi Serra   graphs updates
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
          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');
          });
73bcce88   luigser   COMPONENTS
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
        });
  
        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);
          });
        });
  
        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();
e619a3b0   Luigi Serra   Controllet cross ...
253
  
73bcce88   luigser   COMPONENTS
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
            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>