Skip to content

Commit

Permalink
removed conditional acceptUrl and added regression tests for issue #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabueeee committed May 21, 2018
1 parent 91bc754 commit ea22821
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
## Install

```bash
npm install puppeteer-request-spy
npm install puppeteer-request-spy --save-dev
```
## Usage
Expand Down Expand Up @@ -80,7 +80,7 @@ let pngSpy = new RequestSpy('**/*.png');
let responseFaker = new ResponseFaker('**/*.jpg', someFakeResponse);

let requestInterceptor = new RequestInterceptor(minimatch);
responseFaker.addFaker(mock);
responseFaker.addFaker(responseFaker);
requestInterceptor.addSpy(pngSpy);
requestInterceptor.block('!https://www.example.com');

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "puppeteer-request-spy",
"version": "1.1.1",
"version": "1.1.2",
"description": "watch, fake or block requests from puppeteer matching patterns",
"main": "build/src/index.js",
"scripts": {
Expand Down
11 changes: 3 additions & 8 deletions src/RequestInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,11 @@ export class RequestInterceptor {
}

private async acceptUrl(interceptedUrl: Request, url: string): Promise<void> {
if (this.urlsToBlock.length > 0) {
try {
await interceptedUrl.continue();
this.logger.log(`loaded: ${url}`);
} catch (error) {
this.logger.log((<Error> error).toString());
}
} else {
try {
await interceptedUrl.continue();
this.logger.log(`loaded: ${url}`);
} catch (error) {
this.logger.log((<Error> error).toString());
}
}
}
8 changes: 4 additions & 4 deletions test/integration/puppeteer-request-spy.deploy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ describe('puppeteer-request-spy: integration', function (): void {
it('RequestFaker faking request without requestInterception flag', async (): Promise<void> => {
let logs: Array<string> = [];
let expectedLogs: Array<string> = [
`loaded: ${staticServerIp}/index.html`,
`loaded: ${staticServerIp}/style.css`,
`loaded: ${staticServerIp}/script.js`,
`loaded: ${staticServerIp}/remote.html`
'AssertionError [ERR_ASSERTION]: Request Interception is not enabled!',
'AssertionError [ERR_ASSERTION]: Request Interception is not enabled!',
'AssertionError [ERR_ASSERTION]: Request Interception is not enabled!',
'AssertionError [ERR_ASSERTION]: Request Interception is not enabled!'
];

let responseFaker: ResponseFaker = new ResponseFaker('*remote*', {
Expand Down
239 changes: 239 additions & 0 deletions test/regression/unit.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
import * as assert from 'assert';
import {Request} from 'puppeteer';
import * as sinon from 'sinon';
import {SinonSpy} from 'sinon';
import {RequestInterceptor} from '../../src/RequestInterceptor';
import {RequestSpy} from '../../src/RequestSpy';
import {ResponseFaker} from '../../src/ResponseFaker';
import {TestDouble} from '../common/TestDouble';
import {getRequestDouble} from '../common/testDoubleFactories';

describe('puppeteer-request-spy: regression-unit: #4 Locks after starting to intercept requests when there are no urls to block added ', (): void => {
describe('happy-path: ensure continue is called, when requestInterceptor does not match', () => {
let requestInterceptor: RequestInterceptor;

before(() => {
let matcher: (testString: string, pattern: string) => boolean = sinon.stub().returns(false);
requestInterceptor = new RequestInterceptor(matcher, {log: (): void => undefined});
});

beforeEach(() => {
requestInterceptor.clearUrlsToBlock();
requestInterceptor.clearSpies();
requestInterceptor.clearFakers();
});

it('RequestSpy only', async (): Promise<void> => {
let requestSpy: TestDouble<RequestSpy> = new RequestSpy('some/pattern/**/*');
let request: TestDouble<Request> = getRequestDouble();

requestInterceptor.addSpy(<RequestSpy> requestSpy);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === true && (<SinonSpy> request.respond).called === false && (<SinonSpy> request.abort).called === false);
});

it('ResponseFaker only', async (): Promise<void> => {
let responseFaker: TestDouble<ResponseFaker> = new ResponseFaker('some/pattern/**/*', {});
let request: TestDouble<Request> = getRequestDouble();

requestInterceptor.addFaker(<ResponseFaker> responseFaker);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === true && (<SinonSpy> request.respond).called === false && (<SinonSpy> request.abort).called === false);
});

it('block only', async (): Promise<void> => {
let request: TestDouble<Request> = getRequestDouble();

requestInterceptor.block(['not-matching-pattern1', 'not-matching-pattern2']);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === true && (<SinonSpy> request.respond).called === false && (<SinonSpy> request.abort).called === false);
});

it('block and Faker', async (): Promise<void> => {
let request: TestDouble<Request> = getRequestDouble();
let responseFaker: TestDouble<ResponseFaker> = new ResponseFaker('some/pattern/**/*', {});

requestInterceptor.addFaker(<ResponseFaker> responseFaker);
requestInterceptor.block(['not-matching-pattern1', 'not-matching-pattern2']);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === true && (<SinonSpy> request.respond).called === false && (<SinonSpy> request.abort).called === false);
});

it('Spy and Faker', async (): Promise<void> => {
let request: TestDouble<Request> = getRequestDouble();
let responseFaker: TestDouble<ResponseFaker> = new ResponseFaker('some/pattern/**/*', {});
let requestSpy: TestDouble<RequestSpy> = new RequestSpy('some/pattern/**/*');

requestInterceptor.addSpy(<RequestSpy> requestSpy);
requestInterceptor.addFaker(<ResponseFaker> responseFaker);

await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === true && (<SinonSpy> request.respond).called === false && (<SinonSpy> request.abort).called === false);
});

it('Spy and block', async (): Promise<void> => {
let request: TestDouble<Request> = getRequestDouble();
let requestSpy: TestDouble<RequestSpy> = new RequestSpy('some/pattern/**/*');

requestInterceptor.addSpy(<RequestSpy> requestSpy);
requestInterceptor.block(['not-matching-pattern1', 'not-matching-pattern2']);

await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === true && (<SinonSpy> request.respond).called === false && (<SinonSpy> request.abort).called === false);
});

it('Spy, block and Faker', async (): Promise<void> => {
let request: TestDouble<Request> = getRequestDouble();
let requestSpy: TestDouble<RequestSpy> = new RequestSpy('some/pattern/**/*');
let responseFaker: TestDouble<ResponseFaker> = new ResponseFaker('some/pattern/**/*', {});

requestInterceptor.addFaker(<ResponseFaker> responseFaker);
requestInterceptor.addSpy(<RequestSpy> requestSpy);
requestInterceptor.block(['not-matching-pattern1', 'not-matching-pattern2']);

await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === true && (<SinonSpy> request.respond).called === false && (<SinonSpy> request.abort).called === false);
});
});

describe('sad-path: ensure only one request action is called, when requestInterceptor does match', () => {
let requestInterceptor: RequestInterceptor;

before(() => {
let matcher: (testString: string, pattern: string) => boolean = sinon.stub().returns(true);
requestInterceptor = new RequestInterceptor(matcher, {log: (): void => undefined});
});

beforeEach(() => {
requestInterceptor.clearUrlsToBlock();
requestInterceptor.clearSpies();
requestInterceptor.clearFakers();
});

it('RequestSpy only', async (): Promise<void> => {
let requestSpy: TestDouble<RequestSpy> = new RequestSpy('some/pattern/**/*');
let request: TestDouble<Request> = getRequestDouble();

requestInterceptor.addSpy(<RequestSpy> requestSpy);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === true && (<SinonSpy> request.respond).called === false && (<SinonSpy> request.abort).called === false);
});

it('RequestSpy only', async (): Promise<void> => {
let requestSpy: TestDouble<RequestSpy> = new RequestSpy('some/pattern/**/*');
let request: TestDouble<Request> = getRequestDouble();

requestInterceptor.addSpy(<RequestSpy> requestSpy);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === true && (<SinonSpy> request.respond).called === false && (<SinonSpy> request.abort).called === false);
});

it('ResponseFaker only', async (): Promise<void> => {
let responseFaker: TestDouble<ResponseFaker> = new ResponseFaker('some/pattern/**/*', {});
let request: TestDouble<Request> = getRequestDouble();

requestInterceptor.addFaker(<ResponseFaker> responseFaker);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === false && (<SinonSpy> request.respond).called === true && (<SinonSpy> request.abort).called === false);
});

it('block only', async (): Promise<void> => {
let request: TestDouble<Request> = getRequestDouble();

requestInterceptor.block(['not-matching-pattern1', 'not-matching-pattern2']);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === false && (<SinonSpy> request.respond).called === false && (<SinonSpy> request.abort).called === true);
});

it('block and Faker', async (): Promise<void> => {
let request: TestDouble<Request> = getRequestDouble();
let responseFaker: TestDouble<ResponseFaker> = new ResponseFaker('some/pattern/**/*', {});

requestInterceptor.addFaker(<ResponseFaker> responseFaker);
requestInterceptor.block(['not-matching-pattern1', 'not-matching-pattern2']);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === false && (<SinonSpy> request.respond).called === false && (<SinonSpy> request.abort).called === true);
});

it('Spy and Faker', async (): Promise<void> => {
let request: TestDouble<Request> = getRequestDouble();
let responseFaker: TestDouble<ResponseFaker> = new ResponseFaker('some/pattern/**/*', {});
let requestSpy: TestDouble<RequestSpy> = new RequestSpy('some/pattern/**/*');

requestInterceptor.addSpy(<RequestSpy> requestSpy);
requestInterceptor.addFaker(<ResponseFaker> responseFaker);

await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === false && (<SinonSpy> request.respond).called === true && (<SinonSpy> request.abort).called === false);
});

it('Spy and block', async (): Promise<void> => {
let request: TestDouble<Request> = getRequestDouble();
let requestSpy: TestDouble<RequestSpy> = new RequestSpy('some/pattern/**/*');

requestInterceptor.addSpy(<RequestSpy> requestSpy);
requestInterceptor.block(['not-matching-pattern1', 'not-matching-pattern2']);

await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === false && (<SinonSpy> request.respond).called === false && (<SinonSpy> request.abort).called === true);
});

it('Spy, block and Faker', async (): Promise<void> => {
let request: TestDouble<Request> = getRequestDouble();
let requestSpy: TestDouble<RequestSpy> = new RequestSpy('some/pattern/**/*');
let responseFaker: TestDouble<ResponseFaker> = new ResponseFaker('some/pattern/**/*', {});

requestInterceptor.addFaker(<ResponseFaker> responseFaker);
requestInterceptor.addSpy(<RequestSpy> requestSpy);
requestInterceptor.block(['not-matching-pattern1', 'not-matching-pattern2']);

await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);
await requestInterceptor.intercept(<Request> request);

assert.ok((<SinonSpy> request.continue).called === false && (<SinonSpy> request.respond).called === false && (<SinonSpy> request.abort).called === true);
});
});
});

0 comments on commit ea22821

Please sign in to comment.