|
1 | | -import { createProxyMiddleware, createApp, createAppWithPath } from './_utils'; |
| 1 | +import { createProxyMiddleware, createApp, createAppWithPath, fixRequestBody } from './test-kit'; |
2 | 2 | import * as request from 'supertest'; |
3 | 3 | import { Mockttp, getLocal, CompletedRequest } from 'mockttp'; |
4 | 4 | import { Request, Response } from '../../src/types'; |
5 | 5 | import { NextFunction } from 'express'; |
| 6 | +import * as bodyParser from 'body-parser'; |
6 | 7 |
|
7 | 8 | describe('E2E http-proxy-middleware', () => { |
8 | 9 | describe('http-proxy-middleware creation', () => { |
@@ -78,6 +79,44 @@ describe('E2E http-proxy-middleware', () => { |
78 | 79 | }); |
79 | 80 | }); |
80 | 81 |
|
| 82 | + describe('basic setup with configured body-parser', () => { |
| 83 | + it('should proxy request body from form', async () => { |
| 84 | + agent = request( |
| 85 | + createApp( |
| 86 | + bodyParser.urlencoded({ extended: false }), |
| 87 | + createProxyMiddleware('/api', { |
| 88 | + target: `http://localhost:${mockTargetServer.port}`, |
| 89 | + onProxyReq: fixRequestBody, |
| 90 | + }) |
| 91 | + ) |
| 92 | + ); |
| 93 | + |
| 94 | + await mockTargetServer.post('/api').thenCallback((req) => { |
| 95 | + expect(req.body.text).toBe('foo=bar&bar=baz'); |
| 96 | + return { status: 200 }; |
| 97 | + }); |
| 98 | + await agent.post('/api').send('foo=bar').send('bar=baz').expect(200); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should proxy request body from json', async () => { |
| 102 | + agent = request( |
| 103 | + createApp( |
| 104 | + bodyParser.json(), |
| 105 | + createProxyMiddleware('/api', { |
| 106 | + target: `http://localhost:${mockTargetServer.port}`, |
| 107 | + onProxyReq: fixRequestBody, |
| 108 | + }) |
| 109 | + ) |
| 110 | + ); |
| 111 | + |
| 112 | + await mockTargetServer.post('/api').thenCallback((req) => { |
| 113 | + expect(req.body.json).toEqual({ foo: 'bar', bar: 'baz', doubleByte: '文' }); |
| 114 | + return { status: 200 }; |
| 115 | + }); |
| 116 | + await agent.post('/api').send({ foo: 'bar', bar: 'baz', doubleByte: '文' }).expect(200); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
81 | 120 | describe('custom context matcher/filter', () => { |
82 | 121 | it('should have response body: "HELLO WEB"', async () => { |
83 | 122 | const filter = (path, req) => { |
|
0 commit comments