-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,129 @@ | ||
const test = require('tape'); | ||
const { parse } = require('url'); | ||
const httpie = require('../dist/httpie'); | ||
|
||
// Demo: https://reqres.in/api | ||
function isResponse(t, res, code, expected) { | ||
t.is(res.statusCode, code, `~> statusCode = ${code}`); | ||
t.ok(res.headers.etag, `~> headers.etag exists`); | ||
t.ok(res.headers['content-type'], `~> headers['content-type'] exists`); | ||
t.ok(res.headers['content-length'], `~> headers['content-length'] exists`); | ||
|
||
t.is(Object.prototype.toString.call(res.data), '[object Object]', '~> res.data is an object'); | ||
if (expected) t.same(res.data, expected, '~~> is expected response data!'); | ||
} | ||
|
||
test('exports', t => { | ||
t.plan(8); | ||
t.is(typeof httpie, 'object', 'exports an Object'); | ||
|
||
['send', 'get', 'post', 'put', 'patch', 'del'].forEach(k => { | ||
t.is(typeof httpie[k], 'function', `~> httpie.${k} is a function`); | ||
}); | ||
let out = httpie.send('GET', 'https://www.google.com'); | ||
t.true(out instanceof Promise, '~> always returns a Promise!'); | ||
}); | ||
|
||
test('GET (200)', async t => { | ||
t.plan(6); | ||
let res = await httpie.get('https://reqres.in/api/users/2'); | ||
isResponse(t, res, 200, {"data":{"id":2,"first_name":"Janet","last_name":"Weaver","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"}}); | ||
}); | ||
|
||
test('GET (404)', async t => { | ||
t.plan(9); | ||
try { | ||
await httpie.get('https://reqres.in/api/users/23'); | ||
t.pass('i will not run'); | ||
} catch (err) { | ||
t.true(err instanceof Error, '~> returns a true Error instance'); | ||
t.is(err.message, err.statusMessage, '~> the "message" and "statusMessage" are identical'); | ||
t.is(err.message, 'Not Found', '~~> Not Found'); | ||
isResponse(t, err, 404, {}); | ||
} | ||
}); | ||
|
||
test('POST 201', async t => { | ||
t.plan(9); | ||
|
||
let body = { | ||
name: 'morpheus', | ||
job: 'leader' | ||
}; | ||
|
||
let res = await httpie.post('https://reqres.in/api/users', { body }); | ||
|
||
isResponse(t, res, 201); | ||
t.ok(!!res.data.id, '~~> created item w/ "id" value'); | ||
t.is(res.data.job, body.job, '~~> created item w/ "job" value'); | ||
t.is(res.data.name, body.name, '~~> created item w/ "name" value'); | ||
t.ok(!!res.data.createdAt, '~~> created item w/ "createdAt" value'); | ||
}); | ||
|
||
test('PUT (200)', async t => { | ||
t.plan(8); | ||
|
||
let body = { | ||
name: 'morpheus', | ||
job: 'zion resident' | ||
}; | ||
|
||
t.end(); | ||
let res = await httpie.put('https://reqres.in/api/users/2', { body }); | ||
|
||
isResponse(t, res, 200); | ||
t.is(res.data.job, body.job, '~~> created item w/ "job" value'); | ||
t.is(res.data.name, body.name, '~~> created item w/ "name" value'); | ||
t.ok(!!res.data.updatedAt, '~~> created item w/ "updatedAt" value'); | ||
}); | ||
|
||
test('PATCH (200)', async t => { | ||
t.plan(8); | ||
|
||
let body = { | ||
name: 'morpheus', | ||
job: 'rebel' | ||
}; | ||
|
||
let res = await httpie.patch('https://reqres.in/api/users/2', { body }); | ||
|
||
isResponse(t, res, 200); | ||
t.is(res.data.job, body.job, '~~> created item w/ "job" value'); | ||
t.is(res.data.name, body.name, '~~> created item w/ "name" value'); | ||
t.ok(!!res.data.updatedAt, '~~> created item w/ "updatedAt" value'); | ||
}); | ||
|
||
test('DELETE (204)', async t => { | ||
t.plan(2); | ||
let res = await httpie.del('https://reqres.in/api/users/2'); | ||
t.is(res.statusCode, 204); | ||
t.is(res.data, ''); | ||
}); | ||
|
||
test('GET (301 HTTPS)', async t => { | ||
t.plan(4); | ||
let res = await httpie.get('http://reqres.in/api/users'); | ||
t.is(res.statusCode, 301, '~> statusCode = 301'); | ||
t.is(res.statusMessage, 'Moved Permanently', '~> "Moved Permanently"'); | ||
t.is(res.headers.location, 'https://reqres.in/api/users', '~> has "Location" header'); | ||
t.is(res.data, '', '~> res.data is empty string'); | ||
}); | ||
|
||
test('GET (delay)', async t => { | ||
t.plan(3); | ||
let now = Date.now(); | ||
let res = await httpie.send('GET', 'https://reqres.in/api/users?delay=5'); | ||
t.is(res.statusCode, 200, '~> res.statusCode = 200'); | ||
t.is(typeof res.data, 'object', '~> res.data is an object'); | ||
t.true(Date.now() - now >= 5e3, '~> waited at least 5 seconds'); | ||
}); | ||
|
||
test('POST (string body w/ object url)', async t => { | ||
t.plan(7); | ||
const body = 'peter@klaven'; | ||
const uri = parse('https://reqres.in/api/login'); | ||
await httpie.post(uri, { body }).catch(err => { | ||
t.is(err.message, 'Bad Request'); | ||
isResponse(t, err, 400, { | ||
error: 'Missing email or username' | ||
}); | ||
}); | ||
}); |