Skip to content

Commit

Permalink
fix: forward the thrown errorCode and fall back to 500 (#559)
Browse files Browse the repository at this point in the history
* 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
wkillerud authored May 13, 2024
1 parent d8cb0c5 commit a8db5e9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PassThrough } from 'stream';
import compression from '@fastify/compress';
import HttpError from 'http-errors';
import createError from 'http-errors';
import pino from 'pino';
import cors from '@fastify/cors';
import jwt from '@fastify/jwt';
Expand Down Expand Up @@ -187,7 +187,7 @@ const EikService = class EikService {
reply.send(error);
return;
}
reply.send(new HttpError.InternalServerError());
reply.send(createError(error.statusCode || 500));
});


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"homepage": "https://github.com/eik-lib/service#readme",
"dependencies": {
"@eik/core": "1.3.44",
"@eik/core": "1.3.45",
"convict": "6.2.4",
"fastify": "4.26.2",
"@fastify/compress": "6.5.0",
Expand Down
78 changes: 78 additions & 0 deletions test/400.test.js
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();
});

0 comments on commit a8db5e9

Please sign in to comment.