|
| 1 | +import test from 'tape' |
| 2 | +import url from 'url' |
| 3 | +import path from 'path' |
| 4 | +import sandbox from '@architect/sandbox' |
| 5 | +import tiny from 'tiny-json-http' |
| 6 | + |
| 7 | +const baseUrl = 'http://localhost:3333' |
| 8 | +const __dirname = path.dirname(url.fileURLToPath(import.meta.url)) |
| 9 | + |
| 10 | +test(`Start local server`, async t => { |
| 11 | + await sandbox.start({ quiet: true, cwd: path.join(__dirname, '..') }) |
| 12 | + t.pass('local server started') |
| 13 | + t.end() |
| 14 | +}) |
| 15 | + |
| 16 | +test('GET request', async t => { |
| 17 | + const response = await tiny.get({ headers: { 'accept': 'application/json' }, url: baseUrl + '/verbs' }) |
| 18 | + const expected = `{"verb":"get"}` |
| 19 | + t.ok(JSON.stringify(response.body) === expected, 'GET response') |
| 20 | + t.end() |
| 21 | +}) |
| 22 | + |
| 23 | +test('HEAD request', async t => { |
| 24 | + const response = await tiny.head({ headers: { 'accept': 'application/json' }, url: baseUrl + '/verbs' }) |
| 25 | + const expected = `application/json; charset=utf8` |
| 26 | + t.ok(response.headers['content-type'] === expected, 'HEAD response') |
| 27 | + t.end() |
| 28 | +}) |
| 29 | + |
| 30 | +test('OPTIONS request', async t => { |
| 31 | + const response = await tiny.options({ headers: { 'accept': 'application/json' }, url: baseUrl + '/verbs' }) |
| 32 | + const expected = `OPTIONS, GET, HEAD, POST` |
| 33 | + t.ok(response.headers['allow'] === expected, 'OPTIONS response') |
| 34 | + t.end() |
| 35 | +}) |
| 36 | + |
| 37 | +test('POST request', async t => { |
| 38 | + const response = await tiny.post({ headers: { 'accept': 'application/json' }, url: baseUrl + '/verbs', data: {} }) |
| 39 | + const expected = `{"verb":"post"}` |
| 40 | + t.ok(JSON.stringify(response.body) === expected, 'POST response') |
| 41 | + t.end() |
| 42 | +}) |
| 43 | + |
| 44 | +test('PUT request', async t => { |
| 45 | + const response = await tiny.put({ headers: { 'accept': 'application/json' }, url: baseUrl + '/verbs', data: {} }) |
| 46 | + const expected = `{"verb":"put"}` |
| 47 | + t.ok(JSON.stringify(response.body) === expected, 'PUT response') |
| 48 | + t.end() |
| 49 | +}) |
| 50 | + |
| 51 | +test('PATCH request', async t => { |
| 52 | + const response = await tiny.patch({ headers: { 'accept': 'application/json' }, url: baseUrl + '/verbs', data: {} }) |
| 53 | + const expected = `e0023aa4f` |
| 54 | + t.ok(response.headers['etag'] === expected, 'PATCH response') |
| 55 | + t.end() |
| 56 | +}) |
| 57 | + |
| 58 | +test('DELETE request', async t => { |
| 59 | + const response = await tiny.del({ headers: { 'accept': 'application/json' }, url: baseUrl + '/verbs', data: {} }) |
| 60 | + const expected = `{"verb":"delete"}` |
| 61 | + t.ok(JSON.stringify(response.body) === expected, 'DELETE response') |
| 62 | + t.end() |
| 63 | +}) |
| 64 | + |
| 65 | +test('Shut down local server', async t => { |
| 66 | + await sandbox.end() |
| 67 | + t.pass('Shut down Sandbox') |
| 68 | + t.end() |
| 69 | +}) === 'one' |
0 commit comments