Skip to content

Commit

Permalink
fix: support httpclient mock on allowH2 = true (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Jul 2, 2024
1 parent be92929 commit 3a434bb
Show file tree
Hide file tree
Showing 26 changed files with 838 additions and 33 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/pkg.pr.new.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Publish Any Commit
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm install

- name: Build
run: npm run prepublishOnly --if-present

- run: npx pkg-pr-new publish
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ it('should work', function() {
```

### env for custom bootstrap

EGG_BASE_DIR: the base dir of egg app
EGG_FRAMEWORK: the framework of egg app

Expand All @@ -534,16 +535,8 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).

[MIT](LICENSE)

<!-- GITCONTRIBUTOR_START -->

## Contributors

|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/227713?v=4" width="100px;"/><br/><sub><b>atian25</b></sub>](https://github.com/atian25)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/452899?v=4" width="100px;"/><br/><sub><b>shepherdwind</b></sub>](https://github.com/shepherdwind)<br/>|[<img src="https://avatars.githubusercontent.com/u/456108?v=4" width="100px;"/><br/><sub><b>shaoshuai0102</b></sub>](https://github.com/shaoshuai0102)<br/>|
| :---: | :---: | :---: | :---: | :---: | :---: |
|[<img src="https://avatars.githubusercontent.com/u/2160731?v=4" width="100px;"/><br/><sub><b>mansonchor</b></sub>](https://github.com/mansonchor)<br/>|[<img src="https://avatars.githubusercontent.com/u/5856440?v=4" width="100px;"/><br/><sub><b>whxaxes</b></sub>](https://github.com/whxaxes)<br/>|[<img src="https://avatars.githubusercontent.com/u/3139237?v=4" width="100px;"/><br/><sub><b>brickyang</b></sub>](https://github.com/brickyang)<br/>|[<img src="https://avatars.githubusercontent.com/u/880513?v=4" width="100px;"/><br/><sub><b>zbinlin</b></sub>](https://github.com/zbinlin)<br/>|[<img src="https://avatars.githubusercontent.com/u/36814673?v=4" width="100px;"/><br/><sub><b>GoodMeowing</b></sub>](https://github.com/GoodMeowing)<br/>|[<img src="https://avatars.githubusercontent.com/u/5243774?v=4" width="100px;"/><br/><sub><b>ngot</b></sub>](https://github.com/ngot)<br/>|
|[<img src="https://avatars.githubusercontent.com/u/3274850?v=4" width="100px;"/><br/><sub><b>geekdada</b></sub>](https://github.com/geekdada)<br/>|[<img src="https://avatars.githubusercontent.com/u/7784713?v=4" width="100px;"/><br/><sub><b>shinux</b></sub>](https://github.com/shinux)<br/>|[<img src="https://avatars.githubusercontent.com/u/7530656?v=4" width="100px;"/><br/><sub><b>zhang740</b></sub>](https://github.com/zhang740)<br/>|[<img src="https://avatars.githubusercontent.com/u/225856?v=4" width="100px;"/><br/><sub><b>caoer</b></sub>](https://github.com/caoer)<br/>|[<img src="https://avatars.githubusercontent.com/u/7970645?v=4" width="100px;"/><br/><sub><b>lidianhao123</b></sub>](https://github.com/lidianhao123)<br/>|[<img src="https://avatars.githubusercontent.com/u/9961514?v=4" width="100px;"/><br/><sub><b>limerickgds</b></sub>](https://github.com/limerickgds)<br/>|
[<img src="https://avatars.githubusercontent.com/u/7971415?v=4" width="100px;"/><br/><sub><b>paranoidjk</b></sub>](https://github.com/paranoidjk)<br/>|[<img src="https://avatars.githubusercontent.com/u/1207064?v=4" width="100px;"/><br/><sub><b>gxcsoccer</b></sub>](https://github.com/gxcsoccer)<br/>|[<img src="https://avatars.githubusercontent.com/u/2127199?v=4" width="100px;"/><br/><sub><b>okoala</b></sub>](https://github.com/okoala)<br/>|[<img src="https://avatars.githubusercontent.com/u/10825163?v=4" width="100px;"/><br/><sub><b>ImHype</b></sub>](https://github.com/ImHype)<br/>|[<img src="https://avatars.githubusercontent.com/u/16033313?v=4" width="100px;"/><br/><sub><b>linjiajian999</b></sub>](https://github.com/linjiajian999)<br/>

This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Fri Apr 29 2022 22:49:14 GMT+0800`.
[![Contributors](https://contrib.rocks/image?repo=eggjs/egg-mock)](https://github.com/eggjs/egg-mock/graphs/contributors)

<!-- GITCONTRIBUTOR_END -->
Made with [contributors-img](https://contrib.rocks).
13 changes: 3 additions & 10 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ describe('test ctx', () => {
```

### env for custom bootstrap

EGG_BASE_DIR: the base dir of egg app
EGG_FRAMEWORK: the framework of egg app

Expand All @@ -505,16 +506,8 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).

[MIT](LICENSE)

<!-- GITCONTRIBUTOR_START -->

## Contributors

|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/227713?v=4" width="100px;"/><br/><sub><b>atian25</b></sub>](https://github.com/atian25)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/452899?v=4" width="100px;"/><br/><sub><b>shepherdwind</b></sub>](https://github.com/shepherdwind)<br/>|[<img src="https://avatars.githubusercontent.com/u/456108?v=4" width="100px;"/><br/><sub><b>shaoshuai0102</b></sub>](https://github.com/shaoshuai0102)<br/>|
| :---: | :---: | :---: | :---: | :---: | :---: |
|[<img src="https://avatars.githubusercontent.com/u/2160731?v=4" width="100px;"/><br/><sub><b>mansonchor</b></sub>](https://github.com/mansonchor)<br/>|[<img src="https://avatars.githubusercontent.com/u/5856440?v=4" width="100px;"/><br/><sub><b>whxaxes</b></sub>](https://github.com/whxaxes)<br/>|[<img src="https://avatars.githubusercontent.com/u/3139237?v=4" width="100px;"/><br/><sub><b>brickyang</b></sub>](https://github.com/brickyang)<br/>|[<img src="https://avatars.githubusercontent.com/u/880513?v=4" width="100px;"/><br/><sub><b>zbinlin</b></sub>](https://github.com/zbinlin)<br/>|[<img src="https://avatars.githubusercontent.com/u/36814673?v=4" width="100px;"/><br/><sub><b>GoodMeowing</b></sub>](https://github.com/GoodMeowing)<br/>|[<img src="https://avatars.githubusercontent.com/u/5243774?v=4" width="100px;"/><br/><sub><b>ngot</b></sub>](https://github.com/ngot)<br/>|
|[<img src="https://avatars.githubusercontent.com/u/3274850?v=4" width="100px;"/><br/><sub><b>geekdada</b></sub>](https://github.com/geekdada)<br/>|[<img src="https://avatars.githubusercontent.com/u/7784713?v=4" width="100px;"/><br/><sub><b>shinux</b></sub>](https://github.com/shinux)<br/>|[<img src="https://avatars.githubusercontent.com/u/7530656?v=4" width="100px;"/><br/><sub><b>zhang740</b></sub>](https://github.com/zhang740)<br/>|[<img src="https://avatars.githubusercontent.com/u/225856?v=4" width="100px;"/><br/><sub><b>caoer</b></sub>](https://github.com/caoer)<br/>|[<img src="https://avatars.githubusercontent.com/u/7970645?v=4" width="100px;"/><br/><sub><b>lidianhao123</b></sub>](https://github.com/lidianhao123)<br/>|[<img src="https://avatars.githubusercontent.com/u/9961514?v=4" width="100px;"/><br/><sub><b>limerickgds</b></sub>](https://github.com/limerickgds)<br/>|
[<img src="https://avatars.githubusercontent.com/u/7971415?v=4" width="100px;"/><br/><sub><b>paranoidjk</b></sub>](https://github.com/paranoidjk)<br/>|[<img src="https://avatars.githubusercontent.com/u/1207064?v=4" width="100px;"/><br/><sub><b>gxcsoccer</b></sub>](https://github.com/gxcsoccer)<br/>|[<img src="https://avatars.githubusercontent.com/u/2127199?v=4" width="100px;"/><br/><sub><b>okoala</b></sub>](https://github.com/okoala)<br/>|[<img src="https://avatars.githubusercontent.com/u/10825163?v=4" width="100px;"/><br/><sub><b>ImHype</b></sub>](https://github.com/ImHype)<br/>|[<img src="https://avatars.githubusercontent.com/u/16033313?v=4" width="100px;"/><br/><sub><b>linjiajian999</b></sub>](https://github.com/linjiajian999)<br/>

This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Fri Apr 29 2022 22:49:14 GMT+0800`.
[![Contributors](https://contrib.rocks/image?repo=eggjs/egg-mock)](https://github.com/eggjs/egg-mock/graphs/contributors)

<!-- GITCONTRIBUTOR_END -->
Made with [contributors-img](https://contrib.rocks).
4 changes: 2 additions & 2 deletions app/extend/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ module.exports = {
* @return {MockAgent} agent
*/
mockAgent() {
return mockAgent.getAgent();
return mockAgent.getAgent(this);
},

mockAgentRestore() {
return mockAgent.restore();
return mockAgent.restore(this);
},

/**
Expand Down
4 changes: 2 additions & 2 deletions app/extend/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ module.exports = {
* @return {MockAgent} agent
*/
mockAgent() {
return mockAgent.getAgent();
return mockAgent.getAgent(this);
},

mockAgentRestore() {
return mockAgent.restore();
return mockAgent.restore(this);
},

/**
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const app = require('./lib/app');
const mockAgent = require('./lib/mock_agent');

// egg-bin will set this flag to require files for instrument
if (process.env.EGG_BIN_PREREQUIRE) require('./lib/prerequire');
if (process.env.EGG_BIN_PREREQUIRE) {
require('./lib/prerequire');
}

/**
* @namespace mm
Expand All @@ -22,7 +24,7 @@ Object.assign(mock, mm, {
cluster.restore();
mm.restore();
// return promise
return mockAgent.restore();
return mockAgent.restore(app);
},

/**
Expand Down
21 changes: 18 additions & 3 deletions lib/mock_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,36 @@ const { MockAgent, setGlobalDispatcher, getGlobalDispatcher } = require('urllib'

let _mockAgent;
let _global;
const APP_HTTPCLIENT_AGENT = Symbol('app.httpclient.agent');

module.exports = {
getAgent() {
getAgent(app) {
if (!_global) {
_global = getGlobalDispatcher();
if (typeof app?.httpclient?.getDispatcher === 'function') {
if (!app[APP_HTTPCLIENT_AGENT]) {
// save the raw dispatcher
app[APP_HTTPCLIENT_AGENT] = app.httpclient.getDispatcher();
}
}
}
if (!_mockAgent) {
_mockAgent = new MockAgent();
setGlobalDispatcher(_mockAgent);
if (typeof app?.httpclient?.setDispatcher === 'function') {
app.httpclient.setDispatcher(_mockAgent);
}
}
return _mockAgent;
},
async restore() {
async restore(app) {
if (!_mockAgent) return;
if (_global) setGlobalDispatcher(_global);
if (_global) {
setGlobalDispatcher(_global);
if (app?.[APP_HTTPCLIENT_AGENT]) {
app.httpclient.setDispatcher(app[APP_HTTPCLIENT_AGENT]);
}
}
const agent = _mockAgent;
_mockAgent = null;
await agent.close();
Expand Down
2 changes: 1 addition & 1 deletion lib/mock_httpclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ module.exports = app => {
mockConfigIndex = mockConfigs.length;
mockConfigs.push({ mockUrl, mockResult, mockConfigIndex });
}
const mockPool = mockAgent.getAgent().get(origin);
const mockPool = mockAgent.getAgent(app).get(origin);
// persist default is true
const persist = typeof mockResult?.persist === 'boolean' ? mockResult?.persist : true;
mockMethod.forEach(function(method) {
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"test": "npm run lint && npm run tsd && npm run test-local",
"test-local": "egg-bin test -r ./register.js --ts false",
"cov": "egg-bin cov -r ./register.js --ts false",
"ci": "npm run lint && npm run tsd && npm run cov",
"contributors": "git-contributor"
"ci": "npm run lint && npm run tsd && npm run cov"
},
"dependencies": {
"@types/supertest": "^2.0.7",
Expand Down Expand Up @@ -63,7 +62,6 @@
"egg-tracer": "^2.0.0",
"eslint": "^8.24.0",
"eslint-config-egg": "^12.0.0",
"git-contributor": "^2.0.0",
"mocha": "^10.1.0",
"pedding": "^1.1.0",
"tsd": "^0.31.0",
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/demo_next_h2/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = function() {

};
9 changes: 9 additions & 0 deletions test/fixtures/demo_next_h2/app/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = {
getResult(result) {
return {
body: result,
};
},
};
11 changes: 11 additions & 0 deletions test/fixtures/demo_next_h2/app/controller/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = function* () {
const stream = yield this.getFileStream();
const fields = stream.fields;
this.body = {
fields,
filename: stream.filename,
user: this.user,
};
};
104 changes: 104 additions & 0 deletions test/fixtures/demo_next_h2/app/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
'use strict';

exports.get = function* () {
this.body = {
cookieValue: this.getCookie('foo') || undefined,
cookiesValue: this.cookies.get('foo') || undefined,
sessionValue: this.session.foo,
};
};

exports.post = function* () {
this.body = 'done';
};

exports.hello = function* () {
this.body = 'hi';
};

exports.service = function* () {
this.body = {
foo1: yield this.service.foo.get(),
foo2: yield this.service.bar.foo.get(),
foo3: this.service.foo.getSync(),
thirdService: yield this.service.third.bar.foo.get(),
};
};

exports.serviceOld = function* () {
this.body = yield this.service.old.test();
};

exports.header = function* () {
this.body = {
header: this.get('customheader'),
};
};

exports.urllib = function* () {
const url = 'http://' + this.host;
const method = this.query.method || 'request';
const data = this.query.data ? JSON.parse(this.query.data) : undefined;
const dataType = this.query.dataType;
const foo = this.query.foo;
let requestUrl = url + (this.query.mock_url || '/mock_url');
if (foo) {
requestUrl = `${requestUrl}?foo=${foo}`;
}
let r = this.app.httpclient[method](requestUrl, {
dataType,
data,
});
if (method === 'request') r = r.then(d => d);
const r1 = yield r;
const r2 = yield this.app.httpclient[method](requestUrl, {
method: 'POST',
dataType,
data,
headers: {
'x-custom': 'custom',
},
});
this.body = {
get: Buffer.isBuffer(r1.data) ? r1.data.toString() : r1.data,
post: Buffer.isBuffer(r2.data) ? r2.data.toString() : r2.data,
};
};

exports.streaming = async ctx => {
const url = 'http://' + ctx.host;
const response = await ctx.httpclient.request(url + '/mock_url', {
method: 'GET',
streaming: true,
});
ctx.status = response.status;
ctx.body = response.res;
};

exports.mockUrlGet = function* () {
const foo = this.query.foo;
if (foo) {
this.body = `url get with foo: ${foo}`;
return;
}
this.body = 'url get';
};

exports.mockUrlPost = function* () {
this.body = 'url post';
};

exports.mockUrllibHeaders = function* () {
const url = 'http://' + this.host;
const method = this.query.method || 'request';
const res = yield this.app.httpclient[method](url + '/mock_url');
this.body = res.headers;
};

exports.dataType = function* () {
const url = 'http://' + this.host;
const res = yield this.app.httpclient.request(url + '/mock_url', {
dataType: 'json',
});
this.body = res.data;
};
6 changes: 6 additions & 0 deletions test/fixtures/demo_next_h2/app/controller/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function* () {
this.session.save();
this.body = this.session;
};
12 changes: 12 additions & 0 deletions test/fixtures/demo_next_h2/app/controller/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

exports.get = function* () {
this.body = this.user;
};

exports.post = function* () {
this.body = {
user: this.user,
params: this.request.body,
};
};
18 changes: 18 additions & 0 deletions test/fixtures/demo_next_h2/app/extend/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

module.exports = {
mockDevice(obj) {
obj.mock = true;
return obj;
},

* mockGenerator(obj) {
obj.mock = true;
return obj;
},

mockPromise(obj) {
obj.mock = true;
return Promise.resolve(obj);
},
};
23 changes: 23 additions & 0 deletions test/fixtures/demo_next_h2/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = app => {
app.get('home', '/', app.controller.home.get);
app.get('/hello', app.controller.home.hello);
app.get('/service', app.controller.home.service);
app.get('/service/old', app.controller.home.serviceOld);
app.get('/header', app.controller.home.header);
app.get('/urllib', app.controller.home.urllib);
app.get('/mock_url', app.controller.home.mockUrlGet);
app.get('/mock_url2', app.controller.home.mockUrlGet);
app.post('/mock_url', app.controller.home.mockUrlPost);
app.post('/mock_url2', app.controller.home.mockUrlPost);
app.get('/mock_urllib', app.controller.home.mockUrllibHeaders);
app.get('/data_type', app.controller.home.dataType);
app.get('session', '/session', app.controller.session);

app.post('/', app.controller.home.post);

app.get('/user', app.controller.user.get);
app.post('/user', app.controller.user.post);
app.post('/file', app.controller.file);

app.get('/streaming', 'home.streaming');
};
Loading

0 comments on commit 3a434bb

Please sign in to comment.