Skip to content

Commit

Permalink
FIx CURVEBALL_TRUSTPROXY again + add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
evert committed Nov 6, 2024
1 parent 7113421 commit 6859c1f
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 58 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

1.0.2 (????-??-??)
------------------

* Fixed another bug related to CURVEBALL_TRUSTPROXY


1.0.1 (2024-10-30)
------------------

Expand Down
22 changes: 16 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@
"devDependencies": {
"@curveball/http-errors": "^0.5.0",
"@curveball/kernel": "^1.0.0",
"@types/chai": "^4.2.15",
"@types/chai": "^5.0.1",
"@types/co-body": "^6.1.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.19.6",
"@types/node-fetch": "^2.5.8",
"@types/sinon": "^17.0.3",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "^6.18.1",
"chai": "^5.0.0",
"chai": "^5.1.2",
"eslint": "^8.6.0",
"mocha": "^10.2.0",
"node-fetch": "^3.3.0",
Expand Down
2 changes: 1 addition & 1 deletion src/node/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class NodeRequest<T> extends Request<T> {
*/
ip(trustProxy?: boolean): string {

if (trustProxy ?? process.env.CURVEBALL_TRUSTPROXY) {
if (trustProxy===true || process.env.CURVEBALL_TRUSTPROXY) {
const forwardedForHeader = this.headers.get('X-Forwarded-For');
if (forwardedForHeader) {
return forwardedForHeader.split(',')[0].trim();
Expand Down
118 changes: 69 additions & 49 deletions test/application.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { strict as assert } from 'node:assert';
import { Application, middlewareCall, MemoryRequest, Context } from '../src/index.js';
import { Readable, Writable } from 'node:stream';
import { expect } from 'chai';

let lastPort = 5555;
const getPort = () => {
Expand All @@ -10,12 +11,12 @@ const getPort = () => {
describe('Application', () => {
it('should instantiate', () => {
const application = new Application();
expect(application).to.be.an.instanceof(Application);
assert.ok(application instanceof Application);
});

it('should respond to HTTP requests', async () => {
const application = new Application();
application.use((ctx, next) => {
application.use(ctx => {
ctx.response.body = 'string';
});

Expand All @@ -25,16 +26,19 @@ describe('Application', () => {
const response = await fetch('http://localhost:' + port);
const body = await response.text();

expect(body).to.equal('string');
expect(response.headers.get('server')).to.match(/Curveball\//);
expect(response.status).to.equal(200);
assert.equal(body, 'string');
assert.match(
response.headers.get('server')!,
/Curveball\//
);
assert.equal(response.status, 200);

server.close();
});

it('should work with Buffer responses', async () => {
const application = new Application();
application.use((ctx, next) => {
application.use(ctx => {
ctx.response.body = Buffer.from('buffer');
});

Expand All @@ -45,16 +49,19 @@ describe('Application', () => {
const response = await fetch('http://localhost:'+port);
const body = await response.text();

expect(body).to.equal('buffer');
expect(response.headers.get('server')).to.match(/Curveball\//);
expect(response.status).to.equal(200);
assert.equal(body, 'buffer');
assert.match(
response.headers.get('server')!,
/Curveball\//
);
assert.equal(response.status, 200);

server.close();
});

it('should work with Readable stream responses', async () => {
const application = new Application();
application.use((ctx, next) => {
application.use(ctx => {
ctx.response.body = Readable.from(Buffer.from('stream'));
});

Expand All @@ -64,16 +71,18 @@ describe('Application', () => {
const response = await fetch('http://localhost:'+port);
const body = await response.text();

expect(body.substring(0, 6)).to.equal('stream');
expect(response.headers.get('server')).to.match(/Curveball\//);
expect(response.status).to.equal(200);

assert.equal(body, 'stream');
assert.match(
response.headers.get('server')!,
/Curveball\//
);
assert.equal(response.status, 200);
server.close();
});

it('should work with a callback resonse body', async () => {
const application = new Application();
application.use((ctx, next) => {
application.use(ctx => {
ctx.response.body = (stream: Writable) => {
stream.write('callback');
stream.end();
Expand All @@ -85,16 +94,19 @@ describe('Application', () => {
const response = await fetch('http://localhost:'+port);
const body = await response.text();

expect(body).to.equal('callback');
expect(response.headers.get('server')).to.match(/Curveball\//);
expect(response.status).to.equal(200);
assert.equal(body, 'callback');
assert.match(
response.headers.get('server')!,
/Curveball\//
);
assert.equal(response.status, 200);

server.close();
});

it('should automatically JSON-encode objects', async () => {
const application = new Application();
application.use((ctx, next) => {
application.use(ctx => {
ctx.response.body = { foo: 'bar' };
});
const port = getPort();
Expand All @@ -103,16 +115,19 @@ describe('Application', () => {
const response = await fetch('http://localhost:'+port);
const body = await response.text();

expect(body).to.equal('{\n "foo": "bar"\n}');
expect(response.headers.get('server')).to.match(/Curveball\//);
expect(response.status).to.equal(200);
assert.equal(body, '{\n "foo": "bar"\n}');
assert.match(
response.headers.get('server')!,
/Curveball\//
);
assert.equal(response.status, 200);

server.close();
});

it('should handle "null" bodies', async () => {
const application = new Application();
application.use((ctx, next) => {
application.use(ctx => {
ctx.response.body = null;
});
const port = getPort();
Expand All @@ -121,16 +136,18 @@ describe('Application', () => {
const response = await fetch('http://localhost:'+port);
const body = await response.text();

expect(body).to.equal('');
expect(response.headers.get('server')).to.match(/Curveball\//);
expect(response.status).to.equal(200);

assert.equal(body, '');
assert.match(
response.headers.get('server')!,
/Curveball\//
);
assert.equal(response.status, 200);
server.close();
});

it('should throw an exception for unsupported bodies', async () => {
const application = new Application();
application.use((ctx, next) => {
application.use(ctx => {
ctx.response.body = 5;
});
const port = getPort();
Expand All @@ -139,9 +156,12 @@ describe('Application', () => {
const response = await fetch('http://localhost:'+port);
const body = await response.text();

expect(body).to.include(': 500');
expect(response.headers.get('server')).to.match(/Curveball\//);
expect(response.status).to.equal(500);
assert.ok(body.includes(': 500'));
assert.match(
response.headers.get('server')!,
/Curveball\//
);
assert.equal(response.status, 500);

server.close();
});
Expand All @@ -152,7 +172,7 @@ describe('Application', () => {
ctx.response.body = 'hi';
await next();
});
application.use((ctx, next) => {
application.use(ctx => {
ctx.response.headers.set('X-Foo', 'bar');
});
const port = getPort();
Expand All @@ -161,9 +181,9 @@ describe('Application', () => {
const response = await fetch('http://localhost:'+port);
const body = await response.text();

expect(body).to.equal('hi');
expect(response.headers.get('X-Foo')).to.equal('bar');
expect(response.status).to.equal(200);
assert.equal(body, 'hi');
assert.equal(response.headers.get('X-Foo'), 'bar');
assert.equal(response.status, 200);

server.close();
});
Expand All @@ -182,9 +202,9 @@ describe('Application', () => {
const response = await fetch('http://localhost:'+port);
const body = await response.text();

expect(body).to.equal('hi');
expect(response.headers.get('X-Foo')).to.equal('bar');
expect(response.status).to.equal(200);
assert.equal(body, 'hi');
assert.equal(response.headers.get('X-Foo'), 'bar');
assert.equal(response.status, 200);

server.close();
});
Expand All @@ -207,18 +227,18 @@ describe('Application', () => {
const response = await fetch('http://localhost:'+port);
const body = await response.text();

expect(body).to.equal('hi');
expect(response.status).to.equal(200);
assert.equal(body, 'hi');
assert.equal(response.status, 200);

server.close();
});

it('should not call sequential middlewares if next is not called', async () => {
const application = new Application();
application.use((ctx, next) => {
application.use(ctx => {
ctx.response.body = 'hi';
});
application.use((ctx, next) => {
application.use(ctx => {
ctx.response.headers.set('X-Foo', 'bar');
});
const port = getPort();
Expand All @@ -227,9 +247,9 @@ describe('Application', () => {
const response = await fetch('http://localhost:'+port);
const body = await response.text();

expect(body).to.equal('hi');
expect(response.headers.get('X-Foo')).to.equal(null);
expect(response.status).to.equal(200);
assert.equal(body, 'hi');
assert.equal(response.headers.get('X-Foo'), null);
assert.equal(response.status, 200);

server.close();
});
Expand All @@ -249,8 +269,8 @@ describe('Application', () => {

await fetch('http://localhost:'+port);

expect(error).to.be.an.instanceof(Error);
expect(error.message).to.equal('hi');
assert.ok(error instanceof Error);
assert.equal(error!.message, 'hi');

server.close();
});
Expand All @@ -266,7 +286,7 @@ describe('Application', () => {
const response = await fetch('http://localhost:'+port);
const body = await response.text();

expect(body).to.include(': 500');
assert.ok(body.includes(': 500'));

server.close();
});
Expand Down
Loading

0 comments on commit 6859c1f

Please sign in to comment.