Skip to content

Commit

Permalink
feat: QA-15 Added http methods for GET,POST,PUT,DELETE with filter as…
Browse files Browse the repository at this point in the history
… object or string. Renamed addRestMock to mockREST.

AddRestMock became deprecated.
  • Loading branch information
pawfa committed Jul 22, 2019
1 parent a02881a commit e4c0444
Show file tree
Hide file tree
Showing 6 changed files with 629 additions and 64 deletions.
106 changes: 99 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test('can create client', async () => {
const mocketeer = Mocketeer.setup(page);

// Set up a mock
const mock = await mocketeer.addRestMock(
const mock = await mocketeer.mockREST(
{
method: 'POST',
path: '/api/user',
Expand Down Expand Up @@ -64,7 +64,7 @@ import { Mocketeer } from '@hltech/mocketeer';
const mocketeer = Mocketeer.setup(page);

// Set up a mock
const mock = await mocketeer.addRestMock(
const mock = await mocketeer.mockREST(
{
method: 'POST',
path: '/api/user',
Expand Down Expand Up @@ -133,7 +133,7 @@ const page = await browser.newPage();
await mocketeer.activate(page);
```

#### .addRestMock(filter: RequestFilter, response: MockedResponse, options?): RestMock
#### .mockREST(filter: RequestFilter, response: MockedResponse, options?): RestMock

Respond to xhr and fetch requests that match the `filter` with provided `response`.
Pass query params through `query` argument in `filter` object or simply append text to `url`
Expand All @@ -158,7 +158,7 @@ Newly created instance of `RestMock`.
###### Example

```typescript
mocketeer.addRestMock(
mocketeer.mockREST(
{
method: 'POST',
url: '/api/clients',
Expand All @@ -175,7 +175,7 @@ mocketeer.addRestMock(
###### Example with query passed as an argument

```typescript
mocketeer.addRestMock(
mocketeer.mockREST(
{
method: 'GET',
url: '/api/clients',
Expand All @@ -196,7 +196,7 @@ mocketeer.addRestMock(
###### Example with queryString appended to the url

```typescript
mocketeer.addRestMock(
mocketeer.mockREST(
{
method: 'GET',
url: '/api/clients?city=Bristol&limit=10',
Expand All @@ -210,6 +210,98 @@ mocketeer.addRestMock(
);
```

#### .mockGET(filter: RequestMethodFilter | string, response: MockedResponse, options?): RestMock

#### .mockPOST(filter: RequestMethodFilter | string, response: MockedResponse, options?): RestMock

#### .mockPUT(filter: RequestMethodFilter | string, response: MockedResponse, options?): RestMock

#### .mockDELETE(filter: RequestMethodFilter | string, response: MockedResponse, options?): RestMock

Respond to xhr and fetch requests with adequate rest method that match the `filter` with provided `response`.
Pass `filter` as an object or as an `url` string.
Pass query params through `query` argument in `filter` object or simply append text to `url`

###### Arguments

- `filter` _(RequestMethodFilter)_ used to determine if request matches the mock
- `url: string`
- `query(optional): QueryObject` object literal which accepts strings and arrays of strings as values, transformed to queryString
- `filter` _(string)_ `url` used to determine if request matches the mock
- `response` _(MockedResponse)_ content of mocked response
- `status: number`
- `headers: object`
- `body: any`
- `options` _(object)_ optional config object
- `prority` _(number)_ when intercepted request matches multiple mock, mocketeer will use the one with highest priority

###### Returns

Newly created instance of `RestMock`.

###### Example of GET request

```typescript
mocketeer.mockGET(
{
url: '/api/clients',
},
{
status: 201,
body: {
clientId: 12345,
},
}
);
```

###### Example of GET request with query passed as an argument

```typescript
mocketeer.mockGET(
{
url: '/api/clients',
query: {
city: 'Bristol',
limit: 10,
},
},
{
status: 200,
body: {
clientId: 12345,
},
}
);
```

###### Example of GET request with queryString appended to the url

```typescript
mocketeer.mockGET(
{
url: '/api/clients?city=Bristol&limit=10',
},
{
status: 200,
body: {
clientId: 12345,
},
}
);
```

###### Example of GET request with filter as a string with query

```typescript
mocketeer.mockGET('/api/clients?city=Bristol&limit=10', {
status: 200,
body: {
clientId: 12345,
},
});
```

---

### RestMock
Expand Down Expand Up @@ -239,7 +331,7 @@ Promise resolved with `MatchedRequest` - object representing request that matche
###### Example

```typescript
const createClientMock = mocketeer.addRestMock(
const createClientMock = mocketeer.mockREST(
{
method: 'POST',
url: '/api/clients',
Expand Down
84 changes: 83 additions & 1 deletion src/mocketeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { parse } from 'url';
import { Page, Request, ResourceType } from 'puppeteer';
import dbg from 'debug';
import { RestMock } from './rest-mock';
import { MockedResponse, MockOptions, RequestFilter } from './types';
import {
MockedResponse,
MockOptions,
RequestFilter,
RequestMethodFilter,
REST_METHOD,
} from './types';
import { printRequest, requestToPlainObject } from './utils';

const interceptedTypes: ResourceType[] = ['xhr', 'fetch'];
Expand Down Expand Up @@ -41,6 +47,10 @@ export class Mocketeer {
page.on('request', request => this.onRequest(request));
}

/*
* Use mockREST instead
* @deprecated
*/
public addRestMock(
filter: RequestFilter,
response: MockedResponse,
Expand All @@ -53,6 +63,78 @@ export class Mocketeer {
return mock;
}

public mockREST(
filter: RequestFilter,
response: MockedResponse,
options?: Partial<MockOptions>
): RestMock {
const mock = new RestMock(filter, response, {
...options,
});
this.mocks.push(mock);
return mock;
}

public mockGET(
filter: RequestMethodFilter | string,
response: MockedResponse,
options?: Partial<MockOptions>
): RestMock {
const filterObject =
typeof filter === 'string' ? { url: filter } : filter;

return this.mockREST(
{ ...filterObject, method: REST_METHOD.GET },
response,
options
);
}

public mockPOST(
filter: RequestMethodFilter | string,
response: MockedResponse,
options?: Partial<MockOptions>
): RestMock {
const filterObject =
typeof filter === 'string' ? { url: filter } : filter;

return this.mockREST(
{ ...filterObject, method: REST_METHOD.POST },
response,
options
);
}

public mockPUT(
filter: RequestMethodFilter | string,
response: MockedResponse,
options?: Partial<MockOptions>
): RestMock {
const filterObject =
typeof filter === 'string' ? { url: filter } : filter;

return this.mockREST(
{ ...filterObject, method: REST_METHOD.PUT },
response,
options
);
}

public mockDELETE(
filter: RequestMethodFilter | string,
response: MockedResponse,
options?: Partial<MockOptions>
): RestMock {
const filterObject =
typeof filter === 'string' ? { url: filter } : filter;

return this.mockREST(
{ ...filterObject, method: REST_METHOD.DELETE },
response,
options
);
}

public removeMock(mock: RestMock): RestMock | void {
const index = this.mocks.indexOf(mock);
if (index > -1) {
Expand Down
14 changes: 12 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { ResourceType } from 'puppeteer';

export type QueryObject = Record<string, string | string[]>;

export interface RequestFilter {
method: string;
export interface RequestMethodFilter {
url: string;
query?: QueryObject;
}

export interface RequestFilter extends RequestMethodFilter {
method: string;
}

export interface ParsedFilterRequest {
method: string;
hostname: string | undefined;
Expand Down Expand Up @@ -44,3 +47,10 @@ export interface IMock {
origin: string
): MockedResponse | null;
}

export enum REST_METHOD {
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
DELETE = 'DELETE',
}
23 changes: 22 additions & 1 deletion test/integration/fixture/mock-fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { RequestFilter } from '../../../src/types';
import { RequestFilter, RequestMethodFilter } from '../../../src/types';

export const requestFoo: RequestMethodFilter = {
url: '/foo',
};

export const requestFooWithQuery: RequestMethodFilter = {
url: '/foo',
query: {
param: 'fooParam',
},
};

export const requestGetFoo: RequestFilter = {
method: 'GET',
Expand All @@ -18,6 +29,16 @@ export const requestPostFoo: RequestFilter = {
url: '/foo',
};

export const requestPutFoo: RequestFilter = {
method: 'PUT',
url: '/foo',
};

export const requestDeleteFoo: RequestFilter = {
method: 'DELETE',
url: '/foo',
};

export const response200Empty = {
status: 200,
body: {},
Expand Down
Loading

0 comments on commit e4c0444

Please sign in to comment.