Skip to content

Commit

Permalink
Merge pull request #18 from AngelaE/query-string
Browse files Browse the repository at this point in the history
Add Query String to FlexiPredicate
  • Loading branch information
AngelaE authored Aug 6, 2022
2 parents 4f6cade + 223bf46 commit dd9f506
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- run: npm run unit-test
- run: npm run pack
# todo: use github packages
- run: npm i "file:../project/anev-ts-mountebank-1.5.0.tgz" --save-dev
- run: npm i "file:../project/anev-ts-mountebank-1.6.0.tgz" --save-dev
working-directory: integration-tests
- run: npm ci
working-directory: integration-tests
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mb

I recommend reading the [Mountebank documentation](http://www.mbtest.org/docs/api/overview) for a deeper understanding of their API.

For more samples on how to use this package check this blog: https://angela-evans.com/easy-api-tests-with-mountebank/
For more samples on how to use this package check this blog: https://angela-evans.com/easy-api-tests-with-mountebank/ or the integration tests.

### Create Imposter
```typescript
Expand All @@ -35,6 +35,19 @@ let imposter = new Imposter().withPort(port).withStub(
await mb.createImposter(imposter);
```

### Check for QueryString
Add a query to the stub. For usages check the tests in ./integration-tests/predicate.flexi-predicate.query.mb-tests.ts
```typescript
new Stub()
.withPredicate(
new FlexiPredicate()
.withOperator(Operator.equals)
.withPath('/testpath')
.withQuery({name: 'x', max: 5})
)
.withResponse(new DefaultResponse('found', 222))
```

### Create Debug Proxy
A simple proxy which always proxies the request and records the responses.
This is useful to
Expand Down
69 changes: 69 additions & 0 deletions integration-tests/src/predicate.flexi-predicate.body.mb-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { expect } from 'chai';
import request = require('superagent');

import {
Imposter,
Mountebank,
Stub,
FlexiPredicate,
Operator,
DefaultResponse,
} from '@anev/ts-mountebank';

const port = 12345;
const path = '/testpath';
async function getImposterResponseCode(body: any): Promise<number> {
return (await request.post(`http://localhost:${port}${path}`).send(body))
.statusCode;
}

describe('The flexi predicate works with query', () => {
// only runs on local machine for now
const mb = new Mountebank();

const tests = [
{
predicateBody: {name: 'x', max: 5},
matches: [{name: 'x', max: 5}, {name: 'x', max: 5, min: 1}],
nonMatching: [{name: 'x'}, {name: 'x', max: 6}],
},

];

tests.forEach(async (test) => {
describe(`Body works for predicate '${JSON.stringify(test.predicateBody)}'`, () => {
before(async () => {
const imposter = new Imposter()
.withPort(port)
.withStub(
new Stub()
.withPredicate(
new FlexiPredicate()
.withOperator(Operator.equals)
.withPath(path)
.withBody(test.predicateBody)
)
.withResponse(new DefaultResponse('found', 222))
);

await mb.createImposter(imposter);
});

test.matches.forEach(async (match) => {
it(`works with match '${JSON.stringify(match)}'`, async () => {
// assert
const responseCode = await getImposterResponseCode(match);
expect(responseCode).to.equal(222);
});
});

test.nonMatching.forEach(async (nonMatch) => {
it(`does not work with '${JSON.stringify(nonMatch)}'`, async () => {
// assert
const responseCode = await getImposterResponseCode(nonMatch);
expect(responseCode).to.equal(200);
});
});
});
});
});
2 changes: 1 addition & 1 deletion integration-tests/src/predicate.flexi-predicate.mb-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function getImposterResponseCode(path: string): Promise<number> {
return (await request.get(`http://localhost:${port}${path}`)).statusCode;
}

describe('The flexi predicate', () => {
describe('The flexi predicate works with path', () => {
// only runs on local machine for now
const mb = new Mountebank();

Expand Down
68 changes: 68 additions & 0 deletions integration-tests/src/predicate.flexi-predicate.query.mb-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { expect } from 'chai';
import request = require('superagent');

import {
Imposter,
Mountebank,
Stub,
FlexiPredicate,
Operator,
DefaultResponse,
} from '@anev/ts-mountebank';

const port = 12345;
const path = '/testpath';
async function getImposterResponseCode(queryString: string): Promise<number> {
return (await request.get(`http://localhost:${port}${path}?${queryString}`)).statusCode;
}

describe('The flexi predicate works with query', () => {
// only runs on local machine for now
const mb = new Mountebank();

const tests = [
{
predicateQuery: {name: 'x', max: 5},
matches: ['name=x&max=5', 'max=5&name=x', 'MAX=5&namE=X', 'name=x&max=5&another=y'],
nonMatching: ['max=5', 'name=x&max=6'],
},

];

tests.forEach(async (test) => {
describe(`Query works for predicate '${JSON.stringify(test.predicateQuery)}'`, () => {
before(async () => {
const imposter = new Imposter()
.withPort(port)
.withStub(
new Stub()
.withPredicate(
new FlexiPredicate()
.withOperator(Operator.equals)
.withPath(path)
.withQuery(test.predicateQuery)
)
.withResponse(new DefaultResponse('found', 222))
);

await mb.createImposter(imposter);
});

test.matches.forEach(async (match) => {
it(`works with match '${match}'`, async () => {
// assert
const responseCode = await getImposterResponseCode(match);
expect(responseCode).to.equal(222);
});
});

test.nonMatching.forEach(async (nonMatch) => {
it(`does not work with '${nonMatch}'`, async () => {
// assert
const responseCode = await getImposterResponseCode(nonMatch);
expect(responseCode).to.equal(200);
});
});
});
});
});
4 changes: 2 additions & 2 deletions integration-tests/src/proxy/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Proxy', () => {
it(`can create a debug proxy for mode ${test.mode}`, async () => {
const mb = new Mountebank();

const body = 'yippie';
const body = {x: 'yippie'};
const responseImposter = new Imposter()
.withPort(port)
.withStub(
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('Proxy', () => {
`http://localhost:${proxyPort}${testPath}`
);
expect(response.statusCode).to.equal(firstImposterResponseStatus);
expect(response.body).to.equal(body);
expect(response.body).to.deep.equal(body);

try {
await mb.createImposter(newImposter);
Expand Down
2 changes: 1 addition & 1 deletion project/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@anev/ts-mountebank",
"version": "1.5.0",
"version": "1.6.0",
"description": "Mountebank client for TS / node ",
"scripts": {
"release": "npm run build && npm run test && npm publish --access=public",
Expand Down
30 changes: 24 additions & 6 deletions project/src/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ export enum Operator {

export class FlexiPredicate implements Predicate {
operator: Operator = Operator.equals;
method: HttpMethod | undefined = undefined;
path: string | undefined = undefined;
private _body?: string = undefined;
method?: HttpMethod = undefined;
path?: string = undefined;
query?: any = undefined;
body?: string = undefined;

headers: Map<string, string> = new Map<string, string>();

Expand All @@ -32,21 +33,29 @@ export class FlexiPredicate implements Predicate {
this.headers.set(header, value);
return this;
}

withQuery(query: any): FlexiPredicate {
this.query = query;
return this;
}

withPath(path: string): FlexiPredicate {
this.path = path;
return this;
}

withMethod(method: HttpMethod): FlexiPredicate {
this.method = method;
return this;
}

withBearerToken(token: string): FlexiPredicate {
this.withHeader('authorization', 'bearer ' + token);
return this;
}

withBody(body: any): FlexiPredicate {
this._body = body;
this.body = body;
return this;
}
toJSON(): any {
Expand All @@ -70,8 +79,13 @@ export class FlexiPredicate implements Predicate {
if (this.path) {
res.path = this.path;
}
if (this._body !== undefined) {
res.body = this._body;

if(this.query !== undefined) {
res.query = this.query;
}

if (this.body !== undefined) {
res.body = this.body;
}
return {
[this.operator]: res,
Expand All @@ -83,6 +97,9 @@ export class EqualPredicate implements Predicate {
method: HttpMethod = HttpMethod.GET;
path = '/';
private _body?: string = undefined;
// does not have a queryString option because you can
// just use the FlexiPredicate
// The EqualPredicate is just left here for backwards compatibility

headers: Map<string, string> = new Map<string, string>();

Expand All @@ -107,6 +124,7 @@ export class EqualPredicate implements Predicate {
this._body = body;
return this;
}

toJSON(): any {
const res: any = {};

Expand Down

0 comments on commit dd9f506

Please sign in to comment.