Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Commit 5f0e9b9

Browse files
yohaygyohay-ma
andauthored
add support for application/x-www-form-urlencoded (#37)
Co-authored-by: yohayg <[email protected]>
1 parent ca8526f commit 5f0e9b9

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ module.exports = (opts) => {
6262
} else if (typeof req.body === 'string') {
6363
body = req.body
6464
populateHeaders(headers, body, 'text/plain')
65+
} else if (headers['content-type'] === 'application/x-www-form-urlencoded') {
66+
body = querystring.stringify(req.body)
67+
populateHeaders(headers, body, 'application/x-www-form-urlencoded')
6568
} else {
6669
body = JSON.stringify(req.body)
6770
populateHeaders(headers, body, 'application/json')

test/1.smoke.test.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const request = require('supertest')
55
const bodyParser = require('body-parser')
6-
const expect = require('chai').expect
6+
const { expect } = require('chai')
77
let gateway, service, close, proxy, gHttpServer
88

99
describe('fast-proxy smoke', () => {
@@ -22,6 +22,8 @@ describe('fast-proxy smoke', () => {
2222
// init gateway
2323
gateway = require('restana')()
2424
gateway.use(bodyParser.json())
25+
gateway.use(bodyParser.urlencoded({ extended: true }))
26+
gateway.use(bodyParser.text())
2527

2628
gateway.all('/service/*', function (req, res) {
2729
proxy(req, res, req.url, {})
@@ -46,11 +48,18 @@ describe('fast-proxy smoke', () => {
4648
// init remote service
4749
service = require('restana')()
4850
service.use(bodyParser.json())
49-
51+
service.use(bodyParser.urlencoded({ extended: true }))
52+
service.use(bodyParser.text())
5053
service.get('/service/get', (req, res) => res.send('Hello World!'))
5154
service.post('/service/post', (req, res) => {
5255
res.send(req.body)
5356
})
57+
service.post('/service/post/urlencoded', (req, res) => {
58+
res.send(req.body)
59+
})
60+
service.post('/service/post/text', (req, res) => {
61+
res.send(req.body)
62+
})
5463
service.get('/service/headers', (req, res) => {
5564
res.setHeader('x-agent', 'fast-proxy')
5665
res.setHeader('host', req.headers.host)
@@ -74,7 +83,18 @@ describe('fast-proxy smoke', () => {
7483
.expect(200)
7584
})
7685

77-
it('should 200 on POST to valid remote endpoint', async () => {
86+
it('should 200 on POST plain/text to valid remote endpoint', async () => {
87+
await request(gHttpServer)
88+
.post('/service/post/text')
89+
.set('content-type', 'text/plain')
90+
.send('name is john')
91+
.expect(200)
92+
.then((res) => {
93+
expect(res.text).to.equal('name is john')
94+
})
95+
})
96+
97+
it('should 200 on POST application/json to valid remote endpoint', async () => {
7898
await request(gHttpServer)
7999
.post('/service/post')
80100
.send({ name: 'john' })
@@ -84,6 +104,16 @@ describe('fast-proxy smoke', () => {
84104
})
85105
})
86106

107+
it('should 200 on POST application/x-www-form-urlencoded to valid remote endpoint', async () => {
108+
await request(gHttpServer)
109+
.post('/service/post/urlencoded')
110+
.send('name=john')
111+
.expect(200)
112+
.then((res) => {
113+
expect(res.body.name).to.equal('john')
114+
})
115+
})
116+
87117
it('should 200 on GET /servive/headers', async () => {
88118
await request(gHttpServer)
89119
.get('/service/headers?query=string')

test/5.undici.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const request = require('supertest')
55
const bodyParser = require('body-parser')
6-
const expect = require('chai').expect
6+
const { expect } = require('chai')
77
let gateway, service, close, proxy, gHttpServer
88

99
describe('undici', () => {
@@ -24,7 +24,7 @@ describe('undici', () => {
2424
it('init & start gateway', async () => {
2525
// init gateway
2626
gateway = require('restana')()
27-
27+
gateway.use(bodyParser.json())
2828
gateway.all('/service/*', function (req, res) {
2929
proxy(req, res, req.url, {})
3030
})
@@ -58,14 +58,15 @@ describe('undici', () => {
5858
it('should 200 on POST to valid remote endpoint', async () => {
5959
await request(gHttpServer)
6060
.post('/service/post')
61-
.send({ name: 'john' })
61+
.set('Content-Type', 'application/json')
62+
.send('{"name":"john"}')
6263
.expect(200)
6364
.then((res) => {
6465
expect(res.body.name).to.equal('john')
6566
})
6667
})
6768

68-
it('should 200 on GET /servive/headers', async () => {
69+
it('should 200 on GET /service/headers', async () => {
6970
await request(gHttpServer)
7071
.get('/service/headers')
7172
.expect(200)

0 commit comments

Comments
 (0)