| 1 |
import test from 'ava'; |
| 2 |
import lowercaseKeys from 'lowercase-keys'; |
| 3 |
import getStream from 'get-stream'; |
| 4 |
import Response from '../'; |
| 5 |
import f from './fixtures'; |
| 6 |
|
| 7 |
test('Response is a function', t => { |
| 8 |
t.is(typeof Response, 'function'); |
| 9 |
}); |
| 10 |
|
| 11 |
test('Response cannot be invoked without \'new\'', t => { |
| 12 |
t.throws(() => Response(f.statusCode, f.headers, f.body, f.url)); // eslint-disable-line new-cap |
| 13 |
t.notThrows(() => new Response(f.statusCode, f.headers, f.body, f.url)); |
| 14 |
}); |
| 15 |
|
| 16 |
test('new Response() throws on invalid statusCode', t => { |
| 17 |
const error = t.throws(() => new Response(undefined, f.headers, f.body, f.url)); |
| 18 |
t.is(error.message, 'Argument `statusCode` should be a number'); |
| 19 |
}); |
| 20 |
|
| 21 |
test('new Response() throws on invalid headers', t => { |
| 22 |
const error = t.throws(() => new Response(f.statusCode, undefined, f.body, f.url)); |
| 23 |
t.is(error.message, 'Argument `headers` should be an object'); |
| 24 |
}); |
| 25 |
|
| 26 |
test('new Response() throws on invalid body', t => { |
| 27 |
const error = t.throws(() => new Response(f.statusCode, f.headers, undefined, f.url)); |
| 28 |
t.is(error.message, 'Argument `body` should be a buffer'); |
| 29 |
}); |
| 30 |
|
| 31 |
test('new Response() throws on invalid url', t => { |
| 32 |
const error = t.throws(() => new Response(f.statusCode, f.headers, f.body, undefined)); |
| 33 |
t.is(error.message, 'Argument `url` should be a string'); |
| 34 |
}); |
| 35 |
|
| 36 |
test('response has expected properties', t => { |
| 37 |
const response = new Response(f.statusCode, f.headers, f.body, f.url); |
| 38 |
t.is(response.statusCode, f.statusCode); |
| 39 |
t.deepEqual(response.headers, lowercaseKeys(f.headers)); |
| 40 |
t.is(response.body, f.body); |
| 41 |
t.is(response.url, f.url); |
| 42 |
}); |
| 43 |
|
| 44 |
test('response headers have lowercase keys', t => { |
| 45 |
const response = new Response(f.statusCode, f.headers, f.body, f.url); |
| 46 |
t.not(JSON.stringify(f.headers), response.headers); |
| 47 |
t.deepEqual(response.headers, lowercaseKeys(f.headers)); |
| 48 |
}); |
| 49 |
|
| 50 |
test('response streams body', async t => { |
| 51 |
const response = new Response(f.statusCode, f.headers, f.body, f.url); |
| 52 |
const responseStream = await getStream(response); |
| 53 |
t.is(responseStream, f.bodyText); |
| 54 |
}); |