Skip to content

Commit

Permalink
Handle DNS errors for existing hosts but wrong protocol (fixes #851) (#…
Browse files Browse the repository at this point in the history
…852)

* Handle DNS errors for existing hosts but wrong protocol (fixes #851)

* Bump version

* isDNSErr -> _isDNSErr
  • Loading branch information
inikulin authored Oct 12, 2016
1 parent 85bd321 commit db3141b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "testcafe-hammerhead",
"description": "A powerful web-proxy used as a core for the TestCafe testing framework (https://github.com/DevExpress/testcafe).",
"version": "9.3.8",
"version": "9.3.9",
"homepage": "https://github.com/DevExpress/testcafe-hammerhead",
"bugs": {
"url": "https://github.com/DevExpress/testcafe-hammerhead/issues"
Expand Down
19 changes: 13 additions & 6 deletions src/request-pipeline/destination-request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import { MESSAGE, getText } from '../../messages';
// doesn't work (see: https://github.com/mikeal/request/issues/418).
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

// Utils
function isDNSErr (err) {
return err.message && /ECONNREFUSED|ENOTFOUND/.test(err.message);
}

// DestinationRequest
export default class DestinationRequest extends EventEmitter {
Expand All @@ -24,6 +20,7 @@ export default class DestinationRequest extends EventEmitter {

this.req = null;
this.hasResponse = false;
this.aborted = false;
this.opts = opts;
this.isHttps = opts.protocol === 'https:';
this.protocolInterface = this.isHttps ? https : http;
Expand Down Expand Up @@ -73,11 +70,21 @@ export default class DestinationRequest extends EventEmitter {
}
}

_abort () {
this.aborted = true;
this.req.abort();
}

_isDNSErr (err) {
return err.message && /ECONNREFUSED|ENOTFOUND/.test(err.message) ||
!this.aborted && !this.hasResponse && err.code && /ECONNRESET/.test(err.code);
}

_onTimeout () {
// NOTE: this handler is also called if we get an error response (for example, 404). So, we should check
// for the response presence before raising the timeout error.
if (!this.hasResponse) {
this.req.abort();
this._abort();
this.emit('fatalError', getText(MESSAGE.destRequestTimeout, this.opts.url));
}
}
Expand All @@ -88,7 +95,7 @@ export default class DestinationRequest extends EventEmitter {
this._send();
}

else if (isDNSErr(err))
else if (this._isDNSErr(err))
this.emit('fatalError', getText(MESSAGE.cantResolveUrl, this.opts.url));
else
this.emit('error');
Expand Down
18 changes: 18 additions & 0 deletions test/server/proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,24 @@ describe('Proxy', function () {
request(options);
});

it('Should pass protocol DNS errors for existing host to session', function (done) {
session.handlePageError = function (ctx, err) {
expect(err).eql('Failed to find a DNS-record for the resource at <a href="https://127.0.0.1:2000">https://127.0.0.1:2000</a>.');
ctx.res.end();
done();
return true;
};

var options = {
url: proxy.openSession('https://127.0.0.1:2000', session),
headers: {
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*!/!*;q=0.8'
}
};

request(options);
});

it('Should pass service message processing to session', function (done) {
var options = {
method: 'POST',
Expand Down

0 comments on commit db3141b

Please sign in to comment.