Skip to content

Fix for issue 27 support for log, info,error, warn and verbose methods of context objects #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/IncomingMessage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-underscore-dangle */
import EventEmitter from "events";

const NOOP = () => {};
const NOOP = () => { };

function removePortFromAddress(address) {
return address
Expand All @@ -20,8 +20,8 @@ function createConnectionObject(context) {
const xForwardedFor = req.headers ? req.headers["x-forwarded-for"] : undefined;

return {
encrypted : req.originalUrl && req.originalUrl.toLowerCase().startsWith("https"),
remoteAddress : removePortFromAddress(xForwardedFor)
encrypted: req.originalUrl && req.originalUrl.toLowerCase().startsWith("https"),
remoteAddress: removePortFromAddress(xForwardedFor)
};
}

Expand All @@ -37,14 +37,12 @@ function createConnectionObject(context) {
*/
function sanitizeContext(context) {
const sanitizedContext = {
...context,
log : context.log.bind(context)
...context
};

// We don't want the developper to mess up express flow
// See https://github.com/yvele/azure-function-express/pull/12#issuecomment-336733540
delete sanitizedContext.done;

return sanitizedContext;
}

Expand Down
28 changes: 12 additions & 16 deletions test/ExpressAdapter.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { ExpressAdapter } from "../src";

const NOOP = () => {};

describe("ExpressAdapter", () => {

it("Should work", done => {
Expand All @@ -16,17 +14,16 @@ describe("ExpressAdapter", () => {
});

const context = {
log : NOOP,
bindings : { req: { originalUrl: "http://foo.com/bar" } },
done : () => {
bindings: { req: { originalUrl: "http://foo.com/bar" } },
done: () => {
expect(listenerCalled).toBe(true);

// Response that will be sent to Azure Function runtime
expect(context.res).toEqual({
body : "body",
headers : {},
isRaw : true,
status : 200
body: "body",
headers: {},
isRaw: true,
status: 200
});
done();
}
Expand All @@ -48,17 +45,16 @@ describe("ExpressAdapter", () => {
});

const context = {
log : NOOP,
bindings : { req: { originalUrl: "http://foo.com/bar" } },
done : () => {
bindings: { req: { originalUrl: "http://foo.com/bar" } },
done: () => {
expect(listenerCalled).toBe(true);

// Response that will be sent to Azure Function runtime
expect(context.res).toEqual({
body : "body",
headers : {},
isRaw : true,
status : 200
body: "body",
headers: {},
isRaw: true,
status: 200
});
done();
}
Expand Down
59 changes: 27 additions & 32 deletions test/IncomingMessage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,72 @@ describe("IncomingMessage", () => {
it("Should work", () => {

const context = {
bindings : {
req : {
originalUrl : "https://foo.com/bar",
headers : { "x-forwarded-for": "192.168.0.1:57996" }
bindings: {
req: {
originalUrl: "https://foo.com/bar",
headers: { "x-forwarded-for": "192.168.0.1:57996" }
}
},
log : () => {}
}
};

const req = new IncomingMessage(context);
req.resume();
req.socket.destroy();

expect(req).toMatchObject({
url : "https://foo.com/bar",
connection : {
encrypted : true,
remoteAddress : "192.168.0.1"
url: "https://foo.com/bar",
connection: {
encrypted: true,
remoteAddress: "192.168.0.1"
}
});
});

it("Should work with no headers", () => {

const context = {
bindings : {
req : {
originalUrl : "http://foo.com/bar"
bindings: {
req: {
originalUrl: "http://foo.com/bar"
}
},
log : () => {}
}
};

const req = new IncomingMessage(context);
req.resume();
req.socket.destroy();

expect(req).toMatchObject({
url : "http://foo.com/bar",
connection : {
encrypted : false,
remoteAddress : undefined
url: "http://foo.com/bar",
connection: {
encrypted: false,
remoteAddress: undefined
}
});
});

it("Should work with a full native context object", () => {

const context = {
invocationId : "f0f6e586-0b79-4407-aa53-97919f45eba5",
bindingData : { foo: "bar" },
bindings : {
req : {
originalUrl : "http://foo.com/bar"
invocationId: "f0f6e586-0b79-4407-aa53-97919f45eba5",
bindingData: { foo: "bar" },
bindings: {
req: {
originalUrl: "http://foo.com/bar"
}
},
log : () => {},
done : () => {}
done: () => { }
};

const req = new IncomingMessage(context);
req.resume();
req.socket.destroy();

expect(req).toMatchObject({
url : "http://foo.com/bar",
connection : {
encrypted : false,
remoteAddress : undefined
url: "http://foo.com/bar",
connection: {
encrypted: false,
remoteAddress: undefined
}
});

Expand All @@ -82,8 +79,6 @@ describe("IncomingMessage", () => {
expect(req.context.invocationId).toBe(context.invocationId);
expect(req.context.bindingData).toBe(context.bindingData);
expect(req.context.bindings).toBe(context.bindings);
expect(req.context.log).not.toBe(context.log);
expect(req.context.log).toBeInstanceOf(Function);
expect(req.context.done).toBeUndefined(); // We don't want to pass done

});
Expand Down
26 changes: 12 additions & 14 deletions test/expressIntegration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ describe("express integration", () => {

// 3. Mock Azure Function context
var context = {
bindings : { req: { method: "GET", originalUrl: "https://lol.com/api/foo/bar" } },
log : () => { throw new Error("Log should not be called"); },
done : (error) => {
bindings: { req: { method: "GET", originalUrl: "https://lol.com/api/foo/bar" } },
done: (error) => {
expect(error).toBeUndefined();
expect(context.res.status).toBe(200);
expect(context.res.body).toBe('{"foo":"foo","bar":"bar"}');
expect(context.res.headers).toEqual({
"X-Powered-By" : "Express",
"Cache-Control" : "max-age=600",
"Content-Type" : "application/json; charset=utf-8",
"Content-Length" : "25",
ETag : 'W/"19-0CKEGOfZ5AYCM4LPaa4gzWL6olU"'
"X-Powered-By": "Express",
"Cache-Control": "max-age=600",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "25",
ETag: 'W/"19-0CKEGOfZ5AYCM4LPaa4gzWL6olU"'
});

done();
Expand All @@ -55,16 +54,15 @@ describe("express integration", () => {

// 3. Mock Azure Function context
var context = {
bindings : { req: { method: "GET", originalUrl: "https://lol.com/api/foo/bar" } },
log : () => { throw new Error("Log should not be called"); },
done : (error) => {
bindings: { req: { method: "GET", originalUrl: "https://lol.com/api/foo/bar" } },
done: (error) => {
expect(error).toBeUndefined();
expect(context.res.status).toBe(200);
expect(context.res.body).toBe('{"foo":"foo","bar":"bar"}');
expect(context.res.headers).toEqual({
"Content-Type" : "application/json; charset=utf-8",
"Content-Length" : "25",
ETag : 'W/"19-0CKEGOfZ5AYCM4LPaa4gzWL6olU"'
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "25",
ETag: 'W/"19-0CKEGOfZ5AYCM4LPaa4gzWL6olU"'
});

done();
Expand Down