diff --git a/README.md b/README.md index de6120e..321aa90 100644 --- a/README.md +++ b/README.md @@ -104,10 +104,11 @@ for (let matchedRequest of cssSpy.getMatchedRequests()) { ## API ### class: RequestInterceptor -The `RequestInterceptor` will match any intercepted request against the `matcher` function and notify all spies with a matching pattern and block requests matching any pattern in `urlsToBlock`. +The `RequestInterceptor` will call all spies, fakers and blocker to dertermine if an intercepted request matches. against the `matcher` function and notify all spies with a matching pattern and block requests matching any pattern in `urlsToBlock`. + #### RequestInterceptor constructor(matcher, logger?) -- `matcher`: <(url: string, pattern: string) => boolean>> -- `logger?`: <{log: (text: string) => void}> +- `matcher`: \<(url: string, pattern: string) =\> boolean\>\> +- `logger?`: \<{log: (text: string) =\> void}\> The `matcher` will be called for every url, testing the url against patterns of any `RequestSpy` provided and also any url added to `urlsToBlock`. @@ -118,7 +119,7 @@ The `logger` if provided will output any requested url with a 'loaded' or 'abort Function to be registered with puppeteer's request event. #### RequestInterceptor.addSpy(requestSpy) -- requestSpy: \ spy to register +- requestSpy: \ spy to register Register a spy with the `RequestInterceptor`. @@ -126,9 +127,9 @@ Register a spy with the `RequestInterceptor`. Clears all registered spies. #### RequestInterceptor.addFaker(requestFaker) -- responseFaker: \ faker to register +- responseFaker: \ faker to register -#### RequestInterceptor.clearFakers() +#### RequestInterceptor.clearFakers() Clears all registered fakers. #### RequestInterceptor.block(urlsToBlock) @@ -142,7 +143,12 @@ Clears all registered fakers. #### RequestInterceptor.clearUrlsToBlock() Clears all registered patterns in `urlsToBlock`. -### class: RequestSpy +#### RequestInterceptor.setRequestBlocker(requestBlocker) +- requestBlocker \ + +Allows you to replace the default RequestBlocker by your own implementation. + +### class: RequestSpy implements IRequestSpy `RequestSpy` is used to count and verify intercepted requests matching a specific pattern. #### RequestSpy constructor(pattern) - `pattern`: \> @@ -161,7 +167,9 @@ Clears all registered patterns in `urlsToBlock`. #### RequestSpy.getMatchCount() - returns: \ number of urls that matched the `pattern` -#### RequestSpy.isMatchingRequest(matcher: Matcher, request: Request) +#### RequestSpy.isMatchingRequest(request, matcher) +- request \ request object provided by puppeteer +- matcher \<(url: string, pattern: string) =\> boolean\>\> matching function passed to RequestInterceptor's constructor - returns: \ returns true if any pattern provided to the RequestSpy matches the request url The `RequestInterceptor` calls this method to determine if an interceptedRequest matches the RequestSpy. @@ -171,25 +179,45 @@ The `RequestInterceptor` calls this method to determine if an interceptedRequest The `RequestInterceptor` calls this method when an interceptedRequest matches the pattern. -### class: RequestFaker -`RequestFaker` is used to provide a fake response when matched to a specific pattern. +### class: ResponseFaker implements IResponseFaker +`ResponseFaker` is used to provide a fake response when matched to a specific pattern. -#### RequestFaker constructor(pattern, responseFake) +#### ResponseFaker constructor(pattern, responseFake) - `pattern`: \> - `responseFake`: \<`Response`> for details refer to [puppeteer API](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#requestrespondresponse) -#### RequestFaker.getPatterns() +#### ResponseFaker.getPatterns() - returns: \\> return the `pattern` list of the faker -#### RequestFaker.getResponseFake() +#### ResponseFaker.getResponseFake() - returns: \ return the fake response The `RequestInterceptor` calls this method when an interceptedUrl matches the pattern. -#### RequestSpy.isMatchingRequest(matcher: Matcher, request: Request) -- returns: \ returns true if any pattern provided to the RequestFaker matches the request url +#### ResponseFaker.isMatchingRequest(request, matcher) +- request \ request object provided by puppeteer +- matcher \<(url: string, pattern: string) =\> boolean\>\> matching function passed to RequestInterceptor's constructor +- returns: \ returns true if any pattern provided to the ResponseFaker matches the request url + +The `RequestInterceptor` calls this method to determine if an interceptedRequest matches. + +### class: RequestBlocker implements IResponseBlocker +`RequestBlocker` is used to by the RequestInterceptor to match requests to block. + +#### RequestBlocker.shouldBlockRequest(request, matcher) +- request \ request object provided by puppeteer +- matcher \<(url: string, pattern: string) =\> boolean\>\> matching function passed to RequestInterceptor's constructor + +The `RequestInterceptor` calls this method to determine if an interceptedRequest matches. + +#### RequestBlocker.addUrlsToBlock(urls) +- urls \ | string\> + +Adds new urls to the block list. + +#### RequestBlocker.clearUrlsToBlock() -The `RequestInterceptor` calls this method to determine if an interceptedRequest matches the RequestFaker. +Removes all entries of the block list. # Examples