Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should reset agent on restore #171

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
return mockAgent.getAgent(this);
},

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

Check warning on line 29 in app/extend/agent.js

View check run for this annotation

Codecov / codecov/patch

app/extend/agent.js#L29

Added line #L29 was not covered by tests
},

/**
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();
},
};
2 changes: 1 addition & 1 deletion test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ function call(method) {
if (method === 'app') {
assert(app.logger._mockLogs);
assert(app.coreLogger._mockLogs);
mm.restore();
await mm.restore();
assert(!app.logger._mockLogs);
assert(!app.coreLogger._mockLogs);
}
Expand Down
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
Loading