Skip to content

Commit 40af947

Browse files
committed
Issue #110: HTTP DELETE does not map to the API destroy method
Signed-off-by: macdonst <simon.macdonald@gmail.com>
1 parent d4ad21a commit 40af947

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

app/api/verbs.mjs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export async function get () {
2+
return { json: { verb: 'get' } }
3+
}
4+
5+
export async function head () {
6+
return { json: null }
7+
}
8+
9+
export async function options () {
10+
return {
11+
headers: {
12+
'allow': 'OPTIONS, GET, HEAD, POST'
13+
}
14+
}
15+
}
16+
17+
export async function post () {
18+
return { json: { verb: 'post' } }
19+
}
20+
21+
export async function put () {
22+
return { json: { verb: 'put' } }
23+
}
24+
25+
export async function patch () {
26+
return {
27+
headers: {
28+
'ETag': 'e0023aa4f'
29+
}
30+
}
31+
}
32+
33+
export async function destroy () {
34+
return { json: { verb: 'delete' } }
35+
}

src/http/any-catchall/router.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export default async function api (options, req) {
8787
throw new Error(`Issue when trying to import API: ${apiPath}`, { cause: error })
8888
}
8989

90-
let method = mod[req.method.toLowerCase()]
90+
let method = req.method.toLowerCase() !== 'delete' ? mod[req.method.toLowerCase()] : mod['destroy']
9191
isAsyncMiddleware = Array.isArray(method)
9292
if (isAsyncMiddleware)
9393
method = arc.http.apply(null, method)

test/verbs.mjs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)