-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: forward the thrown errorCode and fall back to 500 (#559)
* fix: forward the thrown errorCode and fall back to 500 Handlers may also throw 400. In any case status codes determined by handlers should be respected here, not swallowed. * test: add test case for 400 bad request Fails until we get an updated core * fix: update eik/core
- Loading branch information
Showing
3 changed files
with
81 additions
and
3 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
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
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import FormData from 'form-data'; | ||
import Fastify from 'fastify'; | ||
import fetch from 'node-fetch'; | ||
import path from 'path'; | ||
import tap from 'tap'; | ||
import url from 'url'; | ||
import fs from 'fs'; | ||
|
||
import Sink from '@eik/core/lib/sinks/test.js'; | ||
import Server from '../lib/main.js'; | ||
|
||
const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); | ||
|
||
const FIXTURE_PKG = path.resolve(__dirname, '../fixtures/archive.tgz'); | ||
|
||
tap.test('400 - GET request with non-existing hostname', async (t) => { | ||
const sink = new Sink(); | ||
const service = new Server({ customSink: sink }); | ||
|
||
const app = Fastify({ | ||
ignoreTrailingSlash: true, | ||
}); | ||
app.register(service.api()); | ||
|
||
const address = await app.listen({ port: 0, host: '127.0.0.1' }); | ||
|
||
let formData = new FormData(); | ||
formData.append('key', 'change_me'); | ||
|
||
const res = await fetch(`${address}/auth/login`, { | ||
method: 'POST', | ||
body: formData, | ||
headers: formData.getHeaders(), | ||
}); | ||
|
||
const { token } = await res.json(); | ||
|
||
formData = new FormData(); | ||
formData.append('package', fs.createReadStream(FIXTURE_PKG)); | ||
|
||
// PUT files on server so we don't get 404 | ||
const uploaded = await fetch(`${address}/pkg/@cuz/fuzz/1.4.8`, { | ||
method: 'PUT', | ||
body: formData, | ||
redirect: 'manual', | ||
headers: { ...formData.getHeaders(), Authorization: `Bearer ${token}` }, | ||
}); | ||
|
||
t.equal( | ||
uploaded.status, | ||
303, | ||
'on PUT of package, server should respond with a 303 redirect', | ||
); | ||
t.equal( | ||
uploaded.headers.get('location'), | ||
`/pkg/@cuz/fuzz/1.4.8`, | ||
'on PUT of package, server should respond with a location header', | ||
); | ||
|
||
// GET file from server | ||
const response = await fetch( | ||
`${address}/pkg/@cuz/fuzz/1.4.8/main/index.js`, | ||
{ | ||
method: 'GET', | ||
headers: { | ||
Host: 'leethaxorz.ai', | ||
}, | ||
}, | ||
); | ||
|
||
t.equal( | ||
response.status, | ||
400, | ||
'server should respond with a 400 Bad Request', | ||
); | ||
|
||
await app.close(); | ||
}); |