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