Skip to content

Commit

Permalink
doc: document regex and function
Browse files Browse the repository at this point in the history
  • Loading branch information
ybonnefond committed Sep 21, 2019
1 parent 104c466 commit c8ba52f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 23 deletions.
77 changes: 57 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ If a query parameter or a header does not match the route definition, stubborn w
it('should respond 501 if a parameter does not match the definition', async () => {
sb.get('/').setQueryParameters({ page: '1' });

const res = await request(`${sb.getOrigin()}?page=2`, { throwHttpErrors: false });
const res = await request(`${sb.getOrigin()}?page=2`, {
throwHttpErrors: false,
});

expect(res.statusCode).toEqual(501);
});
Expand All @@ -110,7 +112,7 @@ it('should respond 501 if a parameter does not match the definition', async () =
You can use `null` as wildcard

```typescript
it('should respond using wildcard', async () => {
it('should match using wildcard', async () => {
sb.get('/')
.setQueryParameters({ page: null })
.setHeaders(null);
Expand All @@ -124,37 +126,72 @@ it('should respond using wildcard', async () => {
});
```

You can use regex to match a parameter, header or body

```typescript
it('should match using a regexp', async () => {
sb.post('/', {
slug: /^[a-z\-]*$/,
});

const res = await request(`${sb.getOrigin()}?page=2`, {
method: 'POST',
body: { slug: 'stubborn-ws' },
});

expect(res.statusCode).toEqual(200);
});
```

You can use a function to match a parameter, header or body

```typescript
it('should match using a function', async () => {
sb.get('/').setQueryParameters({
page: value => parseInt(value as string) > 0,
});

const res = await request('/?page=2');

expect(res).toReplyWith(STATUS_CODES.SUCCESS);
});
```

## Public API

See the [API documentation](https://ybonnefond.github.io/stubborn/)

## FAQ

#### Q: Stubborn is not matching my route definition and always return a 501

Stubborn is STUBBORN, therefore it will return a 501 if it does not exactly match the route definition you have set up.
To help you find what missing in the route definition, you can compare it to the response body returned when receiving a 501:

```typescript
const route = sb.get('/')
// This header definition will miss additional header added by got, like user-agent, connexion, etc...
.setHeaders({ 'X-Api-Key': 'test' });

const res = await request(sb.getOrigin(), {
headers: { 'x-api-key': 'api key' }
});

expect(res.statusCode).toBe(501);
const route = sb
.get('/')
// This header definition will miss additional header added by got, like user-agent, connexion, etc...
.setHeaders({ 'X-Api-Key': 'test' });

const res = await request(sb.getOrigin(), {
headers: { 'x-api-key': 'api key' },
});

expect(res.statusCode).toBe(501);

const def = route.getDefinition();
// Definition used by stubborn to match the request against
console.log('--- DEFINTION ---\n', def);
// Actual request received
console.log('--- REQUEST ---\n', res.body);
// Spot the differences or use a diff tool to find them ;)
const def = route.getDefinition();

// Definition used by stubborn to match the request against
console.log('--- DEFINTION ---\n', def);
// Actual request received
console.log('--- REQUEST ---\n', res.body);

// Spot the differences or use a diff tool to find them ;)
```

#### Q: How do I know if stubborn has been called and matched the route defined?

Stubborn will return a 501 (Not Implemented) if it received a request but cannot match any route.
If the request matches the route it will respond according to the route response configuration and update the `call` property of the route

Expand All @@ -165,7 +202,7 @@ If the request matches the route it will respond according to the route response

// No route setup in Stubborn
const res = await call();

expect(res.statusCode).toBe(501);
expect(res.body).toEqual({
method: 'GET'
Expand Down
31 changes: 28 additions & 3 deletions test/specs/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ describe('index', () => {
});
});

describe('Doc handle basic usage', () => {
describe('README examples', () => {
it('should respond to query', async () => {
const body = { some: 'body' };
sb.get('/').setResponseBody({ some: 'body' });
Expand Down Expand Up @@ -643,7 +643,7 @@ describe('index', () => {
).toReplyWith(STATUS_CODES.NOT_IMPLEMENTED);
});

it('should respond 501 if a parameter is added', async () => {
it('should respond 501 if a parameter is not equal', async () => {
sb.get('/').setQueryParameters({ page: '1' });

expect(
Expand All @@ -653,7 +653,7 @@ describe('index', () => {
).toReplyWith(STATUS_CODES.NOT_IMPLEMENTED);
});

it('should respond using wildcard for paramter and headers', async () => {
it('should respond using wildcard for parameter and headers', async () => {
sb.get('/')
.setQueryParameters({ page: null })
.setHeaders(null);
Expand All @@ -665,6 +665,31 @@ describe('index', () => {
}),
).toReplyWith(STATUS_CODES.SUCCESS);
});

it('should match using regex', async () => {
sb.post('/', {
slug: /^[a-z\-]*$/,
})
.setQueryParameters({ page: /^\d$/ })
.setHeaders(null);

const res = await request('/?page=2', {
method: 'POST',
body: { slug: 'stubborn-ws' },
});

expect(res).toReplyWith(STATUS_CODES.SUCCESS);
});

it('should match using a function', async () => {
sb.get('/').setQueryParameters({
page: value => parseInt(value as string) > 0,
});

const res = await request('/?page=2');

expect(res).toReplyWith(STATUS_CODES.SUCCESS);
});
});

describe('Retain Calls', () => {
Expand Down

0 comments on commit c8ba52f

Please sign in to comment.