Skip to content

Commit

Permalink
Merge pull request #39 from Michaelpalacce/develop
Browse files Browse the repository at this point in the history
[develop] v28.2.1
  • Loading branch information
Michaelpalacce committed Aug 8, 2020
2 parents 30b33e2 + d433eb0 commit ff9a360
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 19 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,11 @@ The event request is an object that is created by the server and passed through
**errorHandler: ErrorHandler**
- Default or Custom error handler that will be called in case of an error

**disableXPoweredBy: Boolean**
- Flag telling the framework if it should sent the X-Powered-By header.
- Disabling it will
- Defaults to false

***
#### Functions exported by the event request:

Expand All @@ -708,6 +713,7 @@ The event request is an object that is created by the server and passed through
- Sends the response to the user with the specified statusCode
- If the response is not a String, it will be JSON.stringified. If that fails, an error will be thrown: app.er.send.error
- setResponseHeader will be called with the calculated Content-Length
- setResponseHeader will be called with X-Powered-By: event_request. This can be disabled by doing: `eventRequest.disableXPoweredBy = true;`
- calls this.end() with the payload

**end( ...args ): void**
Expand Down
3 changes: 3 additions & 0 deletions UPDATELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
28.2.1
- X-Powered-By added when using EventRequest.send()

28.2.0
- Routers can now be added more than once using the router.add( 'path', router ) syntax

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"test": "nyc --reporter=lcov --reporter=text-summary node test.js --debug",
"deploy": "npm run test && npm publish"
},
"version": "28.2.0",
"version": "28.2.1",
"devDependencies": {
"nyc": "^15.1.0"
}
Expand Down
33 changes: 19 additions & 14 deletions server/event_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,21 @@ class EventRequest extends EventEmitter
list[parts.shift().trim()] = decodeURI( parts.join( '=' ) );
});

this.query = parsedUrl.query;
this.clientIp = request.socket.remoteAddress;
this.path = parsedUrl.pathname.trim();
this.method = request.method.toUpperCase();
this.headers = request.headers;
this.validation = ValidationHandler;
this.request = request;
this.response = response;
this.cookies = list;
this.finished = false;
this.extra = {};
this.params = {};
this.block = [];
this.errorHandler = null;
this.query = parsedUrl.query;
this.clientIp = request.socket.remoteAddress;
this.path = parsedUrl.pathname.trim();
this.method = request.method.toUpperCase();
this.headers = request.headers;
this.validation = ValidationHandler;
this.request = request;
this.response = response;
this.cookies = list;
this.finished = false;
this.extra = {};
this.params = {};
this.block = [];
this.errorHandler = null;
this.disableXPoweredBy = false;

// We do this so we can pass the event.next function by reference
const self = this;
Expand Down Expand Up @@ -84,6 +85,7 @@ class EventRequest extends EventEmitter
this.cookies = undefined;
this.params = undefined;
this.clientIp = undefined;
this.disableXPoweredBy = undefined;
this.finished = true;

this.emit( 'finished' );
Expand Down Expand Up @@ -169,6 +171,9 @@ class EventRequest extends EventEmitter
{
payload = typeof response === 'string' ? response : JSON.stringify( response );
this.setResponseHeader( 'Content-Length', payload.length );

if ( this.disableXPoweredBy === false )
this.setResponseHeader( 'X-Powered-By', 'event_request' );
}
catch ( e )
{
Expand Down
6 changes: 4 additions & 2 deletions tests/server/event_request_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ test({
assert.equal( eventRequest.extra, undefined );
assert.equal( eventRequest.cookies, undefined );
assert.equal( eventRequest.params, undefined );
assert.equal( eventRequest.disableXPoweredBy, undefined );
assert.equal( eventRequest.finished, true );
assert.equal( eventRequest.listeners( 'test' ).length, 0 );

Expand Down Expand Up @@ -509,9 +510,10 @@ test({
shouldReturn : () => { setResponseHeader = true; },
with : [
['Location', redirectUrl],
['Content-Length', undefined]
['Content-Length', undefined],
['X-Powered-By', 'event_request']
],
called : 2
called : 3
});

eventRequest.redirect( redirectUrl, 302 );
Expand Down
35 changes: 33 additions & 2 deletions tests/server/server_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1938,13 +1938,44 @@ test({
});

test({
message : 'Server.testServerAddsXPoweredBy.does.not.anymore',
message : 'Server.testServerAddsXPoweredBy',
test : ( done ) => {
app.get( '/textServerAddsXPoweredBy', event => event.send( 'ok' ) );

helpers.sendServerRequest( `/textServerAddsXPoweredBy` ).then( ( response ) => {
assert.equal( response.body.toString(), 'ok' );
assert.equal( typeof response.headers['x-powered-by'] === 'undefined', true );
assert.equal( response.headers['x-powered-by'], 'event_request' );

done();
} ).catch( done );
}
});

test({
message : 'Server.testServerAddsXPoweredBy.when.disabled',
test : ( done ) => {
app.get( '/textServerAddsXPoweredByWhenDisabled', ( event ) => {
event.disableXPoweredBy = true;
event.send( 'ok' );
});

helpers.sendServerRequest( `/textServerAddsXPoweredByWhenDisabled` ).then( ( response ) => {
assert.equal( response.body.toString(), 'ok' );
assert.equal( response.headers['x-powered-by'], undefined );

done();
} ).catch( done );
}
});

test({
message : 'Server.testServerAddsXPoweredBy.when.send.is.not.used',
test : ( done ) => {
app.get( '/textServerAddsXPoweredByWithoutSend', event => event.end( 'ok' ) );

helpers.sendServerRequest( `/textServerAddsXPoweredByWithoutSend` ).then( ( response ) => {
assert.equal( response.body.toString(), 'ok' );
assert.equal( response.headers['x-powered-by'], undefined );

done();
} ).catch( done );
Expand Down

0 comments on commit ff9a360

Please sign in to comment.