Skip to content

Commit

Permalink
feat: mocks can be prioritised
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Ottun committed Mar 29, 2022
1 parent 2a0c95b commit 48f2986
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 105 deletions.
6 changes: 5 additions & 1 deletion docs/content/en/mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ response:

# Optional: defaults to unlimited
limit: (number|unlimited). how many times this mock should be used. defaults to unlimited

# Optional: defaults to 100
priority: (number|unlimited). matching is ordered by priority (highest first) then creation (earliest first)
```

</code-block>
Expand Down Expand Up @@ -103,7 +106,8 @@ limit: (number|unlimited). how many times this mock should be used. defaults to
},
"delay": "seconds to delay the response. defaults to 0"
},
"limit": "(number|unlimited). how many times this mock should be used. defaults to unlimited"
"limit": "(number|unlimited). how many times this mock should be used. defaults to unlimited",
"priority": "matching is ordered by priority (highest first) then creation (earliest first)"
}
```

Expand Down
178 changes: 89 additions & 89 deletions src/engine/engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,26 @@ test('match simple method request', () => {
});

expect(matched).toMatchInlineSnapshot(`
Array [
Object {
"id": "exp1",
"limit": "unlimited",
"name": "sample1",
"request": Object {
"headers": Object {},
"path": "/todos",
},
"response": Object {
"body": Array [
Object {
"id": 2,
"text": "get request",
},
],
Array [
Object {
"id": "exp1",
"limit": "unlimited",
"name": "sample1",
"request": Object {
"headers": Object {},
"path": "/todos",
},
"response": Object {
"body": Array [
Object {
"id": 2,
"text": "get request",
},
},
]
`);
],
},
},
]
`);
});

test('match regex method request', () => {
Expand Down Expand Up @@ -220,29 +220,29 @@ test('match headers request', () => {
});

expect(matched).toMatchInlineSnapshot(`
Array [
Object {
"id": "exp1",
"limit": "unlimited",
"name": "sample1",
"request": Object {
"headers": Object {
"host": "example.com",
},
"method": "GET",
"path": "/todos",
},
"response": Object {
"body": Array [
Object {
"id": 2,
"text": "get request",
},
],
},
Array [
Object {
"id": "exp1",
"limit": "unlimited",
"name": "sample1",
"request": Object {
"headers": Object {
"host": "example.com",
},
]
`);
"method": "GET",
"path": "/todos",
},
"response": Object {
"body": Array [
Object {
"id": 2,
"text": "get request",
},
],
},
},
]
`);
});

test('match json body request', () => {
Expand Down Expand Up @@ -287,26 +287,26 @@ test('match json body request', () => {
});

expect(matched).toMatchInlineSnapshot(`
Array [
Object {
"id": "exp2",
"limit": "unlimited",
"name": "sample2",
"request": Object {
"body": Object {
"id": 3,
"text": "new post",
},
"headers": Object {},
"method": "POST",
"path": "/todos",
},
"response": Object {
"body": Array [],
},
Array [
Object {
"id": "exp2",
"limit": "unlimited",
"name": "sample2",
"request": Object {
"body": Object {
"id": 3,
"text": "new post",
},
]
`);
"headers": Object {},
"method": "POST",
"path": "/todos",
},
"response": Object {
"body": Array [],
},
},
]
`);
});

test('match string body request', () => {
Expand Down Expand Up @@ -348,23 +348,23 @@ test('match string body request', () => {
});

expect(matched).toMatchInlineSnapshot(`
Array [
Object {
"id": "exp2",
"limit": "unlimited",
"name": "sample2",
"request": Object {
"body": "[0-9]th todo",
"headers": Object {},
"method": "POST",
"path": "/todos",
},
"response": Object {
"body": Array [],
},
},
]
`);
Array [
Object {
"id": "exp2",
"limit": "unlimited",
"name": "sample2",
"request": Object {
"body": "[0-9]th todo",
"headers": Object {},
"method": "POST",
"path": "/todos",
},
"response": Object {
"body": Array [],
},
},
]
`);
});

test('mock: add results in error', () => {
Expand All @@ -384,18 +384,18 @@ test('mock: add', () => {
});
expect(add).not.toThrow();
expect(engine.mocks).toMatchInlineSnapshot(`
Array [
Object {
"id": "new",
"limit": "unlimited",
"name": "base expectation",
"request": Object {
"path": "/hello",
},
"response": Object {},
},
]
`);
Array [
Object {
"id": "new",
"limit": "unlimited",
"name": "base expectation",
"request": Object {
"path": "/hello",
},
"response": Object {},
},
]
`);
});

test('mock: remove', () => {
Expand Down
9 changes: 7 additions & 2 deletions src/engine/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ export class Engine extends EventEmitter {
return !!interfaces;
};

private baseMock() {
private baseMock(): Mock {
return {
id: shortId(),
name: 'Mock',
limit: 'unlimited',
request: { path: '/hello' },
};
}

Expand Down Expand Up @@ -160,7 +161,11 @@ export class Engine extends EventEmitter {
return pastMatches < mock.limit;
});

return matches;
return matches.sort((m1, m2) => {
const pr1 = m1.priority || 0;
const pr2 = m2.priority || 0;
return pr2 - pr1;
});
}

async proxy(req: ProxyRequest): Promise<Response> {
Expand Down
71 changes: 71 additions & 0 deletions src/engine/priority.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { create } from '../engine';
import { Mock, Request } from '../types';

test('priority', async () => {
const mocks: Mock[] = [
{
id: 'exp1',
name: 'sample1',
request: {
path: '/todos',
},
response: {
body: 'No Priority set',
},
limit: 2,
},
{
id: 'exp2',
name: 'sample2',
request: {
path: '/todos',
},
response: {
body: 'Mock priority -1',
},
limit: 2,
priority: -1,
},
{
id: 'exp2',
name: 'sample2',
request: {
path: '/todos',
},
response: {
body: 'Mock priority 0',
},
limit: 2,
priority: 0,
},
{
id: 'exp2',
name: 'sample2',
request: {
path: '/todos',
},
response: {
body: 'Mock priority 1',
},
limit: 2,
priority: 1,
},
];

const request: Request = {
path: '/todos',
method: 'GET',
headers: {},
};

const engine = create({ mocks, config: {} });

await engine.execute(request);
const lastMatch = await engine.execute(request);

expect(lastMatch.response).toMatchInlineSnapshot(`
Object {
"body": "Mock priority 1",
}
`);
});
26 changes: 13 additions & 13 deletions src/server/routes/mocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,17 @@ test('retrieve all mocks', async () => {

expect(res.status).toBe(200);
expect(res.body).toMatchInlineSnapshot(`
Array [
Object {
"id": "sample-mock",
"limit": "unlimited",
"name": "test mocks",
"request": Object {
"method": "GET",
"path": "/tasks",
},
"response": null,
},
]
`);
Array [
Object {
"id": "sample-mock",
"limit": "unlimited",
"name": "test mocks",
"request": Object {
"method": "GET",
"path": "/tasks",
},
"response": null,
},
]
`);
});

0 comments on commit 48f2986

Please sign in to comment.