Skip to content

Commit

Permalink
fix: should reset agent on restore
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jul 4, 2024
1 parent e2a4b26 commit 5c5da6f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/extend/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module.exports = {
return mockAgent.getAgent(this);
},

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

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

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

/**
Expand Down
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ module.exports.default = mock;

// inherit & extends mm
Object.assign(mock, mm, {
restore() {
async restore() {
cluster.restore();
mockAgent.restore(app);
// return promise
return mm.restore();
await mockAgent.restore();
return await mm.restore();
},

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ module.exports = function(options) {
// don't delegate properties on MockApplication
if (MOCK_APP_METHOD.includes(prop)) return getProperty(target, prop);
if (!target[APP_INIT]) throw new Error(`can't get ${prop} before ready`);
// it's asyncrounus when agent and app are loading,
// it's asynchronous when agent and app are loading,
// so should get the properties after loader ready
debug('proxy handler.get %s', prop);
return target._app[prop];
Expand Down
15 changes: 12 additions & 3 deletions lib/mock_agent.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const { debuglog } = require('util');
const { MockAgent, setGlobalDispatcher, getGlobalDispatcher } = require('urllib');

const debug = debuglog('egg-mock:lib:mock_agent');

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

module.exports = {
getAgent(app) {
Expand All @@ -12,6 +16,8 @@ module.exports = {
if (!app?.httpclient?.[APP_HTTPCLIENT_AGENT] && typeof app?.httpclient?.getDispatcher === 'function') {
// save the raw dispatcher
app.httpclient[APP_HTTPCLIENT_AGENT] = app.httpclient.getDispatcher();
httpClients.push(app.httpclient);
debug('add new httpClient, size: %d', httpClients.length);
}
if (!_mockAgent) {
_mockAgent = new MockAgent();
Expand All @@ -22,14 +28,17 @@ module.exports = {
}
return _mockAgent;
},
restore(app) {
async restore() {
if (!_mockAgent) return;
if (_global) {
setGlobalDispatcher(_global);
}
if (app?.httpclient?.[APP_HTTPCLIENT_AGENT]) {
app.httpclient.setDispatcher(app.httpclient[APP_HTTPCLIENT_AGENT]);
for (const httpClient of httpClients) {
httpClient.setDispatcher(httpClient[APP_HTTPCLIENT_AGENT]);
}
debug('restore httpClient, size: %d', httpClients.length);
const agent = _mockAgent;
_mockAgent = null;
await agent.close();
},
};
11 changes: 11 additions & 0 deletions test/mock_httpclient_next_h2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ describe('test/mock_httpclient_next_h2.test.js', () => {
.expect(200);
});

it('should auto restore after each case', async () => {
app.mockCsrf();
await request(server)
.get('/urllib')
.expect({
get: 'url get',
post: 'url post',
})
.expect(200);
});

it('should use first mock data on duplicate url mock', async () => {
app.mockCsrf();
app.mockHttpclient(url, 'post', {
Expand Down

0 comments on commit 5c5da6f

Please sign in to comment.