Skip to content
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

[#171017085] fix handling of application/x-www-form-urlencoded #3

Merged
merged 3 commits into from
Jan 31, 2020
Merged
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
5 changes: 5 additions & 0 deletions .auto-changelog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"issuePattern": "\\[#(\\d+)\\]",
"issueUrl": "https://www.pivotaltracker.com/story/show/{id}",
"breakingPattern": "BREAKING CHANGE:"
}
23 changes: 23 additions & 0 deletions .release-it.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// remember to set up a GitHub token before running release-it
// ie. export GITHUB_TOKEN="deadbeef..."

module.exports = {
git: {
tagName: "v${version}",
addFiles: ["package.json", "CHANGELOG.md"],
commitMessage: "chore: release ${version}",
changelog:
"npx auto-changelog --config .auto-changelog.json --stdout --commit-limit false --unreleased --template preview.hbs"
},
hooks: {
"before:release": [
"npx auto-changelog --config .auto-changelog.json --package"
]
},
github: {
release: true
},
npm: {
publish: true
}
};
6 changes: 6 additions & 0 deletions HttpTest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ app.get("/api/HttpTest/headers", (req, res) => {
});
});

app.post("/api/HttpTest/encoded", (req, res) => {
res.json({
body: req.body
});
});

const httpTrigger = createAzureFunctionHandler(app);

export default httpTrigger;
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "io-functions-express",
"version": "0.1.1",
"version": "1.0.0",
"description": "Express adapter for Azure Functions",
"repository": "https://github.com/teamdigitale/io-functions-express",
"author": "https://teamdigitale.governo.it",
"repository": "https://github.com/pagopa/io-functions-express",
"author": "https://pagopa.gov.it",
"license": "MIT",
"files": [
"dist/src"
Expand All @@ -23,24 +23,26 @@
},
"devDependencies": {
"@azure/functions": "^1.0.3",
"@types/express": "^4.17.0",
"@types/express": "^4.17.2",
"@types/express-serve-static-core": "^4.17.2",
"@types/jest": "^24.0.13",
"@types/node": "10.14.1",
"axios": "^0.19.0",
"danger": "^8.0.0",
"danger": "^9.2.0",
"danger-plugin-digitalcitizenship": "*",
"express": "^4.17.1",
"italia-tslint-rules": "*",
"jest": "^24.8.0",
"npm-run-all": "^4.1.5",
"prettier": "^1.12.1",
"rimraf": "^2.6.2",
"tree-kill": "^1.2.2",
"ts-jest": "^24.0.2",
"tslint": "^5.1.0",
"typescript": "^3.5.0"
},
"peerDependencies": {
"express": "^4.0.0"
"express": "^4.17.2"
},
"jest": {
"testEnvironment": "node",
Expand Down
13 changes: 13 additions & 0 deletions preview.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{#each releases}}
{{#if @first}}
{{#each merges}}
- {{{message}}}{{#if href}} [`#{{id}}`]({{href}}){{/if}}
{{/each}}
{{#each fixes}}
- {{{commit.subject}}}{{#each fixes}}{{#if href}} [`#{{id}}`]({{href}}){{/if}}{{/each}}
{{/each}}
{{#each commits}}
- {{#if breaking}}**Breaking change:** {{/if}}{{{subject}}}{{#if href}} [`{{shorthash}}`]({{href}}){{/if}}
{{/each}}
{{/if}}
{{/each}}
14 changes: 10 additions & 4 deletions src/IncomingMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from "@azure/functions";
import EventEmitter = require("events");
import { Socket } from "net";
import { Readable } from "stream";
import { TLSSocket } from "tls";

const NOOP = () => true;
Expand Down Expand Up @@ -54,9 +54,11 @@ function sanitizeContext(context: Context): Context {
/**
* Request object wrapper
*
* see also https://github.com/yvele/azure-function-express/pull/31
*
* @private
*/
export default class IncomingMessage extends EventEmitter {
export default class IncomingMessage extends Readable {
/**
* Note: IncomingMessage assumes that all HTTP in is binded to "req" property
*/
Expand All @@ -65,13 +67,17 @@ export default class IncomingMessage extends EventEmitter {

const req = context.req || ({} as NonNullable<Context["req"]>);

// Push the request body onto this stream
this.push(context.bindings.req.rawBody);

// Close the stream
this.push(null);

Object.assign(this, {
...req, // Inherit
_readableState: { pipesCount: 0 }, // To make unpipe happy
connection: createConnectionObject(context),
context: sanitizeContext(context), // Specific to Azure Function
headers: req.headers || {}, // Should always have a headers object
resume: NOOP,
socket: { destroy: NOOP },
// tslint:disable-next-line: no-any
url: (req as any).originalUrl
Expand Down
29 changes: 28 additions & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import axios from "axios";
import { ChildProcess, spawn } from "child_process";
import * as qs from "querystring";

// do not convert to default import despite vscode hints :-)
import * as treeKill from "tree-kill";

// tslint:disable-next-line: no-let
let spawnedFunc: ChildProcess;
// tslint:disable-next-line: no-let
let funcAddress: string;
// tslint:disable-next-line: no-let
let isStopping = false;

// do not reject promise on non-200 statuses
// tslint:disable-next-line: no-object-mutation
axios.defaults.validateStatus = () => true;

const startFunc = () =>
Expand All @@ -14,6 +22,7 @@ const startFunc = () =>
const func = spawn("func", ["start"]);
func.stdout.on("data", data => {
if (!isStopping) {
// tslint:disable-next-line: no-console
console.log(`${data}`);
}
const matches = String(data).match(/Now listening on: ([^\s]+)/);
Expand All @@ -28,7 +37,7 @@ const startFunc = () =>

const stopFunc = (p: ChildProcess) => {
isStopping = true;
p.kill();
treeKill(p.pid);
};

beforeAll(done => {
Expand Down Expand Up @@ -108,4 +117,22 @@ describe("Azure functions handler", () => {
"x-custom-header-out": "value"
});
});

it("should parse urlencoded request", async () => {
const result = await axios.post(
`${funcAddress}/api/HttpTest/encoded`,
qs.stringify({
body: "foobar"
}),
{
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
}
);
expect(result.status).toEqual(200);
expect(result.data).toMatchObject({
body: "body=foobar"
});
});
});
Loading