Skip to content

Commit

Permalink
fix(logger-plugin): log target port when router option is used
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed May 5, 2024
1 parent 4d3f549 commit b2096e9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- refactor(errors): improve pathFilter error message
- fix(logger-plugin): fix missing target port
- ci(package): npm package provenance
- fix(logger-plugin): log target port when router option is used

## [v3.0.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v3.0.0)

Expand Down
14 changes: 12 additions & 2 deletions src/plugins/default/logger-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,18 @@ export const loggerPlugin: Plugin = (proxyServer, options) => {
const originalUrl = req.originalUrl ?? `${req.baseUrl || ''}${req.url}`;

// construct targetUrl
const target = new URL(options.target as URL);
target.pathname = proxyRes.req.path;
// const port = proxyRes.req?.socket?.autoSelectFamilyAttemptedAddresses?.[0]?.split(':')?.reverse()[0]; //.at(-1),
const port = Object.keys(proxyRes.req?.agent?.sockets || {})?.[0]?.split(':')[1];

const obj = {
protocol: proxyRes.req.protocol,
host: proxyRes.req.host,
port: port,
pathname: proxyRes.req.path,
} as URL;

const target = new URL(`${obj.protocol}//${obj.host}${obj.pathname}`);
target.port = obj.port; // optional port
const targetUrl = target.toString();

const exchange = `[HPM] ${req.method} ${originalUrl} -> ${targetUrl} [${proxyRes.statusCode}]`;
Expand Down
27 changes: 25 additions & 2 deletions test/e2e/http-proxy-middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Mockttp, getLocal, CompletedRequest } from 'mockttp';
import type * as http from 'http';
import type * as express from 'express';
import * as bodyParser from 'body-parser';
import type { Logger } from '../../src/types';

describe('E2E http-proxy-middleware', () => {
describe('http-proxy-middleware creation', () => {
Expand Down Expand Up @@ -433,15 +434,18 @@ describe('E2E http-proxy-middleware', () => {

describe('option.logger', () => {
let logMessages: string[];
let customLogger: Logger;

beforeEach(() => {
logMessages = [];
const customLogger = {
customLogger = {
info: (message: string) => logMessages.push(message),
warn: (message: string) => logMessages.push(message),
error: (message: string) => logMessages.push(message),
};
});

it('should have logged messages', async () => {
agent = request(
createApp(
createProxyMiddleware({
Expand All @@ -451,9 +455,28 @@ describe('E2E http-proxy-middleware', () => {
}),
),
);

await mockTargetServer.forGet('/api/foo/bar').thenReply(200);
await agent.get(`/api/foo/bar`).expect(200);

expect(logMessages).not.toBeUndefined();
expect(logMessages.length).toBe(1);
expect(logMessages.at(0)).toBe(
`[HPM] GET /api/foo/bar -> http://localhost:${mockTargetServer.port}/api/foo/bar [200]`,
);
});

it('should have logged messages', async () => {
it('should have logged messages when router used', async () => {
agent = request(
createApp(
createProxyMiddleware({
router: () => `http://localhost:${mockTargetServer.port}`,
pathFilter: '/api',
logger: customLogger,
}),
),
);

await mockTargetServer.forGet('/api/foo/bar').thenReply(200);
await agent.get(`/api/foo/bar`).expect(200);

Expand Down

0 comments on commit b2096e9

Please sign in to comment.