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


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

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

D
David Graham 已提交
33
asyncTest('parses response headers', 2, function() {
34
  fetch('/headers?' + new Date().getTime()).then(function(response) {
D
David Graham 已提交
35 36 37 38 39 40
    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()
  })
})

41 42 43 44 45 46 47 48
asyncTest('resolves promise on 500 error', 2, function() {
  fetch('/boom').then(function(response) {
    equal(response.status, 500)
    equal(response.body, 'boom')
    start()
  })
})

J
Joshua Peek 已提交
49 50 51 52 53 54
asyncTest('rejects promise for network error', 1, function() {
  fetch('/error').catch(function() {
    ok(true)
    start()
  })
})
55

D
David Graham 已提交
56 57 58 59 60 61 62 63 64
asyncTest('resolves text promise', 1, function() {
  fetch('/hello').then(function(response) {
    return response.text()
  }).then(function(text) {
    equal(text, 'hi')
    start()
  })
})

D
David Graham 已提交
65 66 67 68 69 70 71 72 73
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 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
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 已提交
93

J
Joshua Peek 已提交
94
if (blobSupport) {
J
Joshua Peek 已提交
95 96 97 98 99 100 101 102
  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 已提交
103
  })
J
Joshua Peek 已提交
104
}
D
David Graham 已提交
105

D
David Graham 已提交
106
asyncTest('post sends encoded body', 2, function() {
J
Joshua Peek 已提交
107
  fetch('/request', {
D
David Graham 已提交
108 109 110 111 112 113 114
    method: 'post',
    body: {
      name: 'Hubot',
      title: 'Hubot Robawt',
      undef: undefined,
      nil: null
    }
J
Joshua Peek 已提交
115 116 117
  }).then(function(response) {
    var request = JSON.parse(response.body);
    equal(request.method, 'POST')
D
David Graham 已提交
118 119 120 121
    equal(request.data, 'name=Hubot&title=Hubot+Robawt&nil=')
    start()
  })
})
D
David Graham 已提交
122 123

asyncTest('post sets content-type header', 1, function() {
J
Joshua Peek 已提交
124
  fetch('/request', {
D
David Graham 已提交
125 126
    method: 'post',
    body: {}
J
Joshua Peek 已提交
127 128 129
  }).then(function(response) {
    var request = JSON.parse(response.body);
    equal(request.headers['content-type'], 'application/x-www-form-urlencoded; charset=UTF-8')
D
David Graham 已提交
130 131 132
    start()
  })
})
133

J
Joshua Peek 已提交
134
if (blobSupport) {
J
Joshua Peek 已提交
135 136 137 138 139 140 141 142 143
  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()
    })
144
  })
J
Joshua Peek 已提交
145
}
146

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

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

asyncTest('supports HTTP PUT', 2, function() {
  fetch('/request', {
    method: 'put',
    body: {
      name: 'Hubot',
      title: 'Hubot Robawt',
    }
  }).then(function(response) {
    var request = JSON.parse(response.body);
    equal(request.method, 'PUT')
    equal(request.data, 'name=Hubot&title=Hubot+Robawt')
    start()
  })
})

asyncTest('supports HTTP PATCH', 2, function() {
  fetch('/request', {
186
    method: 'PATCH',
M
Mislav Marohnić 已提交
187 188 189 190 191 192 193
    body: {
      name: 'Hubot',
      title: 'Hubot Robawt',
    }
  }).then(function(response) {
    var request = JSON.parse(response.body);
    equal(request.method, 'PATCH')
194 195 196 197 198
    if (/PhantomJS/.test(navigator.userAgent)) {
      equal(request.data, '')
    } else {
      equal(request.data, 'name=Hubot&title=Hubot+Robawt')
    }
M
Mislav Marohnić 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212
    start()
  })
})

asyncTest('supports HTTP DELETE', 2, function() {
  fetch('/request', {
    method: 'delete',
  }).then(function(response) {
    var request = JSON.parse(response.body);
    equal(request.method, 'DELETE')
    equal(request.data, '')
    start()
  })
})