From 6f79a44a773755ae631ab84ba5b0e28a204606bf Mon Sep 17 00:00:00 2001 From: Chris Midgley Date: Sun, 18 Apr 2021 16:32:21 +0100 Subject: [PATCH] feat: extract fixRequestBody to external handler --- src/handlers/fix-request-body.ts | 26 ++++++++++++++++++++++++++ src/handlers/public.ts | 1 + src/http-proxy-middleware.ts | 25 ------------------------- test/e2e/_utils.ts | 2 +- test/e2e/http-proxy-middleware.spec.ts | 4 +++- 5 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 src/handlers/fix-request-body.ts diff --git a/src/handlers/fix-request-body.ts b/src/handlers/fix-request-body.ts new file mode 100644 index 00000000..c5492ed8 --- /dev/null +++ b/src/handlers/fix-request-body.ts @@ -0,0 +1,26 @@ +import { ClientRequest } from 'http'; +import type { Request } from '../types'; +import * as querystring from 'querystring'; + +/** + * Fix proxied body if bodyParser is involved. + */ +export function fixRequestBody(proxyReq: ClientRequest, req: Request) { + if (!req.body || !Object.keys(req.body).length) { + return; + } + + const contentType = proxyReq.getHeader('Content-Type') as string; + const writeBody = (bodyData: string) => { + proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData)); + proxyReq.write(bodyData); + }; + + if (contentType.includes('application/json')) { + writeBody(JSON.stringify(req.body)); + } + + if (contentType === 'application/x-www-form-urlencoded') { + writeBody(querystring.stringify(req.body)); + } +} diff --git a/src/handlers/public.ts b/src/handlers/public.ts index f5935888..dd7722a6 100644 --- a/src/handlers/public.ts +++ b/src/handlers/public.ts @@ -1 +1,2 @@ export { responseInterceptor } from './response-interceptor'; +export { fixRequestBody } from './fix-request-body'; diff --git a/src/http-proxy-middleware.ts b/src/http-proxy-middleware.ts index cc3a6d71..b93ae92f 100644 --- a/src/http-proxy-middleware.ts +++ b/src/http-proxy-middleware.ts @@ -1,10 +1,8 @@ -import { ClientRequest } from 'http'; import type * as https from 'https'; import type * as express from 'express'; import type { Filter, Request, RequestHandler, Response, Options } from './types'; import * as httpProxy from 'http-proxy'; import { createConfig, Config } from './config-factory'; -import * as querystring from 'querystring'; import * as contextMatcher from './context-matcher'; import * as handlers from './_handlers'; import { getArrow, getInstance } from './logger'; @@ -35,9 +33,6 @@ export class HttpProxyMiddleware { // log errors for debug purpose this.proxy.on('error', this.logError); - // fix proxied body if bodyParser is involved - this.proxy.on('proxyReq', this.fixBody); - // https://github.com/chimurai/http-proxy-middleware/issues/19 // expose function to upgrade externally (this.middleware as any).upgrade = (req, socket, head) => { @@ -198,24 +193,4 @@ export class HttpProxyMiddleware { this.logger.error(errorMessage, requestHref, targetHref, err.code || err, errReference); }; - - private fixBody = (proxyReq: ClientRequest, req: Request) => { - if (!req.body || !Object.keys(req.body).length) { - return; - } - - const contentType = proxyReq.getHeader('Content-Type') as string; - const writeBody = (bodyData: string) => { - proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData)); - proxyReq.write(bodyData); - }; - - if (contentType.includes('application/json')) { - writeBody(JSON.stringify(req.body)); - } - - if (contentType === 'application/x-www-form-urlencoded') { - writeBody(querystring.stringify(req.body)); - } - }; } diff --git a/test/e2e/_utils.ts b/test/e2e/_utils.ts index c898b6b4..b429bfb6 100644 --- a/test/e2e/_utils.ts +++ b/test/e2e/_utils.ts @@ -1,7 +1,7 @@ import * as express from 'express'; import { Express, RequestHandler } from 'express'; -export { createProxyMiddleware, responseInterceptor } from '../../dist/index'; +export { createProxyMiddleware, responseInterceptor, fixRequestBody } from '../../dist/index'; export function createApp(...middleware: RequestHandler[]): Express { const app = express(); diff --git a/test/e2e/http-proxy-middleware.spec.ts b/test/e2e/http-proxy-middleware.spec.ts index fa20fcad..05b0dc74 100644 --- a/test/e2e/http-proxy-middleware.spec.ts +++ b/test/e2e/http-proxy-middleware.spec.ts @@ -1,4 +1,4 @@ -import { createProxyMiddleware, createApp, createAppWithPath } from './_utils'; +import { createProxyMiddleware, createApp, createAppWithPath, fixRequestBody } from './_utils'; import * as request from 'supertest'; import { Mockttp, getLocal, CompletedRequest } from 'mockttp'; import { Request, Response } from '../../src/types'; @@ -86,6 +86,7 @@ describe('E2E http-proxy-middleware', () => { bodyParser.urlencoded({ extended: false }), createProxyMiddleware('/api', { target: `http://localhost:${mockTargetServer.port}`, + onProxyReq: fixRequestBody, }) ) ); @@ -102,6 +103,7 @@ describe('E2E http-proxy-middleware', () => { bodyParser.json(), createProxyMiddleware('/api', { target: `http://localhost:${mockTargetServer.port}`, + onProxyReq: fixRequestBody, }) ) );