Skip to content

Commit e99fa3a

Browse files
evilebottnawihiroppy
authored andcommitted
test: proxy supports http methods (#1928)
1 parent 856169e commit e99fa3a

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"@commitlint/cli": "^7.6.1",
7272
"@commitlint/config-conventional": "^7.6.0",
7373
"babel-loader": "^8.0.6",
74+
"body-parser": "^1.19.0",
7475
"commitlint-azure-pipelines-cli": "^1.0.1",
7576
"copy-webpack-plugin": "^5.0.3",
7677
"css-loader": "^2.1.1",

test/Proxy.test.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const path = require('path');
44
const request = require('supertest');
55
const express = require('express');
6+
const bodyParser = require('body-parser');
67
const WebSocket = require('ws');
78
const testServer = require('./helpers/test-server');
89
const config = require('./fixtures/proxy-config/webpack.config');
@@ -289,4 +290,100 @@ describe('Proxy', () => {
289290
testServer.close(done);
290291
});
291292
});
293+
294+
describe('should support http methods', () => {
295+
let server;
296+
let req;
297+
let listener;
298+
const proxyTarget = {
299+
target: 'http://localhost:9000',
300+
};
301+
302+
beforeAll((done) => {
303+
const proxy = express();
304+
305+
// Parse application/x-www-form-urlencoded
306+
proxy.use(bodyParser.urlencoded({ extended: false }));
307+
308+
// Parse application/json
309+
proxy.use(bodyParser.json());
310+
311+
proxy.get('/get', (proxyReq, res) => {
312+
res.send('GET method from proxy');
313+
});
314+
315+
proxy.head('/head', (proxyReq, res) => {
316+
res.send('HEAD method from proxy');
317+
});
318+
319+
proxy.post('/post-x-www-form-urlencoded', (proxyReq, res) => {
320+
const id = proxyReq.body.id;
321+
322+
res.status(200).send(`POST method from proxy (id: ${id})`);
323+
});
324+
325+
proxy.post('/post-application-json', (proxyReq, res) => {
326+
const id = proxyReq.body.id;
327+
328+
res.status(200).send({ answer: `POST method from proxy (id: ${id})` });
329+
});
330+
331+
proxy.delete('/delete', (proxyReq, res) => {
332+
res.send('DELETE method from proxy');
333+
});
334+
335+
listener = proxy.listen(9000);
336+
337+
server = testServer.start(
338+
config,
339+
{
340+
contentBase,
341+
proxy: {
342+
'**': proxyTarget,
343+
},
344+
},
345+
done
346+
);
347+
req = request(server.app);
348+
});
349+
350+
afterAll((done) => {
351+
testServer.close(() => {
352+
listener.close();
353+
done();
354+
});
355+
});
356+
357+
it('GET method', (done) => {
358+
req.get('/get').expect(200, 'GET method from proxy', done);
359+
});
360+
361+
it('HEAD method', (done) => {
362+
req.head('/head').expect(200, done);
363+
});
364+
365+
it('POST method (application/x-www-form-urlencoded)', (done) => {
366+
req
367+
.post('/post-x-www-form-urlencoded')
368+
.send('id=1')
369+
.expect(200, 'POST method from proxy (id: 1)', done);
370+
});
371+
372+
it('POST method (application/json)', (done) => {
373+
req
374+
.post('/post-application-json')
375+
.send({ id: '1' })
376+
.set('Accept', 'application/json')
377+
.expect('Content-Type', /json/)
378+
.expect(
379+
200,
380+
JSON.stringify({ answer: 'POST method from proxy (id: 1)' }),
381+
done
382+
);
383+
});
384+
385+
it('DELETE method', (done) => {
386+
req.delete('/delete').expect(200, 'DELETE method from proxy', done);
387+
});
388+
});
292389
});

0 commit comments

Comments
 (0)