• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

chimurai / http-proxy-middleware / 18263701359

05 Oct 2025 08:07PM UTC coverage: 97.15%. Remained the same
18263701359

push

github

web-flow
ci(ci.yml): unpin node 24 (#1148)

* ci(ci.yml): unpin node 24

* chore(package): reinstall mockttp deps

* build(mockttp): use 2048 bits in generateCACertificate

170 of 183 branches covered (92.9%)

409 of 421 relevant lines covered (97.15%)

24.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.15
/src/handlers/fix-request-body.ts
1
import type * as http from 'node:http';
2
import * as querystring from 'node:querystring';
13✔
3

4
export type BodyParserLikeRequest = http.IncomingMessage & { body?: any };
5

6
/**
7
 * Fix proxied body if bodyParser is involved.
8
 */
9
export function fixRequestBody<TReq extends BodyParserLikeRequest = BodyParserLikeRequest>(
13✔
10
  proxyReq: http.ClientRequest,
11
  req: TReq,
12
): void {
13
  // skip fixRequestBody() when req.readableLength not 0 (bodyParser failure)
14
  if (req.readableLength !== 0) {
13✔
15
    return;
1✔
16
  }
17

18
  const requestBody = req.body;
12✔
19

20
  if (!requestBody) {
12✔
21
    return;
1✔
22
  }
23

24
  const contentType = proxyReq.getHeader('Content-Type') as string;
11✔
25

26
  if (!contentType) {
11!
27
    return;
×
28
  }
29

30
  const writeBody = (bodyData: string) => {
11✔
31
    proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
11✔
32
    proxyReq.write(bodyData);
11✔
33
  };
34

35
  // Use if-elseif to prevent multiple writeBody/setHeader calls:
36
  // Error: "Cannot set headers after they are sent to the client"
37
  if (contentType.includes('application/json') || contentType.includes('+json')) {
11✔
38
    writeBody(JSON.stringify(requestBody));
5✔
39
  } else if (contentType.includes('application/x-www-form-urlencoded')) {
6✔
40
    writeBody(querystring.stringify(requestBody));
3✔
41
  } else if (contentType.includes('multipart/form-data')) {
3✔
42
    writeBody(handlerFormDataBodyData(contentType, requestBody));
2✔
43
  } else if (contentType.includes('text/plain')) {
1!
44
    writeBody(requestBody);
1✔
45
  }
46
}
47

48
/**
49
 * format FormData data
50
 * @param contentType
51
 * @param data
52
 * @returns
53
 */
54
function handlerFormDataBodyData(contentType: string, data: any) {
55
  const boundary = contentType.replace(/^.*boundary=(.*)$/, '$1');
2✔
56
  let str = '';
2✔
57
  for (const [key, value] of Object.entries(data)) {
2✔
58
    str += `--${boundary}\r\nContent-Disposition: form-data; name="${key}"\r\n\r\n${value}\r\n`;
2✔
59
  }
60
  return str;
2✔
61
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc