test.js 7.5 KB
Newer Older
J
Joshua Peek 已提交
1
test('populates response body', function() {
J
Joshua Peek 已提交
2
  return fetch('/hello').then(function(response) {
J
Joshua Peek 已提交
3
    assert.equal(response.status, 200)
4 5
    return response.text()
  }).then(function(body) {
J
Joshua Peek 已提交
6
    assert.equal(body, 'hi')
D
David Graham 已提交
7 8
  })
})
D
David Graham 已提交
9

J
Joshua Peek 已提交
10
test('sends request headers', function() {
J
Joshua Peek 已提交
11
  return fetch('/request', {
D
David Graham 已提交
12 13 14 15
    headers: {
      'Accept': 'application/json',
      'X-Test': '42'
    }
J
Joshua Peek 已提交
16
  }).then(function(response) {
17 18
    return response.json()
  }).then(function(json) {
J
Joshua Peek 已提交
19 20
    assert.equal(json.headers['accept'], 'application/json')
    assert.equal(json.headers['x-test'], '42')
D
David Graham 已提交
21 22 23
  })
})

J
Joshua Peek 已提交
24
test('parses response headers', function() {
J
Joshua Peek 已提交
25
  return fetch('/headers?' + new Date().getTime()).then(function(response) {
J
Joshua Peek 已提交
26 27
    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')
D
David Graham 已提交
28 29 30
  })
})

J
Joshua Peek 已提交
31
test('resolves promise on 500 error', function() {
J
Joshua Peek 已提交
32
  return fetch('/boom').then(function(response) {
J
Joshua Peek 已提交
33
    assert.equal(response.status, 500)
34 35
    return response.text()
  }).then(function(body) {
J
Joshua Peek 已提交
36
    assert.equal(body, 'boom')
37 38 39
  })
})

J
Joshua Peek 已提交
40
test('rejects promise for network error', function() {
J
Joshua Peek 已提交
41
  return fetch('/error').then(function(response) {
J
Joshua Peek 已提交
42
    assert(false, 'HTTP status ' + response.status + ' was treated as success')
43
  }).catch(function(error) {
J
Joshua Peek 已提交
44
    assert(error instanceof TypeError, 'Rejected with Error')
J
Joshua Peek 已提交
45 46
  })
})
47

J
Joshua Peek 已提交
48
test('handles 204 No Content response', function() {
J
Joshua Peek 已提交
49
  return fetch('/empty').then(function(response) {
J
Joshua Peek 已提交
50
    assert.equal(response.status, 204)
51 52
    return response.text()
  }).then(function(body) {
J
Joshua Peek 已提交
53
    assert.equal(body, '')
54 55 56
  })
})

J
Joshua Peek 已提交
57
test('resolves text promise', function() {
J
Joshua Peek 已提交
58
  return fetch('/hello').then(function(response) {
D
David Graham 已提交
59 60
    return response.text()
  }).then(function(text) {
J
Joshua Peek 已提交
61
    assert.equal(text, 'hi')
D
David Graham 已提交
62 63 64
  })
})

J
Joshua Peek 已提交
65
test('parses json response', function() {
J
Joshua Peek 已提交
66
  return fetch('/json').then(function(response) {
D
David Graham 已提交
67 68
    return response.json()
  }).then(function(json) {
J
Joshua Peek 已提交
69 70
    assert.equal(json.name, 'Hubot')
    assert.equal(json.login, 'hubot')
D
David Graham 已提交
71 72 73
  })
})

J
Joshua Peek 已提交
74
test('handles json parse error', function() {
J
Joshua Peek 已提交
75
  return fetch('/json-error').then(function(response) {
D
David Graham 已提交
76 77
    return response.json()
  }).catch(function(error) {
J
Joshua Peek 已提交
78 79
    assert(error instanceof Error, 'JSON exception is an Error instance')
    assert(error.message, 'JSON exception has an error message')
D
David Graham 已提交
80 81
  })
})
D
David Graham 已提交
82

J
Joshua Peek 已提交
83
;(Response.prototype.blob ? suite : suite.skip)('Blob', function() {
J
Joshua Peek 已提交
84
  test('resolves blob promise', function() {
J
Joshua Peek 已提交
85
    return fetch('/hello').then(function(response) {
J
Joshua Peek 已提交
86 87
      return response.blob()
    }).then(function(blob) {
J
Joshua Peek 已提交
88 89
      assert(blob instanceof Blob, 'blob is a Blob instance')
      assert.equal(blob.size, 2)
J
Joshua Peek 已提交
90
    })
D
David Graham 已提交
91
  })
92

J
Joshua Peek 已提交
93
  test('rejects blob promise after body is consumed', function() {
J
Joshua Peek 已提交
94
    return fetch('/hello').then(function(response) {
J
Joshua Peek 已提交
95
      assert(response.blob, 'Body does not implement blob')
J
Joshua Peek 已提交
96 97 98
      response.blob()
      return response.blob()
    }).catch(function(error) {
J
Joshua Peek 已提交
99
      assert(error instanceof TypeError, 'Promise rejected after body consumed')
J
Joshua Peek 已提交
100
    })
101
  })
J
Joshua Peek 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
})

;(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']))
    })
  })

  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')
    })
  })

  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')
    })
  })
})
135

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

J
Joshua Peek 已提交
146
test('rejects text promise after body is consumed', function() {
J
Joshua Peek 已提交
147
  return fetch('/hello').then(function(response) {
J
Joshua Peek 已提交
148
    assert(response.text, 'Body does not implement text')
149 150 151
    response.text()
    return response.text()
  }).catch(function(error) {
J
Joshua Peek 已提交
152
    assert(error instanceof TypeError, 'Promise rejected after body consumed')
D
David Graham 已提交
153 154 155
  })
})

J
Joshua Peek 已提交
156
test('supports HTTP PUT', function() {
J
Joshua Peek 已提交
157
  return fetch('/request', {
M
Mislav Marohnić 已提交
158
    method: 'put',
159
    body: 'name=Hubot'
M
Mislav Marohnić 已提交
160
  }).then(function(response) {
161 162
    return response.json()
  }).then(function(request) {
J
Joshua Peek 已提交
163 164
    assert.equal(request.method, 'PUT')
    assert.equal(request.data, 'name=Hubot')
M
Mislav Marohnić 已提交
165 166 167
  })
})

J
Joshua Peek 已提交
168
test('supports HTTP PATCH', function() {
J
Joshua Peek 已提交
169
  return fetch('/request', {
170
    method: 'PATCH',
171
    body: 'name=Hubot'
M
Mislav Marohnić 已提交
172
  }).then(function(response) {
173 174
    return response.json()
  }).then(function(request) {
J
Joshua Peek 已提交
175
    assert.equal(request.method, 'PATCH')
176
    if (/PhantomJS/.test(navigator.userAgent)) {
J
Joshua Peek 已提交
177
      assert.equal(request.data, '')
178
    } else {
J
Joshua Peek 已提交
179
      assert.equal(request.data, 'name=Hubot')
180
    }
M
Mislav Marohnić 已提交
181 182 183
  })
})

J
Joshua Peek 已提交
184
test('supports HTTP DELETE', function() {
J
Joshua Peek 已提交
185
  return fetch('/request', {
M
Mislav Marohnić 已提交
186 187
    method: 'delete',
  }).then(function(response) {
188 189
    return response.json()
  }).then(function(request) {
J
Joshua Peek 已提交
190 191
    assert.equal(request.method, 'DELETE')
    assert.equal(request.data, '')
M
Mislav Marohnić 已提交
192 193
  })
})
J
Joshua Peek 已提交
194

J
Joshua Peek 已提交
195 196
// TODO: Avoid URL, not in PhantomJS
;(self.URL ? suite : suite.skip)('Redirect', function() {
J
Joshua Peek 已提交
197 198 199 200 201 202 203 204
  test('handles 301 redirect response', function() {
    return fetch('/redirect/301').then(function(response) {
      assert.equal(response.status, 200)
      assert.equal(response.url ? new URL(response.url).pathname : null, '/hello')
      return response.text()
    }).then(function(body) {
      assert.equal(body, 'hi')
    })
J
Joshua Peek 已提交
205 206
  })

J
Joshua Peek 已提交
207 208 209 210 211 212 213 214
  test('handles 302 redirect response', function() {
    return fetch('/redirect/302').then(function(response) {
      assert.equal(response.status, 200)
      assert.equal(response.url ? new URL(response.url).pathname : null, '/hello')
      return response.text()
    }).then(function(body) {
      assert.equal(body, 'hi')
    })
J
Joshua Peek 已提交
215 216
  })

J
Joshua Peek 已提交
217 218 219 220 221 222 223 224
  test('handles 303 redirect response', function() {
    return fetch('/redirect/303').then(function(response) {
      assert.equal(response.status, 200)
      assert.equal(response.url ? new URL(response.url).pathname : null, '/hello')
      return response.text()
    }).then(function(body) {
      assert.equal(body, 'hi')
    })
J
Joshua Peek 已提交
225 226
  })

J
Joshua Peek 已提交
227 228
  test('handles 307 redirect response', function() {
    return fetch('/redirect/307').then(function(response) {
J
Joshua Peek 已提交
229
      assert.equal(response.status, 200)
J
Joshua Peek 已提交
230
      assert.equal(response.url ? new URL(response.url).pathname : null, '/hello')
J
Joshua Peek 已提交
231 232
      return response.text()
    }).then(function(body) {
J
Joshua Peek 已提交
233
      assert.equal(body, 'hi')
J
Joshua Peek 已提交
234
    })
J
Joshua Peek 已提交
235
  })
J
Joshua Peek 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249

  // PhantomJS doesn't support 308 redirects
  if (!navigator.userAgent.match(/PhantomJS/)) {
    test('handles 308 redirect response', function() {
      return fetch('/redirect/308').then(function(response) {
        assert.equal(response.status, 200)
      assert.equal(response.url ? new URL(response.url).pathname : null, '/hello')
        return response.text()
      }).then(function(body) {
        assert.equal(body, 'hi')
      })
    })
  }
})