Skip to content

Commit

Permalink
馃悰 FIX: Allow override HttpClientNext (#5037)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Sep 28, 2022
1 parent e5bed4e commit 7ee19e8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/egg.js
Expand Up @@ -52,6 +52,7 @@ class EggApplication extends EggCore {
this.ContextLogger = ContextLogger;
this.ContextHttpClient = ContextHttpClient;
this.HttpClient = HttpClient;
this.HttpClientNext = HttpClientNext;

this.loader.loadConfig();

Expand Down Expand Up @@ -293,7 +294,7 @@ class EggApplication extends EggCore {
get httpclient() {
if (!this[HTTPCLIENT]) {
if (this.config.httpclient.useHttpClientNext) {
this[HTTPCLIENT] = new HttpClientNext(this);
this[HTTPCLIENT] = new this.HttpClientNext(this);
} else if (this.config.httpclient.enableDNSCache) {
this[HTTPCLIENT] = new DNSCacheHttpClient(this);
} else {
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/apps/httpclient-next-overwrite/app.js
@@ -0,0 +1,21 @@
'use strict';

const assert = require('assert');

module.exports = app => {
class CustomHttpClient extends app.HttpClientNext {
request(url, opt) {
return new Promise(resolve => {
assert(/^http/.test(url), 'url should start with http, but got ' + url);
resolve();
}).then(() => {
return super.request(url, opt);
});
}

curl(url, opt) {
return this.request(url, opt);
}
}
app.HttpClientNext = CustomHttpClient;
};
@@ -0,0 +1,8 @@
'use strict';

exports.httpclient = {
useHttpClientNext: true,
request: {
timeout: 99,
},
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/httpclient-next-overwrite/package.json
@@ -0,0 +1,3 @@
{
"name": "httpclient-overwrite"
}
26 changes: 26 additions & 0 deletions test/lib/core/httpclient.test.js
Expand Up @@ -260,6 +260,32 @@ describe('test/lib/core/httpclient.test.js', () => {
});
});

describe('overwrite httpclient support useHttpClientNext=true', () => {
let app;
before(() => {
app = utils.app('apps/httpclient-next-overwrite');
return app.ready();
});
after(() => app.close());

it('should set request default global timeout to 99ms', () => {
return app.httpclient.curl(`${url}/timeout`)
.catch(err => {
assert(err);
assert(err.name === 'HttpClientRequestTimeoutError');
assert(err.message.includes('Request timeout for 99 ms'));
});
});

it('should assert url', () => {
return app.httpclient.curl('unknown url')
.catch(err => {
assert(err);
assert(err.message.includes('url should start with http, but got unknown url'));
});
});
});

describe('httpclient tracer', () => {
let app;
before(() => {
Expand Down

0 comments on commit 7ee19e8

Please sign in to comment.