Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
258 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
var Factory = require('./Factory').Factory | ||
|
||
var HttpRequestOptions = function () { | ||
this.headers = [] | ||
this.method = 'GET' | ||
this.params = {} | ||
this.postData = '' | ||
this.rawOutput = false | ||
this.timeout = 90 | ||
} | ||
|
||
Object.defineProperty(HttpRequestOptions, 'name', { | ||
value: 'Net.HttpRequestOptions', | ||
writable: false | ||
}) | ||
|
||
var HttpRequestResult = function () { | ||
this.code = 200 | ||
this.data = undefined | ||
this.error = '' | ||
this.headers = [] | ||
this.raw_headers = '' | ||
this.text = '' | ||
} | ||
|
||
Object.defineProperty(HttpRequestResult, 'name', { | ||
value: 'Net.HttpRequestResult', | ||
writable: false | ||
}) | ||
|
||
var definition = { | ||
defaults: { | ||
http: { | ||
responses: [], | ||
roundRobin: false | ||
} | ||
}, | ||
state: { | ||
http: {}, | ||
log: { | ||
http: [] | ||
} | ||
}, | ||
properties: { | ||
HttpRequestOptions: HttpRequestOptions, | ||
HttpRequestResult: HttpRequestResult | ||
}, | ||
handlers: { | ||
httpRequest: function (self, url, callback, options) { | ||
if (!self._state.http.responses) { | ||
self._state.http.responses = self._settings.http.responses.slice() | ||
} | ||
var responses = self._state.http.responses | ||
var index = -1 | ||
for (var i = 0; i < responses.length; i++) { | ||
var cursor = responses[i] | ||
if (!cursor.matcher || cursor.matcher(url, options)) { | ||
index = i | ||
break | ||
} | ||
} | ||
if (index === -1) { | ||
var message = 'Failed to find matching response for url ' + url + | ||
' and options ' + JSON.stringify(options) | ||
throw new Error(message) | ||
} | ||
var response = responses.splice(index, 1)[0] | ||
if (self._settings.http.roundRobin) { | ||
responses.push(response) | ||
} | ||
var structure = new HttpRequestResult() | ||
var properties = ['code', 'headers', 'data', 'raw_headers', 'text'] | ||
properties.forEach(function (property) { | ||
if (response.hasOwnProperty(property)) { | ||
structure[property] = response[property] | ||
} | ||
}) | ||
if (response.delay) { | ||
response.delay().then(callback.bind(null, structure)) | ||
} else { | ||
callback(structure) | ||
} | ||
}, | ||
httpRequestAsync: function (self, url, options) { | ||
return new Promise(function (resolve) { | ||
definition.handlers.httpRequest(self, url, resolve, options) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
Net: Factory.create('Net', definition) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* @callback FOnStubFlush | ||
* | ||
* @param {IStub} self | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* eslint-env mocha */ | ||
/* eslint-disable no-unused-expressions */ | ||
|
||
var Sinon = require('sinon') | ||
var Chai = require('chai') | ||
var expect = Chai.expect | ||
|
||
Chai.use(require('chai-as-promised')) | ||
|
||
var Net = require('../../../../lib/Stub/Net').Net | ||
|
||
describe('Integration', function () { | ||
describe('/Stub', function () { | ||
describe('/Net.js', function () { | ||
describe('.Net', function () { | ||
describe('.httpRequestAsync', function () { | ||
var options | ||
var instance | ||
var response | ||
|
||
beforeEach(function () { | ||
response = { | ||
code: 200, | ||
headers: [] | ||
} | ||
options = { | ||
http: { | ||
responses: [response] | ||
} | ||
} | ||
autoFactory() | ||
}) | ||
|
||
var autoFactory = function () { | ||
instance = new Net(options) | ||
return instance | ||
} | ||
|
||
it('uses first response if it has no matcher', function () { | ||
options.http.responses = [{code: 200}] | ||
autoFactory() | ||
return instance | ||
.httpRequestAsync('test') | ||
.then(function (value) { | ||
expect(value).to.have.property('code').eq(response.code) | ||
}) | ||
}) | ||
|
||
it('uses next response if first didn\'t match', function () { | ||
var expectedCode = 200 | ||
options.http.responses = [ | ||
{ | ||
code: 404, | ||
matcher: function () { | ||
return false | ||
} | ||
}, | ||
{ | ||
code: expectedCode, | ||
matcher: function () { | ||
return true | ||
} | ||
} | ||
] | ||
autoFactory() | ||
return instance | ||
.httpRequestAsync('test') | ||
.then(function (value) { | ||
expect(value).to.have.property('code').eq(expectedCode) | ||
}) | ||
}) | ||
|
||
it('throws if no responses have matched', function () { | ||
options.http.responses = [] | ||
autoFactory() | ||
var promise = instance.httpRequestAsync('url') | ||
return expect(promise).to.eventually.be.rejected | ||
}) | ||
|
||
it('discards response if roundRobin is turned off', function () { | ||
options.http.roundRobin = false | ||
autoFactory() | ||
return instance | ||
.httpRequestAsync('url') | ||
.then(function () { | ||
var promise = instance.httpRequestAsync('url') | ||
return expect(promise).to.eventually.be.rejected | ||
}) | ||
}) | ||
|
||
it('places response in the end if roundRobin is turned on', function () { | ||
options.http.roundRobin = true | ||
options.http.responses = [ | ||
{code: 200}, | ||
{code: 201} | ||
] | ||
autoFactory() | ||
return instance | ||
.httpRequestAsync('url') | ||
.then(function (response) { | ||
expect(response.code).to.eq(200) | ||
return instance.httpRequestAsync('url') | ||
}) | ||
.then(function (response) { | ||
expect(response.code).to.eq(201) | ||
return instance.httpRequestAsync('url') | ||
}) | ||
.then(function (response) { | ||
expect(response.code).to.eq(200) | ||
}) | ||
}) | ||
|
||
it('uses 200 as default code', function () { | ||
response = {} | ||
autoFactory() | ||
return instance | ||
.httpRequestAsync('url') | ||
.then(function (value) { | ||
expect(value.code).to.eq(200) | ||
}) | ||
}) | ||
|
||
it('respects provided delay', function () { | ||
var handler = Sinon.spy(function () { | ||
return Promise.resolve() | ||
}) | ||
options.http.responses = [ | ||
{delay: handler} | ||
] | ||
autoFactory() | ||
return instance | ||
.httpRequestAsync('url') | ||
.then(function () { | ||
expect(handler.callCount).to.eq(1) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('.HttpRequestOptions', function () { | ||
it('is instantiable', function () { | ||
var lambda = function () { | ||
var net = new Net() | ||
return new net.HttpRequestOptions() | ||
} | ||
expect(lambda).not.to.throw() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |