Skip to content

Commit

Permalink
fix: 使用 body-parser 之后某些代理失效, 一直挂起
Browse files Browse the repository at this point in the history
  • Loading branch information
wll8 committed Jun 20, 2022
1 parent 66dd1f8 commit 0badc30
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mockm",
"version": "1.1.26-alpha.26",
"version": "1.1.26-alpha.27",
"description": "Analog interface server, painless parallel development of front and back ends.",
"main": "server.js",
"bin": {
Expand Down
3 changes: 3 additions & 0 deletions server/util/business.js
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,9 @@ function business() { // 与业务相关性的函数
// // }
// })
reqHandle().injectionReq({req, res, type: `get`})

// https://github.com/chimurai/http-proxy-middleware/pull/492
require(`http-proxy-middleware`).fixRequestBody(proxyReq, req)
},
onProxyRes: (proxyRes, req, res) => {
allowCors({res: proxyRes, req, proxyConfig: userConfig})
Expand Down
23 changes: 23 additions & 0 deletions test/config.proxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@ const util = require('./util.js')
const http = util.http

describe('config.proxy', () => {
it(`代理到原始服务器`, async () => {
util.ok(await util.runMockm({
mockm: () => ({
proxy: {
'/': `http://www.httpbin.org`,
},
}),
okFn: async ({arg, str}) => {
const get = (await http.get(`http://127.0.0.1:${arg.port}/get?a=1&b=2`)).data
const post = (await http.post(`http://127.0.0.1:${arg.port}/post`, {a: 1, b: 2})).data
const bin = (await http.get(`http://127.0.0.1:${arg.port}/image/png`)).data
const form = (await util.upload(`http://127.0.0.1:${arg.port}/post`, {a: 1, b: 2})).data
const upload = (await util.upload(`http://127.0.0.1:${arg.port}/post`, {f1: require(`fs`).createReadStream(__filename)})).data
return (
get.args.a === `1`
&& post.json.a === 1
&& bin.match(`PNG`)
&& form.form.a === `1`
&& util.getType(upload.files.f1, `string`)
)
},
}))
})
it(`拦截请求`, async () => {
util.ok(await util.runMockm({
mockm: () => ({
Expand Down
1 change: 1 addition & 0 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ function upload(api, data) {
}

module.exports = {
getType,
upload,
to,
ok,
Expand Down

0 comments on commit 0badc30

Please sign in to comment.