test.js 4.8 KB
Newer Older
D
David Graham 已提交
1 2 3 4 5 6
MockXHR.responses = {
  '/hello': function(xhr) {
    xhr.respond(200, 'hi')
  },
  '/boom': function(xhr) {
    xhr.respond(500, 'boom')
D
David Graham 已提交
7
  },
8 9 10
  '/error': function(xhr) {
    xhr.error()
  },
D
David Graham 已提交
11 12 13
  '/form': function(xhr) {
    xhr.respond(200, 'number=1&space=one+two&empty=&encoded=a%2Bb&')
  },
D
David Graham 已提交
14 15 16 17 18
  '/json': function(xhr) {
    xhr.respond(200, JSON.stringify({name: 'Hubot', login: 'hubot'}))
  },
  '/json-error': function(xhr) {
    xhr.respond(200, 'not json {')
D
David Graham 已提交
19 20
  },
  '/headers': function(xhr) {
D
David Graham 已提交
21
    var headers = [
D
David Graham 已提交
22 23 24 25
      'Date: Mon, 13 Oct 2014 21:02:27 GMT',
      'Content-Type: text/html; charset=utf-8'
    ].join('\r\n')
    xhr.respond(200, 'hi', headers + '\r\n')
D
David Graham 已提交
26 27 28 29 30
  }
}

window.XMLHttpRequest = MockXHR

D
David Graham 已提交
31
asyncTest('populates response body', 3, function() {
D
David Graham 已提交
32
  fetch('/hello').then(function(response) {
D
David Graham 已提交
33
    equal(MockXHR.last().method, 'GET')
D
David Graham 已提交
34 35 36 37 38
    equal(response.status, 200)
    equal(response.body, 'hi')
    start()
  })
})
D
David Graham 已提交
39

D
David Graham 已提交
40
asyncTest('sends request headers', 2, function() {
D
David Graham 已提交
41 42 43 44 45
  fetch('/hello', {
    headers: {
      'Accept': 'application/json',
      'X-Test': '42'
    }
D
David Graham 已提交
46
  }).then(function() {
D
David Graham 已提交
47 48 49 50 51 52 53
    var request = MockXHR.last()
    equal(request.headers['Accept'], 'application/json')
    equal(request.headers['X-Test'], '42')
    start()
  })
})

D
David Graham 已提交
54 55 56 57 58 59 60 61
asyncTest('parses response headers', 2, function() {
  fetch('/headers').then(function(response) {
    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()
  })
})

62 63 64 65 66 67 68 69
asyncTest('resolves promise on 500 error', 2, function() {
  fetch('/boom').then(function(response) {
    equal(response.status, 500)
    equal(response.body, 'boom')
    start()
  })
})

70 71 72 73 74 75 76
asyncTest('rejects promise for network error', 1, function() {
  fetch('/error').catch(function() {
    ok(true)
    start()
  })
})

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

D
David Graham 已提交
86 87 88 89 90 91 92 93 94
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 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
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 已提交
114 115 116 117 118 119 120 121 122 123

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 已提交
124

D
David Graham 已提交
125
asyncTest('post sends encoded body', 2, function() {
D
David Graham 已提交
126 127 128 129 130 131 132 133
  fetch('/hello', {
    method: 'post',
    body: {
      name: 'Hubot',
      title: 'Hubot Robawt',
      undef: undefined,
      nil: null
    }
D
David Graham 已提交
134
  }).then(function() {
D
David Graham 已提交
135 136 137 138 139 140
    var request = MockXHR.last()
    equal(request.method, 'post')
    equal(request.data, 'name=Hubot&title=Hubot+Robawt&nil=')
    start()
  })
})
D
David Graham 已提交
141 142 143 144 145

asyncTest('post sets content-type header', 1, function() {
  fetch('/hello', {
    method: 'post',
    body: {}
D
David Graham 已提交
146
  }).then(function() {
D
David Graham 已提交
147 148 149 150 151
    var request = MockXHR.last()
    equal(request.headers['Content-Type'], 'application/x-www-form-urlencoded; charset=UTF-8')
    start()
  })
})
152

153
asyncTest('rejects blob promise after body is consumed', 2, function() {
154 155 156 157 158
  fetch('/hello').then(function(response) {
    response.blob()
    return response.blob()
  }).catch(function(error) {
    ok(error instanceof TypeError, 'Promise rejected after body consumed')
159
    ok(error.message === 'Body already consumed', 'Promise rejected for incorrect reason')
160 161 162 163
    start()
  })
})

164
asyncTest('rejects json promise after body is consumed', 2, function() {
165 166 167 168 169
  fetch('/json').then(function(response) {
    response.json()
    return response.json()
  }).catch(function(error) {
    ok(error instanceof TypeError, 'Promise rejected after body consumed')
170
    ok(error.message === 'Body already consumed', 'Promise rejected for incorrect reason')
171 172 173 174
    start()
  })
})

175
asyncTest('rejects text promise after body is consumed', 2, function() {
176 177 178 179 180
  fetch('/hello').then(function(response) {
    response.text()
    return response.text()
  }).catch(function(error) {
    ok(error instanceof TypeError, 'Promise rejected after body consumed')
181
    ok(error.message === 'Body already consumed', 'Promise rejected for incorrect reason')
182 183 184
    start()
  })
})