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
4 changed files
with
310 additions
and
70 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,62 @@ | ||
const expect = require('unexpected') | ||
.clone() | ||
.use(require('unexpected-messy')); | ||
const messy = require('messy'); | ||
|
||
const errors = require('../errors'); | ||
const resolveExpectedRequestProperties = require('../resolveExpectedRequestProperties'); | ||
const trimMessyHeaders = require('../trimMessyHeaders'); | ||
|
||
module.exports = class OrderedMockStrategy { | ||
constructor(requestDescriptions) { | ||
this.requestDescriptions = requestDescriptions; | ||
this.nextRequestDescriptionIndex = 0; | ||
} | ||
|
||
get isEmpty() { | ||
return this.nextRequestDescriptionIndex >= this.requestDescriptions.length; | ||
} | ||
|
||
firstDescriptionRemaining() { | ||
return this.nextDescriptionForIncomingRequest(); | ||
} | ||
|
||
nextDescriptionForIncomingRequest(requestStruct) { | ||
if (this.isEmpty) { | ||
return Promise.resolve(null); | ||
} | ||
|
||
const description = this.requestDescriptions[ | ||
this.nextRequestDescriptionIndex | ||
]; | ||
this.nextRequestDescriptionIndex += 1; | ||
|
||
if (!requestStruct) { | ||
// skip early exit when exhausting requests | ||
return Promise.resolve(description); | ||
} | ||
|
||
return Promise.resolve() | ||
.then(() => { | ||
const assertionMockRequest = new messy.HttpRequest( | ||
requestStruct.properties | ||
); | ||
trimMessyHeaders(assertionMockRequest.headers); | ||
|
||
// update the request with the spec it needs to satisfy | ||
const assertionSeenSpec = resolveExpectedRequestProperties( | ||
description.request | ||
); | ||
|
||
expect.errorMode = 'default'; | ||
return expect(assertionMockRequest, 'to satisfy', assertionSeenSpec); | ||
}) | ||
.then(() => description) | ||
.catch(e => { | ||
throw new errors.EarlyExitError({ | ||
message: 'Seen request did not match the expected request.', | ||
data: description | ||
}); | ||
}); | ||
} | ||
}; |
Oops, something went wrong.