Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Commit

Permalink
fix: correct handling of parameters in URI templates
Browse files Browse the repository at this point in the history
* Resolve #165 - incorrect handling of parameters in URL
* Added sorting of resourceURLs for better matching
  • Loading branch information
snakeye authored and JackuB committed Jul 23, 2019
1 parent 7bc8619 commit ded3fb4
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ apiDescription(_); // Extend lodash

const mock = (refract, options) => {
return new Promise((resolve, reject) => {
let mocks = [];
let routes = {};

const resourceGroups = _.filter(refract.content, {
'element': 'category'
Expand Down Expand Up @@ -77,14 +77,30 @@ const mock = (refract, options) => {
responseBody,
responseHeaders
};
mocks.push(mockRoute(nockOptions));

if(!routes.hasOwnProperty(resourceUrl)) {
routes[resourceUrl] = [];
}

routes[resourceUrl].push(nockOptions);
});
});
});
});
});
});

// sort URLs by length
const urls = Object.keys(routes);
urls.sort((a, b) => b.length - a.length);

// collect mocks
const mocks = [];
urls.forEach(url => {
const resources = routes[url];
resources.forEach(resource => mocks.push(mockRoute(resource)))
});

const restore = () => {
mocks.forEach((mock) => {
mock.interceptors.forEach((interceptor) => nock.removeInterceptor(interceptor));
Expand Down
37 changes: 37 additions & 0 deletions test/fixtures/apib-url-parameters.apib
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FORMAT: 1A
HOST: http://example.com

# API blueprint

# Data Structures

## User (object)
+ id: `1` (number)
+ username: `example` (string)

## Post (object)
+ id: `1` (number)
+ user_id: `1` (number)
+ text: `Post text` (string)

# Group User

## User details [/users/{user}/]

### Get user details [GET]

+ Parameters
+ user: `example` (string)

+ Response 200 (application/json)
+ Attributes (User)

## User posts [/users/{user}/posts/]

### Get user's posts [GET]

+ Parameters
+ user: `example` (string)

+ Response 200 (application/json)
+ Attributes (array[Post])
32 changes: 32 additions & 0 deletions test/mock-apib-url-parameters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from 'fs';
import hippie from 'hippie';
import apish from '../dist/apish.js';

describe('Mock APIB with parameters in URL', () => {
let mockResult = {};
before(() => {
const apib = fs.readFileSync(__dirname + '/fixtures/apib-url-parameters.apib');
return mockResult = apish(apib.toString());
});

after(() => {
mockResult.value().restore();
});

it('should mock the GET /users/example/posts/ request including body', (done) => {
hippie()
.json()
.base('http://example.com')
.get('/users/example/posts/')
.expectStatus(200)
.expectHeader('Content-Type', 'application/json')
.expectBody([
{
id: 1,
user_id: 1,
text: 'Post text'
}
])
.end(done);
});
});

0 comments on commit ded3fb4

Please sign in to comment.