Skip to content

Commit

Permalink
Issue 11: updated readme and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dtobe committed Jun 19, 2020
1 parent b113cf5 commit 4333d81
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ yarn.lock
test/

# Sample project
samples/
example/
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Express.js-based mock server for RESTful APIs inspired by Wiremock.
- JSON body
- Set up lists of responses
- Set a request delay (e.g. to simulate lag)
- Query number of calls for assertions

## Installation
To run aietes-js [Node.js](https://nodejs.org/en/download/) v10 or newer is required.
Expand Down Expand Up @@ -172,8 +173,32 @@ Resetting the delay:
mockServer.setDelayMs(0, "/endpoint1", "get");
```

#### Assertions
To facilitate assertions the Aietes server instance offers an API to query the number of calls to the combination of endpoint and HTTP method.
In its simplest form the query takes two string arguments:
```javascript
mockServer.timesCalled('/endpoint1/', 'get')
```
* Note: Even though NodeJs routes are by default not case-sensitive and ignore the trailing '/', this is *not* true of the Aietes server.
i.E. `mockServer.timesCalled('/endpoint1/', 'get')` != `mockServer.timesCalled('/endpoint1', 'get')`
and `mockServer.timesCalled('/ENDpoint1/', 'get')` != `mockServer.timesCalled('/endPOINT1/', 'get')`

The first argument may also be a single-argument predicate to filter by:
```javascript
mockServer.timesCalled(path => { return path.startsWith('/endpoint'); }, 'get')
```
The second argument may also be a list of HTTP methods:
```javascript
mockServer.timesCalled('/endpoint1/', ['get', 'post'])
```
The clear the number of calls:
```javascript
mockServer.clearStats()
```
* Note: stats are also cleared when the response config is reset. They are left unchanged on update.

## Examples
A complete example project including the above can be found in the `samples` directory of the project.
A complete example project including the above can be found in the `/example` directory of the project.

## Contact
[Issues, bugs and feature requests](https://github.com/dtobe/aietes-js/projects/1)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('Sample IT for the bitcoin service', () => {

afterEach(() => {
server.close();
externalServiceMock.clearStats();
});

afterAll(() => {
Expand All @@ -43,6 +44,7 @@ describe('Sample IT for the bitcoin service', () => {
expect(res.status).toBe(200);
expect(responseMarkup).toBeTruthy();
expect(responseMarkup).toContain('{"btcPrice":{"up":"5148.82"}}');
expect(externalServiceMock.timesCalled('/api/currentprice', 'get')).toBe(1);
});

it('should return 200 and display DOWN if Bitcoin price is below $5000', async() => {
Expand All @@ -54,5 +56,6 @@ describe('Sample IT for the bitcoin service', () => {
expect(res.status).toBe(200);
expect(responseMarkup).toBeTruthy();
expect(responseMarkup).toContain('{"btcPrice":{"down":"4148.82"}}');
expect(externalServiceMock.timesCalled('/api/currentprice', 'get')).toBe(1);
});
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions test/verify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ describe('Aietes Server verify call counts IT', () => {
expect(mockServer.timesCalled('/endpoint2/', 'get')).toBe(1);
});

it('should not reset stats when merely updating response config', async() => {
await request(mockServer.server).get('/endpoint1/');

mockServer.update({
'/endpoint1/': {
get: {
status: 500
}
}
});

await request(mockServer.server).get('/endpoint1/');
expect(mockServer.timesCalled('/endpoint1/', 'get')).toBe(2);
});

it('should reset stats but not response config when calling clearStats', async() => {
await request(mockServer.server).get('/endpoint1/');

Expand Down

0 comments on commit 4333d81

Please sign in to comment.