Skip to content

Commit

Permalink
fix: fixed assert
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Ottun committed Oct 9, 2021
1 parent f3a062d commit 61470cd
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 71 deletions.
1 change: 1 addition & 0 deletions __mocks__/axios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default jest.fn();
60 changes: 8 additions & 52 deletions src/engine/assert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,51 +167,7 @@ test('at least 1 times with other records', async () => {
const verification: Verification = { request, limit: { atLeast: 1 } };
const verified = engine.assert(verification) as VerificationError;

expect(verified.message).toMatchInlineSnapshot(
`"Expected to have received GET:/todos/take-trash-out at least 1 times but was received 0 times"`,
);
});

test('assert 3 counts on a 2 limit response', async () => {
const mocks: Mock[] = [
{
id: 'exp1',
name: 'sample1',
request: {
headers: {},
path: '/happy_feet',
method: 'GET',
},
response: {
statusCode: 200,
},
limit: 2,
},
];

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

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

await engine.execute(request);
await engine.execute(request);
await engine.execute(request);
await engine.execute(request);
await engine.execute(request);

const verification: Verification = {
request,
limit: { atLeast: 3 },
};
const verified = engine.assert(verification) as VerificationError;

expect(verified.message).toMatchInlineSnapshot(
`"Expected to have received GET:/happy_feet at least 3 times but was received 2 times"`,
);
expect(verified).toMatchInlineSnapshot(`true`);
});

test('at most 3 times', async () => {
Expand Down Expand Up @@ -318,11 +274,11 @@ test('empty record matches', () => {
const verified = engine.assert(verification);

expect(verified).toMatchInlineSnapshot(`
Object {
"actual": 0,
"expected": 1,
"message": "Expected to have received GET:/todos at least 1 times but was received 0 times",
"records": Array [],
}
`);
Object {
"actual": 0,
"expected": 1,
"message": "Expected to have received GET:/todos at least 1 times but was received 0 times",
"records": Array [],
}
`);
});
14 changes: 7 additions & 7 deletions src/engine/engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,13 @@ test('match string body request', () => {
`);
});

test('Mock: add results in error', () => {
test('mock: add results in error', () => {
const engine = new Engine([], {});
const add = () => engine.addMock({ request: {} } as any);
expect(add).toThrowError('Request requires a path');
});

test('Mock: add', () => {
test('mock: add', () => {
const engine = new Engine([], {});
const add = () =>
engine.addMock({
Expand All @@ -398,7 +398,7 @@ test('Mock: add', () => {
`);
});

test('Mock: remove', () => {
test('mock: remove', () => {
const engine = new Engine([], {});
engine.addMock({
id: 'new',
Expand All @@ -410,7 +410,7 @@ test('Mock: remove', () => {
expect(engine.mocks).toMatchInlineSnapshot(`Array []`);
});

test(`Clear all records`, () => {
test(`records: clear all records`, () => {
const mocks: Mock[] = [
{
id: 'exp1',
Expand Down Expand Up @@ -443,7 +443,7 @@ test(`Clear all records`, () => {
expect(engine.mocks).toMatchInlineSnapshot(`Array []`);
});

test('update an existing mock', () => {
test('mock: update an existing mock', () => {
const mocks: Mock[] = [
{
id: 'exp1',
Expand Down Expand Up @@ -496,7 +496,7 @@ test('update an existing mock', () => {
`);
});

test('ignore a non-existing mock', () => {
test('mock: update: ignore a non-existing mock', () => {
const mocks: Mock[] = [
{
id: 'exp1',
Expand Down Expand Up @@ -567,7 +567,7 @@ test('ignore a non-existing mock', () => {
`);
});

test('re-order mocks', () => {
test('mocks re-orders', () => {
const mocks: Mock[] = [
{
id: 'mock1',
Expand Down
23 changes: 12 additions & 11 deletions src/engine/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,15 @@ export class Engine extends EventEmitter {
}

private verifyRequest(request: Request): Record[] {
return this.$records
.filter((rec) => methodMatcher(request, rec.request) === true)
.filter((rec) => rec.request.path === request.path)
.filter((rec) => headerMatcher(request, rec.request) === true)
.filter((rec) => bodyMatcher(request, rec.request) === true)
.filter((rec) => rec.matches.length > 0)
.sort((a, b) => a.timestamp - b.timestamp);
return (
this.$records
.filter((rec) => methodMatcher(request, rec.request))
.filter((rec) => rec.request.path === request.path)
.filter((rec) => headerMatcher(request, rec.request))
.filter((rec) => bodyMatcher(request, rec.request))
// .filter((rec) => rec.matches.length > 0)
.sort((a, b) => a.timestamp - b.timestamp)
);
}

private createProxy = (request: Request): Proxy => {
Expand All @@ -130,14 +132,13 @@ export class Engine extends EventEmitter {
private createProxyRequest = ({ req, proxy }: { req: Request; proxy: Proxy }): ProxyRequest => {
const port = proxy.port || (proxy.protocol === 'https' ? 443 : 80);
const url = `${proxy.protocol || 'http'}://${proxy.host}:${port}${req.path}`;
const hostHeaders = !proxy.keepHost ? { Host: proxy.host } : {};

return {
path: req.path,
url,
headers: Object.assign({}, req.headers, proxy.headers || {}, hostHeaders),
headers: Object.assign({}, req.headers, proxy.headers || {}),
method: req.method,
params: req.queryParams,
params: req.queryParams || {},
data: req.body,
};
};
Expand Down Expand Up @@ -177,7 +178,7 @@ export class Engine extends EventEmitter {
} catch (error) {
const defaultResponse = {
status: 500,
headers: { 'content-type': 'text/text' },
headers: { 'content-type': 'application/text' },
data: error.toString(),
};
const { response = defaultResponse } = error;
Expand Down
2 changes: 1 addition & 1 deletion src/engine/matchers/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { Request } from '../../types';
import stringMatcher from './string';

export default (expRequest: Request, req: Request): boolean => {
return stringMatcher(req.method, expRequest.method || 'GET');
return stringMatcher(req.method, expRequest.method || '.*');
};

0 comments on commit 61470cd

Please sign in to comment.