|
3 | 3 | const path = require('path');
|
4 | 4 | const request = require('supertest');
|
5 | 5 | const express = require('express');
|
| 6 | +const bodyParser = require('body-parser'); |
6 | 7 | const WebSocket = require('ws');
|
7 | 8 | const testServer = require('./helpers/test-server');
|
8 | 9 | const config = require('./fixtures/proxy-config/webpack.config');
|
@@ -289,4 +290,100 @@ describe('Proxy', () => {
|
289 | 290 | testServer.close(done);
|
290 | 291 | });
|
291 | 292 | });
|
| 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 | + }); |
292 | 389 | });
|
0 commit comments