test.js 5.3 KB
Newer Older
J
Joshua Peek 已提交
1 2 3 4 5 6 7 8 9
var blobSupport = (function() {
  try {
    new Blob();
    return true
  } catch(e) {
    return false
  }
})();

J
Joshua Peek 已提交
10
asyncTest('populates response body', 2, function() {
D
David Graham 已提交
11 12
  fetch('/hello').then(function(response) {
    equal(response.status, 200)
13 14 15
    return response.text()
  }).then(function(body) {
    equal(body, 'hi')
D
David Graham 已提交
16 17 18
    start()
  })
})
D
David Graham 已提交
19

D
David Graham 已提交
20
asyncTest('sends request headers', 2, function() {
J
Joshua Peek 已提交
21
  fetch('/request', {
D
David Graham 已提交
22 23 24 25
    headers: {
      'Accept': 'application/json',
      'X-Test': '42'
    }
J
Joshua Peek 已提交
26
  }).then(function(response) {
27 28 29 30
    return response.json()
  }).then(function(json) {
    equal(json.headers['accept'], 'application/json')
    equal(json.headers['x-test'], '42')
D
David Graham 已提交
31 32 33 34
    start()
  })
})

D
David Graham 已提交
35
asyncTest('parses response headers', 2, function() {
36
  fetch('/headers?' + new Date().getTime()).then(function(response) {
D
David Graham 已提交
37 38 39 40 41 42
    equal(response.headers.get('Date'), 'Mon, 13 Oct 2014 21:02:27 GMT')
    equal(response.headers.get('Content-Type'), 'text/html; charset=utf-8')
    start()
  })
})

43 44 45
asyncTest('resolves promise on 500 error', 2, function() {
  fetch('/boom').then(function(response) {
    equal(response.status, 500)
46 47 48
    return response.text()
  }).then(function(body) {
    equal(body, 'boom')
49 50 51 52
    start()
  })
})

J
Joshua Peek 已提交
53
asyncTest('rejects promise for network error', 1, function() {
54 55 56 57
  fetch('/error').then(function(response) {
    ok(false, 'HTTP status ' + response.status + ' was treated as success')
    start()
  }).catch(function() {
J
Joshua Peek 已提交
58 59 60 61
    ok(true)
    start()
  })
})
62

63 64 65
asyncTest('handles 204 No Content response', 2, function() {
  fetch('/empty').then(function(response) {
    equal(response.status, 204)
66 67 68
    return response.text()
  }).then(function(body) {
    equal(body, '')
69 70 71 72
    start()
  })
})

D
David Graham 已提交
73 74 75 76 77 78 79 80 81
asyncTest('resolves text promise', 1, function() {
  fetch('/hello').then(function(response) {
    return response.text()
  }).then(function(text) {
    equal(text, 'hi')
    start()
  })
})

D
David Graham 已提交
82 83 84 85 86 87 88 89 90
asyncTest('parses form encoded response', 1, function() {
  fetch('/form').then(function(response) {
    return response.formData()
  }).then(function(form) {
    ok(form instanceof FormData, 'Parsed a FormData object')
    start()
  })
})

D
David Graham 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
asyncTest('parses json response', 2, function() {
  fetch('/json').then(function(response) {
    return response.json()
  }).then(function(json) {
    equal(json.name, 'Hubot')
    equal(json.login, 'hubot')
    start()
  })
})

asyncTest('handles json parse error', 2, function() {
  fetch('/json-error').then(function(response) {
    return response.json()
  }).catch(function(error) {
    ok(error instanceof Error, 'JSON exception is an Error instance')
    ok(error.message, 'JSON exception has an error message')
    start()
  })
})
D
David Graham 已提交
110

J
Joshua Peek 已提交
111
if (blobSupport) {
J
Joshua Peek 已提交
112 113 114 115 116 117 118 119
  asyncTest('resolves blob promise', 2, function() {
    fetch('/hello').then(function(response) {
      return response.blob()
    }).then(function(blob) {
      ok(blob instanceof Blob, 'blob is a Blob instance')
      equal(blob.size, 2)
      start()
    })
D
David Graham 已提交
120
  })
J
Joshua Peek 已提交
121
}
D
David Graham 已提交
122

D
David Graham 已提交
123
asyncTest('post sets content-type header', 2, function() {
J
Joshua Peek 已提交
124
  fetch('/request', {
D
David Graham 已提交
125
    method: 'post',
D
David Graham 已提交
126
    body: new FormData()
J
Joshua Peek 已提交
127
  }).then(function(response) {
128 129
    return response.json()
  }).then(function(json) {
D
David Graham 已提交
130
    equal(json.method, 'POST')
131
    ok(/^multipart\/form-data;/.test(json.headers['content-type']))
D
David Graham 已提交
132 133 134
    start()
  })
})
135

J
Joshua Peek 已提交
136
if (blobSupport) {
J
Joshua Peek 已提交
137 138 139 140 141 142 143 144 145
  asyncTest('rejects blob promise after body is consumed', 2, function() {
    fetch('/hello').then(function(response) {
      response.blob()
      return response.blob()
    }).catch(function(error) {
      ok(error instanceof TypeError, 'Promise rejected after body consumed')
      ok(error.message === 'Body already consumed', 'Promise rejected for incorrect reason')
      start()
    })
146
  })
J
Joshua Peek 已提交
147
}
148

149
asyncTest('rejects json promise after body is consumed', 2, function() {
150 151 152 153 154
  fetch('/json').then(function(response) {
    response.json()
    return response.json()
  }).catch(function(error) {
    ok(error instanceof TypeError, 'Promise rejected after body consumed')
155
    ok(error.message === 'Body already consumed', 'Promise rejected for incorrect reason')
156 157 158 159
    start()
  })
})

160
asyncTest('rejects text promise after body is consumed', 2, function() {
161 162 163 164 165
  fetch('/hello').then(function(response) {
    response.text()
    return response.text()
  }).catch(function(error) {
    ok(error instanceof TypeError, 'Promise rejected after body consumed')
166
    ok(error.message === 'Body already consumed', 'Promise rejected for incorrect reason')
167 168 169
    start()
  })
})
M
Mislav Marohnić 已提交
170 171 172 173

asyncTest('supports HTTP PUT', 2, function() {
  fetch('/request', {
    method: 'put',
174
    body: 'name=Hubot'
M
Mislav Marohnić 已提交
175
  }).then(function(response) {
176 177
    return response.json()
  }).then(function(request) {
M
Mislav Marohnić 已提交
178
    equal(request.method, 'PUT')
179
    equal(request.data, 'name=Hubot')
M
Mislav Marohnić 已提交
180 181 182 183 184 185
    start()
  })
})

asyncTest('supports HTTP PATCH', 2, function() {
  fetch('/request', {
186
    method: 'PATCH',
187
    body: 'name=Hubot'
M
Mislav Marohnić 已提交
188
  }).then(function(response) {
189 190
    return response.json()
  }).then(function(request) {
M
Mislav Marohnić 已提交
191
    equal(request.method, 'PATCH')
192 193 194
    if (/PhantomJS/.test(navigator.userAgent)) {
      equal(request.data, '')
    } else {
195
      equal(request.data, 'name=Hubot')
196
    }
M
Mislav Marohnić 已提交
197 198 199 200 201 202 203 204
    start()
  })
})

asyncTest('supports HTTP DELETE', 2, function() {
  fetch('/request', {
    method: 'delete',
  }).then(function(response) {
205 206
    return response.json()
  }).then(function(request) {
M
Mislav Marohnić 已提交
207 208 209 210 211
    equal(request.method, 'DELETE')
    equal(request.data, '')
    start()
  })
})