From 2b30ff6672a14df34a88cc755d4f4e66376d0de3 Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Mon, 9 Dec 2024 06:05:29 +0000 Subject: [PATCH] feat: add http proxy middleware --- packages/http-proxy-new/CHANGELOG.md | 176 ++++++++++++++++++ packages/http-proxy-new/README.md | 12 ++ packages/http-proxy-new/index.d.ts | 1 + packages/http-proxy-new/jest.config.js | 8 + packages/http-proxy-new/jest.setup.js | 2 + packages/http-proxy-new/package.json | 41 ++++ packages/http-proxy-new/src/configuration.ts | 10 + packages/http-proxy-new/src/index.ts | 2 + packages/http-proxy-new/src/middleware.ts | 32 ++++ packages/http-proxy-new/test/express.test.ts | 125 +++++++++++++ packages/http-proxy-new/test/faas.test.ts | 126 +++++++++++++ .../fixtures/express/src/configuration.ts | 55 ++++++ .../http-proxy-new/test/fixtures/faas/f.yml | 5 + .../test/fixtures/faas/src/configuration.ts | 49 +++++ .../test/fixtures/koa/src/configuration.ts | 45 +++++ .../test/fixtures/web/package.json | 3 + .../fixtures/web/src/config/config.default.ts | 24 +++ .../test/fixtures/web/src/config/plugin.ts | 2 + .../test/fixtures/web/src/configuration.ts | 26 +++ packages/http-proxy-new/test/koa.test.ts | 127 +++++++++++++ packages/http-proxy-new/test/web.test.ts | 124 ++++++++++++ packages/http-proxy-new/tsconfig.json | 11 ++ packages/http-proxy-new/typedoc.json | 4 + 23 files changed, 1010 insertions(+) create mode 100644 packages/http-proxy-new/CHANGELOG.md create mode 100644 packages/http-proxy-new/README.md create mode 100644 packages/http-proxy-new/index.d.ts create mode 100644 packages/http-proxy-new/jest.config.js create mode 100644 packages/http-proxy-new/jest.setup.js create mode 100644 packages/http-proxy-new/package.json create mode 100644 packages/http-proxy-new/src/configuration.ts create mode 100644 packages/http-proxy-new/src/index.ts create mode 100644 packages/http-proxy-new/src/middleware.ts create mode 100644 packages/http-proxy-new/test/express.test.ts create mode 100644 packages/http-proxy-new/test/faas.test.ts create mode 100644 packages/http-proxy-new/test/fixtures/express/src/configuration.ts create mode 100644 packages/http-proxy-new/test/fixtures/faas/f.yml create mode 100644 packages/http-proxy-new/test/fixtures/faas/src/configuration.ts create mode 100644 packages/http-proxy-new/test/fixtures/koa/src/configuration.ts create mode 100644 packages/http-proxy-new/test/fixtures/web/package.json create mode 100644 packages/http-proxy-new/test/fixtures/web/src/config/config.default.ts create mode 100644 packages/http-proxy-new/test/fixtures/web/src/config/plugin.ts create mode 100644 packages/http-proxy-new/test/fixtures/web/src/configuration.ts create mode 100644 packages/http-proxy-new/test/koa.test.ts create mode 100644 packages/http-proxy-new/test/web.test.ts create mode 100644 packages/http-proxy-new/tsconfig.json create mode 100644 packages/http-proxy-new/typedoc.json diff --git a/packages/http-proxy-new/CHANGELOG.md b/packages/http-proxy-new/CHANGELOG.md new file mode 100644 index 000000000000..e49bef0c7804 --- /dev/null +++ b/packages/http-proxy-new/CHANGELOG.md @@ -0,0 +1,176 @@ +# Change Log + +All notable changes to this project will be documented in this file. +See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. + +# [3.7.0](https://github.com/midwayjs/midway/compare/v3.6.1...v3.7.0) (2022-10-29) + +**Note:** Version bump only for package @midwayjs/http-proxy + +# [3.6.0](https://github.com/midwayjs/midway/compare/v3.5.3...v3.6.0) (2022-10-10) + +### Features + +- add guard ([#2345](https://github.com/midwayjs/midway/issues/2345)) ([1b952a1](https://github.com/midwayjs/midway/commit/1b952a1b09adbb88ff3cff9a2974eb1e37ce53a5)) + +## [3.5.3](https://github.com/midwayjs/midway/compare/v3.5.2...v3.5.3) (2022-09-25) + +### Features + +- add proxyTimeout for httpProxy ([#2308](https://github.com/midwayjs/midway/issues/2308)) ([fb14118](https://github.com/midwayjs/midway/commit/fb141183223098534a075ab20bd3e2c366e40d4a)) + +## [3.5.2](https://github.com/midwayjs/midway/compare/v3.5.1...v3.5.2) (2022-09-08) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.5.1](https://github.com/midwayjs/midway/compare/v3.5.0...v3.5.1) (2022-09-06) + +**Note:** Version bump only for package @midwayjs/http-proxy + +# [3.5.0](https://github.com/midwayjs/midway/compare/v3.4.13...v3.5.0) (2022-08-29) + +### Features + +- add retry wrapper for invoke some remote data ([#2271](https://github.com/midwayjs/midway/issues/2271)) ([1c47338](https://github.com/midwayjs/midway/commit/1c473386937293104369cc8e5727c5330de4f85c)) + +## [3.4.13](https://github.com/midwayjs/midway/compare/v3.4.12...v3.4.13) (2022-08-24) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.4.12](https://github.com/midwayjs/midway/compare/v3.4.11...v3.4.12) (2022-08-20) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.4.11](https://github.com/midwayjs/midway/compare/v3.4.10...v3.4.11) (2022-08-16) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.4.10](https://github.com/midwayjs/midway/compare/v3.4.9...v3.4.10) (2022-08-12) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.4.9](https://github.com/midwayjs/midway/compare/v3.4.8...v3.4.9) (2022-08-10) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.4.8](https://github.com/midwayjs/midway/compare/v3.4.7...v3.4.8) (2022-08-02) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.4.7](https://github.com/midwayjs/midway/compare/v3.4.6...v3.4.7) (2022-08-01) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.4.6](https://github.com/midwayjs/midway/compare/v3.4.5...v3.4.6) (2022-07-31) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.4.4](https://github.com/midwayjs/midway/compare/v3.4.3...v3.4.4) (2022-07-25) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.4.3](https://github.com/midwayjs/midway/compare/v3.4.2...v3.4.3) (2022-07-21) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.4.1](https://github.com/midwayjs/midway/compare/v3.4.0...v3.4.1) (2022-07-20) + +**Note:** Version bump only for package @midwayjs/http-proxy + +# [3.4.0](https://github.com/midwayjs/midway/compare/v3.4.0-beta.12...v3.4.0) (2022-07-20) + +**Note:** Version bump only for package @midwayjs/http-proxy + +# [3.4.0-beta.12](https://github.com/midwayjs/midway/compare/v3.4.0-beta.11...v3.4.0-beta.12) (2022-07-20) + +**Note:** Version bump only for package @midwayjs/http-proxy + +# [3.4.0-beta.11](https://github.com/midwayjs/midway/compare/v3.4.0-beta.10...v3.4.0-beta.11) (2022-07-19) + +**Note:** Version bump only for package @midwayjs/http-proxy + +# [3.4.0-beta.10](https://github.com/midwayjs/midway/compare/v3.4.0-beta.9...v3.4.0-beta.10) (2022-07-18) + +**Note:** Version bump only for package @midwayjs/http-proxy + +# [3.4.0-beta.9](https://github.com/midwayjs/midway/compare/v3.4.0-beta.8...v3.4.0-beta.9) (2022-07-14) + +**Note:** Version bump only for package @midwayjs/http-proxy + +# [3.4.0-beta.8](https://github.com/midwayjs/midway/compare/v3.4.0-beta.7...v3.4.0-beta.8) (2022-07-12) + +**Note:** Version bump only for package @midwayjs/http-proxy + +# [3.4.0-beta.7](https://github.com/midwayjs/midway/compare/v3.4.0-beta.6...v3.4.0-beta.7) (2022-07-12) + +**Note:** Version bump only for package @midwayjs/http-proxy + +# [3.4.0-beta.6](https://github.com/midwayjs/midway/compare/v3.4.0-beta.5...v3.4.0-beta.6) (2022-07-07) + +**Note:** Version bump only for package @midwayjs/http-proxy + +# [3.4.0-beta.5](https://github.com/midwayjs/midway/compare/v3.4.0-beta.4...v3.4.0-beta.5) (2022-07-07) + +**Note:** Version bump only for package @midwayjs/http-proxy + +# [3.4.0-beta.4](https://github.com/midwayjs/midway/compare/v3.4.0-beta.3...v3.4.0-beta.4) (2022-07-04) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.3.11](https://github.com/midwayjs/midway/compare/v3.3.10...v3.3.11) (2022-05-27) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.3.9](https://github.com/midwayjs/midway/compare/v3.3.8...v3.3.9) (2022-05-25) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.3.7](https://github.com/midwayjs/midway/compare/v3.3.6...v3.3.7) (2022-05-13) + +### Bug Fixes + +- koa pipe ([#1973](https://github.com/midwayjs/midway/issues/1973)) ([5cebb76](https://github.com/midwayjs/midway/commit/5cebb76e793abd75bc34dd328d9c1db3d430b58a)) + +## [3.3.6](https://github.com/midwayjs/midway/compare/v3.3.5...v3.3.6) (2022-05-11) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.3.5](https://github.com/midwayjs/midway/compare/v3.3.4...v3.3.5) (2022-04-27) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.3.4](https://github.com/midwayjs/midway/compare/v3.3.3...v3.3.4) (2022-04-21) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.3.3](https://github.com/midwayjs/midway/compare/v3.3.2...v3.3.3) (2022-04-14) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.3.2](https://github.com/midwayjs/midway/compare/v3.3.1...v3.3.2) (2022-04-13) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.3.1](https://github.com/midwayjs/midway/compare/v3.3.0...v3.3.1) (2022-04-11) + +**Note:** Version bump only for package @midwayjs/http-proxy + +# [3.3.0](https://github.com/midwayjs/midway/compare/v3.2.2...v3.3.0) (2022-04-07) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.2.2](https://github.com/midwayjs/midway/compare/v3.2.1...v3.2.2) (2022-03-30) + +**Note:** Version bump only for package @midwayjs/http-proxy + +## [3.2.1](https://github.com/midwayjs/midway/compare/v3.2.0...v3.2.1) (2022-03-27) + +### Bug Fixes + +- swagger ui replace json path ([#1860](https://github.com/midwayjs/midway/issues/1860)) ([0f3728d](https://github.com/midwayjs/midway/commit/0f3728daccba12923f23f5b498c7dda13ced36d7)) + +# [3.2.0](https://github.com/midwayjs/midway/compare/v3.1.6...v3.2.0) (2022-03-25) + +### Features + +- add http proxy component ([#1843](https://github.com/midwayjs/midway/issues/1843)) ([5281e31](https://github.com/midwayjs/midway/commit/5281e316220591b84a0200c6cdf1572680f6e2f6)) diff --git a/packages/http-proxy-new/README.md b/packages/http-proxy-new/README.md new file mode 100644 index 000000000000..8a69b061a4c3 --- /dev/null +++ b/packages/http-proxy-new/README.md @@ -0,0 +1,12 @@ +# midwayjs http-proxy-middleware module + +[![Package Quality](http://npm.packagequality.com/shield/@midwayjs/mongoose.svg)](http://packagequality.com/#?package=@midwayjs/mongoose) +[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/midwayjs/midway/pulls) + +this is a sub package for midway. + +Document: [https://midwayjs.org](https://midwayjs.org) + +## License + +[MIT]((http://github.com/midwayjs/midway/blob/master/LICENSE)) diff --git a/packages/http-proxy-new/index.d.ts b/packages/http-proxy-new/index.d.ts new file mode 100644 index 000000000000..e4ddc9027ee3 --- /dev/null +++ b/packages/http-proxy-new/index.d.ts @@ -0,0 +1 @@ +export * from './dist/index'; diff --git a/packages/http-proxy-new/jest.config.js b/packages/http-proxy-new/jest.config.js new file mode 100644 index 000000000000..784df0fb05b0 --- /dev/null +++ b/packages/http-proxy-new/jest.config.js @@ -0,0 +1,8 @@ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node', + testPathIgnorePatterns: ['/test/fixtures'], + coveragePathIgnorePatterns: ['/test/', '/dist/'], + setupFilesAfterEnv: ['./jest.setup.js'], + coverageProvider: 'v8', +}; diff --git a/packages/http-proxy-new/jest.setup.js b/packages/http-proxy-new/jest.setup.js new file mode 100644 index 000000000000..53c7930592d0 --- /dev/null +++ b/packages/http-proxy-new/jest.setup.js @@ -0,0 +1,2 @@ +process.env.MIDWAY_TS_MODE = 'true'; +jest.setTimeout(30000); diff --git a/packages/http-proxy-new/package.json b/packages/http-proxy-new/package.json new file mode 100644 index 000000000000..3556362c1525 --- /dev/null +++ b/packages/http-proxy-new/package.json @@ -0,0 +1,41 @@ +{ + "name": "@midwayjs/http-proxy-middleware", + "version": "3.19.0", + "description": "Midway Component for http proxy middleware", + "main": "dist/index.js", + "typings": "index.d.ts", + "scripts": { + "build": "tsc", + "test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand", + "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit", + "ci": "npm run test" + }, + "keywords": [ + "midway", + "http", + "proxy" + ], + "author": "", + "files": [ + "dist/**/*.js", + "dist/**/*.d.ts", + "index.d.ts" + ], + "engines": { + "node": ">=12" + }, + "license": "MIT", + "dependencies": { + "http-proxy-middleware": "3.0.3", + "koa2-connect": "1.0.2" + }, + "devDependencies": { + "@midwayjs/core": "^3.19.0", + "@midwayjs/express": "^3.19.0", + "@midwayjs/faas": "^3.19.0", + "@midwayjs/koa": "^3.19.0", + "@midwayjs/mock": "^3.19.0", + "@midwayjs/serverless-app": "^3.19.0", + "@midwayjs/web": "^3.19.0" + } +} diff --git a/packages/http-proxy-new/src/configuration.ts b/packages/http-proxy-new/src/configuration.ts new file mode 100644 index 000000000000..deb9e73d0b21 --- /dev/null +++ b/packages/http-proxy-new/src/configuration.ts @@ -0,0 +1,10 @@ +import { + Configuration, +} from '@midwayjs/core'; + +@Configuration({ + namespace: 'http-proxy-middleware', +}) +export class HttpProxyConfiguration { + async onReady() {} +} diff --git a/packages/http-proxy-new/src/index.ts b/packages/http-proxy-new/src/index.ts new file mode 100644 index 000000000000..2f28d8ee3b0b --- /dev/null +++ b/packages/http-proxy-new/src/index.ts @@ -0,0 +1,2 @@ +export { HttpProxyConfiguration as Configuration } from './configuration'; +export * from './middleware'; diff --git a/packages/http-proxy-new/src/middleware.ts b/packages/http-proxy-new/src/middleware.ts new file mode 100644 index 000000000000..e29cd9f95f73 --- /dev/null +++ b/packages/http-proxy-new/src/middleware.ts @@ -0,0 +1,32 @@ +import { + Logger, + Middleware, + IMiddleware, + ILogger, + IMidwayApplication, +} from '@midwayjs/core'; +import { createProxyMiddleware, Options } from 'http-proxy-middleware'; +import * as c2k from 'koa2-connect'; + +@Middleware() +export class HttpProxyMiddleware implements IMiddleware { + @Logger() + logger: ILogger; + + resolve(app: IMidwayApplication, proxyOptions: Options) { + if (!proxyOptions.logger) { + proxyOptions.logger = this.logger; + } + if (app.getNamespace() === 'express') { + return createProxyMiddleware(proxyOptions); + } else { + return async function (ctx, next) { + return await c2k(createProxyMiddleware(proxyOptions))(ctx, next); + } + } + } + + static getName() { + return 'http-proxy-middleware'; + } +} diff --git a/packages/http-proxy-new/test/express.test.ts b/packages/http-proxy-new/test/express.test.ts new file mode 100644 index 000000000000..efae93168a0b --- /dev/null +++ b/packages/http-proxy-new/test/express.test.ts @@ -0,0 +1,125 @@ +import { createApp, createHttpRequest, close } from '@midwayjs/mock'; +import { join } from 'path'; +import * as assert from 'assert'; +import * as nock from 'nock'; + +describe('test/express.test.ts', function () { + let app; + beforeAll(async () => { + nock('https://gw.alicdn.com').get('/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123').reply(200, '123', {'content-type': 'image/png'}); + nock('https://www.baidu.com').get('/').reply(200, '123', {'content-type': 'text/html'}); + nock('https://sm.bdimg.com').get('/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js').reply(200, '123', {'content-type': 'application/x-javascript'}); + nock('https://httpbin.org') + .persist() + .get('/get?name=midway').reply(200, { + "args": { + "name": "midway" + }, + "headers": { + "Host": "httpbin.org", + }, + "url": "https://httpbin.org/get?name=midway" + }, {'content-type': 'application/json'}) + .post('/post').reply(200, function (uri, requestBody) { + const body = { + 'headers': { + 'Host': 'httpbin.org', + 'Content-Type': this.req.headers['content-type'], + 'url': 'https://httpbin.org/post' + }, + 'url': 'https://httpbin.org/post' + } as any; + if (this.req.headers['content-type'] === 'application/x-www-form-urlencoded') { + body.form = { + "name": "midway" + }; + } + if (this.req.headers['content-type'] === 'application/json') { + body.data = JSON.stringify(requestBody); + } + return body; + }, {'content-type': 'application/json'}); + + const appDir = join(__dirname, 'fixtures/express'); + app = await createApp(appDir); + }) + + afterAll(async () => { + await close(app); + nock.restore(); + }); + + it('get image by host', async () => { + const request = await createHttpRequest(app); + + await request.get('/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.headers['content-type'] === 'image/png') + assert(response.body.length); + }); + }); + + it('get html by host', async () => { + const request = await createHttpRequest(app); + await request.get('/baidu') + .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.text.length); + assert(response.text.endsWith('')); + }); + }); + + it('get javascript by target', async () => { + const request = await createHttpRequest(app); + await request.get('/bdimg/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.headers['content-type'] === 'application/x-javascript') + }); + }); + + it('get to httpbin', async () => { + const request = await createHttpRequest(app); + await request.get('/httpbin/get?name=midway') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.body.url === 'https://httpbin.org/get?name=midway'); + assert(response.body.args.name === 'midway'); + assert(response.body.headers['Host'] === 'httpbin.org'); + }); + }); + + it('post json to httpbin', async () => { + const request = await createHttpRequest(app); + await request.post('/httpbin/post') + .send({name: 'midway'}) + .set('Accept', 'application/json') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.body.url === 'https://httpbin.org/post'); + assert(response.body.headers['Content-Type'] === 'application/json'); + assert(response.body.data === JSON.stringify({ name: 'midway'})); + }); + }); + + it('post x-www-form-urlencoded to httpbin', async () => { + const request = await createHttpRequest(app); + await request.post('/httpbin/post') + .send('name=midway') + .set('Accept', 'application/json') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.body.url === 'https://httpbin.org/post'); + assert(response.body.headers['Content-Type'] === 'application/x-www-form-urlencoded'); + assert(response.body.form.name === 'midway'); + }); + }); +}); diff --git a/packages/http-proxy-new/test/faas.test.ts b/packages/http-proxy-new/test/faas.test.ts new file mode 100644 index 000000000000..c2c30d9a0db5 --- /dev/null +++ b/packages/http-proxy-new/test/faas.test.ts @@ -0,0 +1,126 @@ +import { createHttpRequest, close, createFunctionApp } from '@midwayjs/mock'; +import { join } from 'path'; +import * as assert from 'assert'; +import * as ServerlessApp from '../../../packages-legacy/serverless-app/src'; +import * as nock from 'nock'; + +describe('test/faas.test.ts', function () { + let app; + beforeAll(async () => { + // nock('https://gw.alicdn.com').get('/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123').reply(200, '123', {'content-type': 'image/png;charset=utf-8'}); + nock('https://www.baidu.com').get('/').reply(200, '123', {'content-type': 'text/html'}); + nock('https://sm.bdimg.com').get('/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js').reply(200, '123', {'content-type': 'application/x-javascript'}); + nock('https://httpbin.org') + .persist() + .get('/get?name=midway').reply(200, { + "args": { + "name": "midway" + }, + "headers": { + "Host": "httpbin.org", + }, + "url": "https://httpbin.org/get?name=midway" + }, {'content-type': 'application/json'}) + .post('/post').reply(200, function (uri, requestBody) { + const body = { + 'headers': { + 'Host': 'httpbin.org', + 'Content-Type': this.req.headers['content-type'], + 'url': 'https://httpbin.org/post' + }, + 'url': 'https://httpbin.org/post' + } as any; + if (this.req.headers['content-type'] === 'application/x-www-form-urlencoded') { + body.form = { + "name": "midway" + }; + } + if (this.req.headers['content-type'] === 'application/json') { + body.data = JSON.stringify(requestBody); + } + return body; + }, {'content-type': 'application/json'}); + + const appDir = join(__dirname, 'fixtures/faas'); + app = await createFunctionApp(appDir, {}, ServerlessApp); + }) + + afterAll(async () => { + await close(app); + nock.restore(); + }); + + it('get image by host', async () => { + const request = await createHttpRequest(app); + await request.get('/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.headers['content-type'] === 'image/png; charset=utf-8') + assert(response.body.length); + }); + }); + + it('get javascript by target', async () => { + const request = await createHttpRequest(app); + await request.get('/gcdn/mtb/lib-mtop/2.6.1/mtop.js') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.headers['content-type'] === 'application/javascript; charset=utf-8') + assert(response.text.startsWith('!function(a,b){function c()')); + }); + }); + + it('get to httpbin', async () => { + const request = await createHttpRequest(app); + await request.get('/httpbin/get?name=midway') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.body.url === 'https://httpbin.org/get?name=midway'); + assert(response.body.args.name === 'midway'); + assert(response.body.headers['Host'] === 'httpbin.org'); + }); + }); + + it('get html by host', async () => { + const request = await createHttpRequest(app); + await request.get('/baidu') + .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.text.length); + assert(response.text.endsWith('')); + }); + }); + + it('post json to httpbin', async () => { + const request = await createHttpRequest(app); + await request.post('/httpbin/post') + .send({name: 'midway'}) + .set('Accept', 'application/json') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.body.url === 'https://httpbin.org/post'); + assert(response.body.headers['Content-Type'] === 'application/json'); + assert(response.body.data === JSON.stringify({ name: 'midway'})); + }); + }); + + it('post x-www-form-urlencoded to httpbin', async () => { + const request = await createHttpRequest(app); + await request.post('/httpbin/post') + .send('name=midway') + .set('Accept', 'application/json') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.body.url === 'https://httpbin.org/post'); + assert(response.body.headers['Content-Type'] === 'application/x-www-form-urlencoded'); + assert(response.body.form.name === 'midway'); + }); + }); +}); diff --git a/packages/http-proxy-new/test/fixtures/express/src/configuration.ts b/packages/http-proxy-new/test/fixtures/express/src/configuration.ts new file mode 100644 index 000000000000..65fa78d0d6ba --- /dev/null +++ b/packages/http-proxy-new/test/fixtures/express/src/configuration.ts @@ -0,0 +1,55 @@ +import { All, Configuration, Controller, Inject } from '@midwayjs/core'; +import * as express from '@midwayjs/express'; +import * as proxy from '../../../../src'; + +@Configuration({ + imports: [ + express, + proxy + ], + importConfigs: [ + { + default: { + keys: ['test'], + httpProxy: { + strategy: { + a: { + // https://gw.alicdn.com/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png + match: /\/tfs\//, + host: 'https://gw.alicdn.com', + }, + b: { + // https://g.alicdn.com/mtb/lib-mtop/2.6.1/mtop.js + match: /\/bdimg\/(.*)$/, + target: 'https://sm.bdimg.com/$1', + }, + c: { + // https://httpbin.org/ + match: /\/httpbin\/(.*)$/, + target: 'https://httpbin.org/$1', + }, + d: { + match: /.*?baidu.*$/, + target: 'https://www.baidu.com/' + } + } + }, + } + } + ] +}) +export class AutoConfiguration { +} + +@Controller('/') +export class HomeController { + + @Inject() + ctx; + + @All('/*') + async all() { + return 'hello'; + } +} + diff --git a/packages/http-proxy-new/test/fixtures/faas/f.yml b/packages/http-proxy-new/test/fixtures/faas/f.yml new file mode 100644 index 000000000000..bcc7040e311d --- /dev/null +++ b/packages/http-proxy-new/test/fixtures/faas/f.yml @@ -0,0 +1,5 @@ +service: serverless-midway-test + +provider: + name: aliyun + runtime: nodejs12 \ No newline at end of file diff --git a/packages/http-proxy-new/test/fixtures/faas/src/configuration.ts b/packages/http-proxy-new/test/fixtures/faas/src/configuration.ts new file mode 100644 index 000000000000..225e9b333d18 --- /dev/null +++ b/packages/http-proxy-new/test/fixtures/faas/src/configuration.ts @@ -0,0 +1,49 @@ +import { Configuration, Provide, ServerlessTrigger, ServerlessTriggerType } from '@midwayjs/core'; +import * as faas from '@midwayjs/faas'; +import * as proxy from '../../../../src'; + +@Configuration({ + imports: [ + faas, + proxy + ], + importConfigs: [ + { + default: { + httpProxy: { + strategy: { + a: { + // https://gw.alicdn.com/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png + match: /\/tfs\//, + host: 'https://gw.alicdn.com', + }, + b: { + // https://g.alicdn.com/mtb/lib-mtop/2.6.1/mtop.js + match: /\/gcdn\/(.*)$/, + target: 'https://g.alicdn.com/$1', + }, + c: { + // https://httpbin.org/ + match: /\/httpbin\/(.*)$/, + target: 'https://httpbin.org/$1', + }, + d: { + match: /.*?baidu.*$/, + target: 'https://www.baidu.com/' + } + } + } + } + } + ] +}) +export class AutoConfiguration {} + +@Provide() +export class HelloHttpService { + @ServerlessTrigger(ServerlessTriggerType.HTTP, { path: '/*', method: 'all'}) + async get() { + return 'hello' + } +} + diff --git a/packages/http-proxy-new/test/fixtures/koa/src/configuration.ts b/packages/http-proxy-new/test/fixtures/koa/src/configuration.ts new file mode 100644 index 000000000000..e75729165354 --- /dev/null +++ b/packages/http-proxy-new/test/fixtures/koa/src/configuration.ts @@ -0,0 +1,45 @@ +import { Configuration } from '@midwayjs/core'; +import * as koa from '@midwayjs/koa'; +import * as proxy from '../../../../src'; + +@Configuration({ + imports: [ + koa, + proxy + ], + importConfigs: [ + { + default: { + keys: ["test"], + httpProxy: { + strategy: { + a: { + // https://gw.alicdn.com/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png + match: /\/tfs\//, + host: 'https://gw.alicdn.com', + }, + b: { + // https://g.alicdn.com/mtb/lib-mtop/2.6.1/mtop.js + match: /\/bdimg\/(.*)$/, + target: 'https://sm.bdimg.com/$1', + }, + c: { + // https://httpbin.org/ + match: /\/httpbin\/(.*)$/, + target: 'https://httpbin.org/$1', + }, + d: { + match: /.*?baidu.*$/, + target: 'https://www.baidu.com/' + }, + e: { + match: /.*?du\/(.*)$/, + target: 'https://www.baidu.com/$1' + } + } + } + } + } + ] +}) +export class AutoConfiguration {} diff --git a/packages/http-proxy-new/test/fixtures/web/package.json b/packages/http-proxy-new/test/fixtures/web/package.json new file mode 100644 index 000000000000..621cdc6a4174 --- /dev/null +++ b/packages/http-proxy-new/test/fixtures/web/package.json @@ -0,0 +1,3 @@ +{ + "name": "ali-demo" +} diff --git a/packages/http-proxy-new/test/fixtures/web/src/config/config.default.ts b/packages/http-proxy-new/test/fixtures/web/src/config/config.default.ts new file mode 100644 index 000000000000..dc7dd93dcbe2 --- /dev/null +++ b/packages/http-proxy-new/test/fixtures/web/src/config/config.default.ts @@ -0,0 +1,24 @@ +export const keys = 'test'; +export const httpProxy = { + strategy: { + a: { + // https://gw.alicdn.com/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png + match: /\/tfs\//, + host: 'https://gw.alicdn.com', + }, + b: { + // https://g.alicdn.com/mtb/lib-mtop/2.6.1/mtop.js + match: /\/bdimg\/(.*)$/, + target: 'https://sm.bdimg.com/$1', + }, + c: { + // https://httpbin.org/ + match: /\/httpbin\/(.*)$/, + target: 'https://httpbin.org/$1', + }, + d: { + match: /.*?baidu.*$/, + target: 'https://www.baidu.com/' + } + }, +}; diff --git a/packages/http-proxy-new/test/fixtures/web/src/config/plugin.ts b/packages/http-proxy-new/test/fixtures/web/src/config/plugin.ts new file mode 100644 index 000000000000..c19bc759b87f --- /dev/null +++ b/packages/http-proxy-new/test/fixtures/web/src/config/plugin.ts @@ -0,0 +1,2 @@ + +export const security = false; \ No newline at end of file diff --git a/packages/http-proxy-new/test/fixtures/web/src/configuration.ts b/packages/http-proxy-new/test/fixtures/web/src/configuration.ts new file mode 100644 index 000000000000..a0c8b0c3a58a --- /dev/null +++ b/packages/http-proxy-new/test/fixtures/web/src/configuration.ts @@ -0,0 +1,26 @@ +import { All, Configuration, Controller } from '@midwayjs/core'; +import * as koa from '@midwayjs/web'; +import * as defaultConfig from './config/config.default'; +import * as proxy from '../../../../src'; + +@Configuration({ + imports: [ + koa, + proxy + ], + importConfigs: [ + { + default: defaultConfig + } + ] +}) +export class AutoConfiguration {} + +@Controller() +export class HelloHttpService { + @All('/*') + async get() { + return 'hello' + } +} + diff --git a/packages/http-proxy-new/test/koa.test.ts b/packages/http-proxy-new/test/koa.test.ts new file mode 100644 index 000000000000..94fa1fb103c8 --- /dev/null +++ b/packages/http-proxy-new/test/koa.test.ts @@ -0,0 +1,127 @@ +import { createApp, createHttpRequest, close } from '@midwayjs/mock'; +import { join } from 'path'; +import * as assert from 'assert'; +import * as nock from 'nock'; + +describe('test/koa.test.ts', function () { + let app; + beforeAll(async () => { + nock('https://gw.alicdn.com').get('/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123').reply(200, '123', {'content-type': 'image/png'}); + nock('https://www.baidu.com').get('/').reply(200, '123', {'content-type': 'text/html'}); + nock('https://www.baidu.com').get('/link?url=dXnGnrTKHV5EBpYuOj5Febx_pVXwbaONJvOSBOev6VO&wd=&eqid=da62137f000db74500000005638f3778').reply(200, ` + `, {'content-type': 'text/html', 'content-encoding': 'br'}); + nock('https://sm.bdimg.com').get('/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js').reply(200, '123', {'content-type': 'application/x-javascript'}); + nock('https://httpbin.org') + .persist() + .get('/get?name=midway').reply(200, { + "args": { + "name": "midway" + }, + "headers": { + "Host": "httpbin.org", + }, + "url": "https://httpbin.org/get?name=midway" + }, {'content-type': 'application/json'}) + .post('/post').reply(200, function (uri, requestBody) { + const body = { + 'headers': { + 'Host': 'httpbin.org', + 'Content-Type': this.req.headers['content-type'], + 'url': 'https://httpbin.org/post' + }, + 'url': 'https://httpbin.org/post' + } as any; + if (this.req.headers['content-type'] === 'application/x-www-form-urlencoded') { + body.form = { + "name": "midway" + }; + } + if (this.req.headers['content-type'] === 'application/json') { + body.data = JSON.stringify(requestBody); + } + return body; + }, {'content-type': 'application/json'}); + const appDir = join(__dirname, 'fixtures/koa'); + app = await createApp(appDir); + }) + + afterAll(async () => { + await close(app); + nock.restore(); + }); + + it('get image by host', async () => { + const request = await createHttpRequest(app); + + await request.get('/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.headers['content-type'] === 'image/png') + assert(response.body.length); + }); + }); + + it('get html by host', async () => { + const request = await createHttpRequest(app); + await request.get('/baidu') + .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.text.length); + assert(response.text.endsWith('')); + }); + }); + + it('get javascript by target', async () => { + const request = await createHttpRequest(app); + await request.get('/bdimg/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.headers['content-type'] === 'application/x-javascript') + }); + }); + + it('get to httpbin', async () => { + const request = await createHttpRequest(app); + await request.get('/httpbin/get?name=midway') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.body.url === 'https://httpbin.org/get?name=midway'); + assert(response.body.args.name === 'midway'); + assert(response.body.headers['Host'] === 'httpbin.org'); + }); + }); + + it('post json to httpbin', async () => { + const request = await createHttpRequest(app); + await request.post('/httpbin/post') + .send({name: 'midway'}) + .set('Accept', 'application/json') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.body.url === 'https://httpbin.org/post'); + assert(response.body.headers['Content-Type'] === 'application/json'); + assert(response.body.data === JSON.stringify({ name: 'midway'})); + }); + }); + + it('post x-www-form-urlencoded to httpbin', async () => { + const request = await createHttpRequest(app); + await request.post('/httpbin/post') + .send('name=midway') + .set('Accept', 'application/json') + .expect(200) + .then(async response => { + + assert(response.status === 200) + assert(response.body.url === 'https://httpbin.org/post'); + assert(response.body.headers['Content-Type'] === 'application/x-www-form-urlencoded'); + assert(response.body.form.name === 'midway'); + }); + }); +}); diff --git a/packages/http-proxy-new/test/web.test.ts b/packages/http-proxy-new/test/web.test.ts new file mode 100644 index 000000000000..747674f2e007 --- /dev/null +++ b/packages/http-proxy-new/test/web.test.ts @@ -0,0 +1,124 @@ +import { createApp, createHttpRequest, close } from '@midwayjs/mock'; +import { join } from 'path'; +import * as assert from 'assert'; +import * as nock from 'nock'; +describe('test/web.test.ts', function () { + let app; + beforeAll(async () => { + nock('https://gw.alicdn.com').get('/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123').reply(200, '123', {'content-type': 'image/png'}); + nock('https://www.baidu.com').get('/').reply(200, '123', {'content-type': 'text/html'}); + nock('https://sm.bdimg.com').get('/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js').reply(200, '123', {'content-type': 'application/x-javascript'}); + nock('https://httpbin.org') + .persist() + .get('/get?name=midway').reply(200, { + "args": { + "name": "midway" + }, + "headers": { + "Host": "httpbin.org", + }, + "url": "https://httpbin.org/get?name=midway" + }, {'content-type': 'application/json'}) + .post('/post').reply(200, function (uri, requestBody) { + const body = { + 'headers': { + 'Host': 'httpbin.org', + 'Content-Type': this.req.headers['content-type'], + 'url': 'https://httpbin.org/post' + }, + 'url': 'https://httpbin.org/post' + } as any; + if (this.req.headers['content-type'] === 'application/x-www-form-urlencoded') { + body.form = { + "name": "midway" + }; + } + if (this.req.headers['content-type'] === 'application/json') { + body.data = JSON.stringify(requestBody); + } + return body; + }, {'content-type': 'application/json'}); + const appDir = join(__dirname, 'fixtures/web'); + app = await createApp(appDir); + }) + + afterAll(async () => { + await close(app); + nock.restore(); + }); + + it('get image by host', async () => { + const request = await createHttpRequest(app); + + await request.get('/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.headers['content-type'] === 'image/png') + assert(response.body.length); + }); + }); + + it('get html by host', async () => { + const request = await createHttpRequest(app); + await request.get('/baidu') + .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.text.length); + assert(response.text.endsWith('')); + }); + }); + + it('get javascript by target', async () => { + const request = await createHttpRequest(app); + await request.get('/bdimg/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.headers['content-type'] === 'application/x-javascript') + }); + }); + + it('get to httpbin', async () => { + const request = await createHttpRequest(app); + await request.get('/httpbin/get?name=midway') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.body.url === 'https://httpbin.org/get?name=midway'); + assert(response.body.args.name === 'midway'); + assert(response.body.headers['Host'] === 'httpbin.org'); + }); + }); + + it('post json to httpbin', async () => { + const request = await createHttpRequest(app); + await request.post('/httpbin/post') + .send({name: 'midway'}) + .set('Accept', 'application/json') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.body.url === 'https://httpbin.org/post'); + assert(response.body.headers['Content-Type'] === 'application/json'); + assert(response.body.data === JSON.stringify({ name: 'midway'})); + }); + }); + + it('post x-www-form-urlencoded to httpbin', async () => { + const request = await createHttpRequest(app); + await request.post('/httpbin/post') + .send('name=midway') + .set('Accept', 'application/json') + .expect(200) + .then(async response => { + + assert(response.status === 200) + assert(response.body.url === 'https://httpbin.org/post'); + assert(response.body.headers['Content-Type'] === 'application/x-www-form-urlencoded'); + assert(response.body.form.name === 'midway'); + }); + }); +}); diff --git a/packages/http-proxy-new/tsconfig.json b/packages/http-proxy-new/tsconfig.json new file mode 100644 index 000000000000..324fe88c9b14 --- /dev/null +++ b/packages/http-proxy-new/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.json", + "compileOnSave": true, + "compilerOptions": { + "rootDir": "src", + "outDir": "dist" + }, + "include": [ + "./src/**/*.ts" + ] +} diff --git a/packages/http-proxy-new/typedoc.json b/packages/http-proxy-new/typedoc.json new file mode 100644 index 000000000000..f593f276c273 --- /dev/null +++ b/packages/http-proxy-new/typedoc.json @@ -0,0 +1,4 @@ +{ + "extends": ["../../typedoc.base.json"], + "entryPoints": ["src/index.ts"] +}