Skip to content

Commit

Permalink
Merge pull request #3 from Tabueeee/v-1.1.1
Browse files Browse the repository at this point in the history
fixed not working on all lang levels; improved typings documentation …
  • Loading branch information
Tabueeee committed May 7, 2018
2 parents 2754829 + 2bb4bdf commit 4a60aae
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 80 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ let responseFaker = new ResponseFaker('/ajax/some-request', {
body: JSON.stringify({successful: false, payload: []})
});

requestInterceptor.addFaker(mock);
requestInterceptor.addFaker(responseFaker);
```
For further details on how to replace different formats of data like images, text or html, please refer to the examples provided in the [github repository](https://github.com/Tabueeee/puppeteer-request-spy/tree/master/examples).

Expand Down Expand Up @@ -134,7 +134,7 @@ Clears all registered patterns in `urlsToBlock`.
### class: RequestSpy
`RequestSpy` is used to count and verify intercepted requests matching a specific pattern.
#### RequestSpy constructor(pattern)
- `pattern`: \<string|Array<string>>
- `pattern`: \<string|Array\<string>>

`pattern` passed to the `matcher` function of the `RequestInterceptor`.

Expand All @@ -160,7 +160,7 @@ The `RequestInterceptor` calls this method when an interceptedUrl matches the pa

#### RequestSpy constructor(pattern, responseFake)
- `pattern`: \<string|Array<string>>
- `responseFake`: `Response` for details refer to [puppeteer API](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#requestrespondresponse)
- `responseFake`: \<`Response`> for details refer to [puppeteer API](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#requestrespondresponse)

#### RequestFaker.getPatterns()
- returns: \<Array\<string\>\> return the `pattern` list of the faker
Expand Down
4 changes: 2 additions & 2 deletions examples/fake-test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('example-block', function () {

before(() => {
requestInterceptor = new RequestInterceptor(minimatch, console);
defaultPicture = fs.readFileSync('./t2.png');
defaultPicture = fs.readFileSync('./some-picture.png');
imageResponseFaker = new ResponseFaker('**/*.jpg', {
status: 200,
contentType: 'image/png',
Expand All @@ -52,7 +52,7 @@ describe('example-block', function () {

jsonResponseFaker = new ResponseFaker('**/*.jpg', {
status: 200,
contentType: 'image/png',
contentType: 'application/json',
body: JSON.stringify({data: []})
});

Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "puppeteer-request-spy",
"version": "1.1.0",
"version": "1.1.1",
"description": "watch, fake or block requests from puppeteer matching patterns",
"main": "build/src/index.js",
"scripts": {
Expand All @@ -10,9 +10,10 @@
"build-dev": "node_modules/.bin/tsc -p tsconfig.json --sourcemap",
"pretest": "npm run build",
"test": "node_modules/.bin/mocha --timeout 10000 --require source-map-support/register build/test/**/*.spec.js",
"test-silent": "node_modules/.bin/mocha --timeout 10000 --require source-map-support/register build/test/isolation/**/*.dev.spec.js > test-ts.log",
"test-silent-full": "node_modules/.bin/mocha --timeout 10000 --require source-map-support/register build/test/**/*.spec.js > test-ts.log",
"test-silent": "node_modules/.bin/mocha --timeout 10000 --require source-map-support/register build/test/**/*.dev.spec.js > test-ts.log",
"pretest-coverage": "npm run build-dev",
"test-coverage": "node_modules/.bin/nyc --all --reporter=html npm run test-silent",
"test-coverage": "node_modules/.bin/nyc --all --reporter=html npm run test-silent-full",
"pretest-coverage-travis": "npm run build-dev",
"test-coverage-travis": "node_modules/.bin/nyc --all --reporter=text-lcov npm run test-silent | node node_modules/coveralls/bin/coveralls.js"
},
Expand Down Expand Up @@ -51,10 +52,10 @@
"devDependencies": {
"@types/minimatch": "^3.0.3",
"@types/mocha": "^5.2.0",
"@types/node": "^10.0.0",
"@types/node": "^10.0.4",
"@types/puppeteer": "^1.3.1",
"@types/sinon": "^4.3.1",
"@types/puppeteer": "^1.2.3",
"coveralls": "^3.0.0",
"coveralls": "^3.0.1",
"minimatch": "^3.0.4",
"mocha": "^5.1.1",
"nyc": "^11.7.1",
Expand Down
20 changes: 0 additions & 20 deletions src/RequestInspector.ts

This file was deleted.

19 changes: 15 additions & 4 deletions src/RequestSpy.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import {RequestInspector} from './RequestInspector';

export class RequestSpy extends RequestInspector {
export class RequestSpy {

private hasMatchingUrl: boolean = false;
private matchedUrls: Array<string> = [];
private matchCount: number = 0;
private patterns: Array<string> = [];

public constructor(patterns: Array<string> | string) {
super(patterns);
if (typeof patterns !== 'string' && patterns.constructor !== Array) {
throw new Error('invalid pattern, pattern must be of type string or string[].');
}

if (typeof patterns === 'string') {
patterns = [patterns];
}

this.patterns = patterns;
}

public getPatterns(): Array<string> {
return this.patterns;
}

public hasMatch(): boolean {
Expand Down
18 changes: 15 additions & 3 deletions src/ResponseFaker.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import {RespondOptions} from 'puppeteer';
import {RequestInspector} from './RequestInspector';

export class ResponseFaker extends RequestInspector {
export class ResponseFaker {

private responseFake: RespondOptions;
private patterns: Array<string> = [];

public constructor(patterns: Array<string> | string, responseFake: RespondOptions) {
super(patterns);
if (typeof patterns !== 'string' && patterns.constructor !== Array) {
throw new Error('invalid pattern, pattern must be of type string or string[].');
}

if (typeof patterns === 'string') {
patterns = [patterns];
}

this.patterns = patterns;
this.responseFake = responseFake;
}

public getResponseFake(): RespondOptions {
return this.responseFake;
}

public getPatterns(): Array<string> {
return this.patterns;
}
}
59 changes: 17 additions & 42 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,43 @@
import {Request, RespondOptions} from 'puppeteer';
import {Logger} from './common/Logger';
import { Request } from 'puppeteer';
import { Logger } from './common/Logger';

export declare abstract class RequestInspector {
private patterns;

constructor(patterns: Array<string> | string);

getPatterns(): Array<string>;
}

/**
* @description 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.
**/
export declare class RequestInterceptor {
private spies;
private requestSpies;
private responseFakers;
private urlsToBlock;
private matcher;
private logger;

constructor(matcher: (testString: string, pattern: string) => boolean, logger?: Logger);

intercept(interceptedUrl: Request): void;

intercept(interceptedUrl: Request): Promise<void>;
addSpy(requestSpy: RequestSpy): void;

addFaker(responseFaker: ResponseFaker): void;
block(urls: Array<string> | string): void;

clearSpies(): void;

clearFakers(): void;
clearUrlsToBlock(): void;

setUrlsToBlock(urlsToBlock: Array<string>): void;

private blockUrl(interceptedUrl);

private acceptUrl(interceptedUrl);
private blockUrl(interceptedUrl, url);
private acceptUrl(interceptedUrl, url);
}

/**
* @description RequestSpy is used to count and verify intercepted requests matching a specific pattern.
**/
export declare class RequestSpy extends RequestInspector {
export declare class RequestSpy {
private hasMatchingUrl;
private matchedUrls;
private matchCount;

private patterns;
constructor(patterns: Array<string> | string);

getPatterns(): Array<string>;
hasMatch(): boolean;

getMatchedUrls(): Array<string>;

getMatchCount(): number;

addMatchedUrl(interceptedRequest: string): void;
addMatchedUrl(matchedUrl: string): void;
}

/**
* @description RequestFaker is used to provide a fake response when matched to a specific pattern.
**/
export declare class ResponseFaker extends RequestInspector {
import { RespondOptions } from 'puppeteer';
export declare class ResponseFaker {
private responseFake;

private patterns;
constructor(patterns: Array<string> | string, responseFake: RespondOptions);

getResponseFake(): RespondOptions;
getPatterns(): Array<string>;
}

0 comments on commit 4a60aae

Please sign in to comment.