test.js 13.0 KB
Newer Older
J
Joshua Peek 已提交
1
test('resolves promise on 500 error', function() {
J
Joshua Peek 已提交
2
  return fetch('/boom').then(function(response) {
J
Joshua Peek 已提交
3
    assert.equal(response.status, 500)
4 5
    return response.text()
  }).then(function(body) {
J
Joshua Peek 已提交
6
    assert.equal(body, 'boom')
D
David Graham 已提交
7 8
  })
})
D
David Graham 已提交
9

J
Joshua Peek 已提交
10
test('rejects promise for network error', function() {
J
Joshua Peek 已提交
11
  return fetch('/error').then(function(response) {
J
Joshua Peek 已提交
12
    assert(false, 'HTTP status ' + response.status + ' was treated as success')
13
  }).catch(function(error) {
J
Joshua Peek 已提交
14
    assert(error instanceof TypeError, 'Rejected with Error')
D
David Graham 已提交
15 16 17
  })
})

18 19 20 21 22 23 24 25 26 27
// https://fetch.spec.whatwg.org/#headers-class
suite('Headers', function() {
  test('headers are case insensitve', function() {
    var headers = new Headers({'Accept': 'application/json'})
    assert.equal(headers.get('ACCEPT'), 'application/json')
    assert.equal(headers.get('Accept'), 'application/json')
    assert.equal(headers.get('accept'), 'application/json')
  })
})

J
Joshua Peek 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41
// https://fetch.spec.whatwg.org/#request-class
suite('Request', function() {
  test('sends request headers', function() {
    return fetch('/request', {
      headers: {
        'Accept': 'application/json',
        'X-Test': '42'
      }
    }).then(function(response) {
      return response.json()
    }).then(function(json) {
      assert.equal(json.headers['accept'], 'application/json')
      assert.equal(json.headers['x-test'], '42')
    })
D
David Graham 已提交
42 43 44
  })
})

J
Joshua Peek 已提交
45 46
// https://fetch.spec.whatwg.org/#response-class
suite('Response', function() {
J
Joshua Peek 已提交
47 48 49 50 51 52 53
  test('construct response with String body', function() {
    var response = new Response('hello')
    response.text().then(function(text) {
      assert.equal(text, 'hello')
    })
  })

J
Joshua Peek 已提交
54
  ;(Response.prototype.blob ? test : test.skip)('construct response with Blob body', function() {
J
Joshua Peek 已提交
55 56 57 58 59 60
    var response = new Response(new Blob(['hello']))
    response.text().then(function(text) {
      assert.equal(text, 'hello')
    })
  })

J
Joshua Peek 已提交
61 62 63 64 65 66 67
  test('populates response body', function() {
    return fetch('/hello').then(function(response) {
      assert.equal(response.status, 200)
      return response.text()
    }).then(function(body) {
      assert.equal(body, 'hi')
    })
68 69
  })

J
Joshua Peek 已提交
70 71 72 73 74
  test('parses response headers', function() {
    return fetch('/headers?' + new Date().getTime()).then(function(response) {
      assert.equal(response.headers.get('Date'), 'Mon, 13 Oct 2014 21:02:27 GMT')
      assert.equal(response.headers.get('Content-Type'), 'text/html; charset=utf-8')
    })
J
Joshua Peek 已提交
75 76
  })
})
77

J
Joshua Peek 已提交
78 79
// https://fetch.spec.whatwg.org/#body-mixin
suite('Body mixin', function() {
L
Laurence Rowe 已提交
80
  ;(Response.prototype.arrayBuffer ? suite : suite.skip)('arrayBuffer', function() {
L
Laurence Rowe 已提交
81 82 83 84 85 86 87 88 89
    test('resolves arrayBuffer promise', function() {
      return fetch('/hello').then(function(response) {
        return response.arrayBuffer()
      }).then(function(buf) {
        assert(buf instanceof ArrayBuffer, 'buf is an ArrayBuffer instance')
        assert.equal(buf.byteLength, 2)
      })
    })

J
Joshua Peek 已提交
90 91
    test('arrayBuffer handles binary data', function() {
      return fetch('/binary').then(function(response) {
L
Laurence Rowe 已提交
92 93 94
        return response.arrayBuffer()
      }).then(function(buf) {
        assert(buf instanceof ArrayBuffer, 'buf is an ArrayBuffer instance')
95 96
        assert.equal(buf.byteLength, 256, 'buf.byteLength is correct')
        var view = new Uint8Array(buf)
J
Joshua Peek 已提交
97
        for (var i = 0; i < 256; i++) {
98 99
          assert.equal(view[i], i)
        }
L
Laurence Rowe 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113
      })
    })

    test('rejects arrayBuffer promise after body is consumed', function() {
      return fetch('/hello').then(function(response) {
        assert(response.arrayBuffer, 'Body does not implement arrayBuffer')
        response.blob()
        return response.arrayBuffer()
      }).catch(function(error) {
        assert(error instanceof TypeError, 'Promise rejected after body consumed')
      })
    })
  })

J
Joshua Peek 已提交
114 115 116 117 118 119 120 121 122
  ;(Response.prototype.blob ? suite : suite.skip)('blob', function() {
    test('resolves blob promise', function() {
      return fetch('/hello').then(function(response) {
        return response.blob()
      }).then(function(blob) {
        assert(blob instanceof Blob, 'blob is a Blob instance')
        assert.equal(blob.size, 2)
      })
    })
123

J
Joshua Peek 已提交
124 125
    test('blob handles binary data', function() {
      return fetch('/binary').then(function(response) {
126 127 128
        return response.blob()
      }).then(function(blob) {
        assert(blob instanceof Blob, 'blob is a Blob instance')
129
        assert.equal(blob.size, 256, 'blob.size is correct')
130 131 132
      })
    })

J
Joshua Peek 已提交
133 134 135 136 137 138 139 140
    test('rejects blob promise after body is consumed', function() {
      return fetch('/hello').then(function(response) {
        assert(response.blob, 'Body does not implement blob')
        response.blob()
        return response.blob()
      }).catch(function(error) {
        assert(error instanceof TypeError, 'Promise rejected after body consumed')
      })
J
Joshua Peek 已提交
141
    })
D
David Graham 已提交
142 143
  })

J
Joshua Peek 已提交
144 145 146 147 148 149 150 151 152 153 154
  ;(Response.prototype.formData ? suite : suite.skip)('formData', function() {
    test('post sets content-type header', function() {
      return fetch('/request', {
        method: 'post',
        body: new FormData()
      }).then(function(response) {
        return response.json()
      }).then(function(json) {
        assert.equal(json.method, 'POST')
        assert(/^multipart\/form-data;/.test(json.headers['content-type']))
      })
J
Joshua Peek 已提交
155
    })
J
Joshua Peek 已提交
156

J
Joshua Peek 已提交
157 158 159 160 161 162 163 164
    test('rejects formData promise after body is consumed', function() {
      return fetch('/json').then(function(response) {
        assert(response.formData, 'Body does not implement formData')
        response.formData()
        return response.formData()
      }).catch(function(error) {
        assert(error instanceof TypeError, 'Promise rejected after body consumed')
      })
J
Joshua Peek 已提交
165 166
    })

J
Joshua Peek 已提交
167 168 169 170 171 172
    test('parses form encoded response', function() {
      return fetch('/form').then(function(response) {
        return response.formData()
      }).then(function(form) {
        assert(form instanceof FormData, 'Parsed a FormData object')
      })
173
    })
D
David Graham 已提交
174 175
  })

J
Joshua Peek 已提交
176 177 178 179 180 181 182 183
  suite('json', function() {
    test('parses json response', function() {
      return fetch('/json').then(function(response) {
        return response.json()
      }).then(function(json) {
        assert.equal(json.name, 'Hubot')
        assert.equal(json.login, 'hubot')
      })
J
Joshua Peek 已提交
184
    })
185

J
Joshua Peek 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    test('rejects json promise after body is consumed', function() {
      return fetch('/json').then(function(response) {
        assert(response.json, 'Body does not implement json')
        response.json()
        return response.json()
      }).catch(function(error) {
        assert(error instanceof TypeError, 'Promise rejected after body consumed')
      })
    })

    test('handles json parse error', function() {
      return fetch('/json-error').then(function(response) {
        return response.json()
      }).catch(function(error) {
        assert(error instanceof Error, 'JSON exception is an Error instance')
        assert(error.message, 'JSON exception has an error message')
      })
    })
D
David Graham 已提交
204 205
  })

J
Joshua Peek 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  suite('text', function() {
    test('handles 204 No Content response', function() {
      return fetch('/empty').then(function(response) {
        assert.equal(response.status, 204)
        return response.text()
      }).then(function(body) {
        assert.equal(body, '')
      })
    })

    test('resolves text promise', function() {
      return fetch('/hello').then(function(response) {
        return response.text()
      }).then(function(text) {
        assert.equal(text, 'hi')
      })
    })

    test('rejects text promise after body is consumed', function() {
      return fetch('/hello').then(function(response) {
        assert(response.text, 'Body does not implement text')
        response.text()
        return response.text()
      }).catch(function(error) {
        assert(error instanceof TypeError, 'Promise rejected after body consumed')
      })
    })
D
David Graham 已提交
233 234
  })
})
D
David Graham 已提交
235

J
Joshua Peek 已提交
236 237
// https://fetch.spec.whatwg.org/#methods
suite('Methods', function() {
J
Joshua Peek 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  test('supports HTTP GET', function() {
    return fetch('/request', {
      method: 'get',
      body: 'name=Hubot'
    }).then(function(response) {
      return response.json()
    }).then(function(request) {
      assert.equal(request.method, 'GET')
      assert.equal(request.data, '')
    })
  })
    test('supports HTTP POST', function() {
    return fetch('/request', {
      method: 'post',
      body: 'name=Hubot'
    }).then(function(response) {
      return response.json()
    }).then(function(request) {
      assert.equal(request.method, 'POST')
      assert.equal(request.data, 'name=Hubot')
J
Joshua Peek 已提交
258
    })
D
David Graham 已提交
259
  })
D
David Graham 已提交
260

J
Joshua Peek 已提交
261 262 263 264 265 266 267 268
  test('supports HTTP PUT', function() {
    return fetch('/request', {
      method: 'put',
      body: 'name=Hubot'
    }).then(function(response) {
      return response.json()
    }).then(function(request) {
      assert.equal(request.method, 'PUT')
J
Joshua Peek 已提交
269
      assert.equal(request.data, 'name=Hubot')
J
Joshua Peek 已提交
270
    })
D
David Graham 已提交
271
  })
272

J
Joshua Peek 已提交
273 274 275 276 277 278 279 280 281 282 283
  var patchSupported = !/PhantomJS/.test(navigator.userAgent)

  ;(patchSupported ? test : test.skip)('supports HTTP PATCH', function() {
    return fetch('/request', {
      method: 'PATCH',
      body: 'name=Hubot'
    }).then(function(response) {
      return response.json()
    }).then(function(request) {
      assert.equal(request.method, 'PATCH')
      assert.equal(request.data, 'name=Hubot')
J
Joshua Peek 已提交
284
    })
285 286
  })

J
Joshua Peek 已提交
287 288 289 290 291 292 293 294 295
  test('supports HTTP DELETE', function() {
    return fetch('/request', {
      method: 'delete',
    }).then(function(response) {
      return response.json()
    }).then(function(request) {
      assert.equal(request.method, 'DELETE')
      assert.equal(request.data, '')
    })
296 297 298
  })
})

J
Joshua Peek 已提交
299 300
// https://fetch.spec.whatwg.org/#atomic-http-redirect-handling
suite('Atomic HTTP redirect handling', function() {
J
Joshua Peek 已提交
301 302 303
  test('handles 301 redirect response', function() {
    return fetch('/redirect/301').then(function(response) {
      assert.equal(response.status, 200)
J
Joshua Peek 已提交
304
      assert.match(response.url, /\/hello/)
J
Joshua Peek 已提交
305 306 307 308
      return response.text()
    }).then(function(body) {
      assert.equal(body, 'hi')
    })
D
David Graham 已提交
309 310
  })

J
Joshua Peek 已提交
311 312 313
  test('handles 302 redirect response', function() {
    return fetch('/redirect/302').then(function(response) {
      assert.equal(response.status, 200)
J
Joshua Peek 已提交
314
      assert.match(response.url, /\/hello/)
J
Joshua Peek 已提交
315 316 317
      return response.text()
    }).then(function(body) {
      assert.equal(body, 'hi')
318
    })
319
  })
M
Mislav Marohnić 已提交
320

J
Joshua Peek 已提交
321 322 323
  test('handles 303 redirect response', function() {
    return fetch('/redirect/303').then(function(response) {
      assert.equal(response.status, 200)
J
Joshua Peek 已提交
324
      assert.match(response.url, /\/hello/)
J
Joshua Peek 已提交
325 326 327 328
      return response.text()
    }).then(function(body) {
      assert.equal(body, 'hi')
    })
M
Mislav Marohnić 已提交
329 330
  })

J
Joshua Peek 已提交
331 332
  test('handles 307 redirect response', function() {
    return fetch('/redirect/307').then(function(response) {
J
Joshua Peek 已提交
333
      assert.equal(response.status, 200)
J
Joshua Peek 已提交
334
      assert.match(response.url, /\/hello/)
J
Joshua Peek 已提交
335 336
      return response.text()
    }).then(function(body) {
J
Joshua Peek 已提交
337
      assert.equal(body, 'hi')
J
Joshua Peek 已提交
338
    })
M
Mislav Marohnić 已提交
339 340
  })

J
Joshua Peek 已提交
341
  var permanentRedirectSupported = !/PhantomJS|Trident/.test(navigator.userAgent)
J
Joshua Peek 已提交
342 343 344 345 346 347 348 349

  ;(permanentRedirectSupported ? test : test.skip)('handles 308 redirect response', function() {
    return fetch('/redirect/308').then(function(response) {
      assert.equal(response.status, 200)
      assert.match(response.url, /\/hello/)
      return response.text()
    }).then(function(body) {
      assert.equal(body, 'hi')
J
Joshua Peek 已提交
350
    })
M
Mislav Marohnić 已提交
351 352
  })
})
J
Joshua Peek 已提交
353

354 355 356
// https://fetch.spec.whatwg.org/#concept-request-credentials-mode
suite('credentials mode', function() {
  var omitSupported = !self.fetch.polyfill
J
Joshua Peek 已提交
357

358 359 360 361 362
  ;(omitSupported ? suite : suite.skip)('omit', function() {
    test('request credentials defaults to omit', function() {
      var request = new Request('')
      assert.equal(request.credentials, 'omit')
    })
J
Joshua Peek 已提交
363

364 365 366 367 368 369 370 371 372
    test('does not send cookies with implicit omit credentials', function() {
      return fetch('/cookie?name=foo&value=bar').then(function() {
        return fetch('/cookie?name=foo');
      }).then(function(response) {
        return response.text()
      }).then(function(data) {
        assert.equal(data, '')
      })
    })
J
Joshua Peek 已提交
373

374 375 376 377 378 379 380 381 382
    test('does not send cookies with omit credentials', function() {
      return fetch('/cookie?name=foo&value=bar').then(function() {
        return fetch('/cookie?name=foo', {credentials: 'omit'})
      }).then(function(response) {
        return response.text()
      }).then(function(data) {
        assert.equal(data, '')
      })
    })
J
Joshua Peek 已提交
383 384
  })

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
  suite('same-origin', function() {
    test('request credentials uses inits member', function() {
      var request = new Request('', {credentials: 'same-origin'})
      assert.equal(request.credentials, 'same-origin')
    })

    test('send cookies with same-origin credentials', function() {
      return fetch('/cookie?name=foo&value=bar').then(function() {
        return fetch('/cookie?name=foo', {credentials: 'same-origin'})
      }).then(function(response) {
        return response.text()
      }).then(function(data) {
        assert.equal(data, 'bar')
      })
    })
J
Joshua Peek 已提交
400 401
  })

402 403 404 405 406 407 408 409 410 411
  suite('include', function() {
    test('send cookies with include credentials', function() {
      return fetch('/cookie?name=foo&value=bar').then(function() {
        return fetch('/cookie?name=foo', {credentials: 'include'})
      }).then(function(response) {
        return response.text()
      }).then(function(data) {
        assert.equal(data, 'bar')
      })
    })
J
Joshua Peek 已提交
412 413
  })
})