forked from FormidableLabs/yesno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FormidableLabs#64 First pass at matching().respond()
- Loading branch information
1 parent
b9d2907
commit dcb0413
Showing
6 changed files
with
151 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,84 @@ | ||
import { ISerializedHttp, RequestSerializer } from './http-serializer'; | ||
import { ResponseForRequest } from './filtering/collection'; | ||
import { match, Matcher } from './filtering/matcher'; | ||
import { | ||
ISerializedHttp, | ||
ISerializedRequest, | ||
ISerializedResponse, | ||
RequestSerializer, | ||
} from './http-serializer'; | ||
import { RecordMode as Mode } from './recording'; | ||
|
||
export interface IInFlightRequest { | ||
startTime: number; | ||
requestSerializer: RequestSerializer; | ||
} | ||
|
||
export interface IResponseForMatchingRequest { | ||
response: ResponseForRequest; | ||
matcher: Matcher; | ||
} | ||
|
||
/** | ||
* Store the current execution context for YesNo by tracking requests & mocks. | ||
*/ | ||
export default class Context { | ||
public mode: Mode = Mode.Spy; | ||
|
||
/** | ||
* Completed serialized request-response objects. Used for: | ||
* A. Assertions | ||
* B. Saved to disk if in record mode | ||
*/ | ||
public interceptedRequestsCompleted: ISerializedHttp[] = []; | ||
|
||
/** | ||
* Serialized records loaded from disk. | ||
*/ | ||
public loadedMocks: ISerializedHttp[] = []; | ||
|
||
/** | ||
* Proxied requests which have not yet responded. When completed | ||
* the value is set to "null" but the index is preserved. | ||
*/ | ||
public inFlightRequests: Array<IInFlightRequest | null> = []; | ||
|
||
public responsesForMatchingRequests: IResponseForMatchingRequest[] = []; | ||
|
||
public clear() { | ||
this.interceptedRequestsCompleted = []; | ||
this.inFlightRequests = []; | ||
this.loadedMocks = []; | ||
this.responsesForMatchingRequests = []; | ||
} | ||
|
||
public getMatchingMocks(matcher: Matcher): ISerializedHttp[] { | ||
return this.loadedMocks.filter(match(matcher)); | ||
} | ||
|
||
public getMatchingIntercepted(matcher: Matcher): ISerializedHttp[] { | ||
return this.interceptedRequestsCompleted.filter(match(matcher)); | ||
} | ||
|
||
public hasResponsesDefinedForMatchers(): boolean { | ||
return !!this.responsesForMatchingRequests.length; | ||
} | ||
|
||
public addResponseForMatchingRequests(matchingResponse: IResponseForMatchingRequest): void { | ||
this.responsesForMatchingRequests.push(matchingResponse); | ||
} | ||
|
||
public getResponseDefinedMatching(request: ISerializedRequest): ISerializedResponse | undefined { | ||
let firstMatchingResponse: ISerializedResponse | undefined; | ||
|
||
for (const { matcher, response } of this.responsesForMatchingRequests) { | ||
const doesMatch = match(matcher)({ request }); | ||
|
||
if (doesMatch) { | ||
firstMatchingResponse = typeof response === 'function' ? response(request) : response; | ||
break; | ||
} | ||
} | ||
|
||
return firstMatchingResponse; | ||
} | ||
} |
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
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