Skip to content

Commit

Permalink
create test and exmaple with colon in path
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Aug 23, 2020
1 parent e87e0a9 commit 9e8e14f
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 15 deletions.
30 changes: 28 additions & 2 deletions examples/3-eov-operations/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ paths:
description: |
ping then pong!
# operationId is used in /ping path (x-eov-operation-id is optional). x-eov-operation-id can be used in place of operationId. If both x-eov-operation-id and operationId are present, x-eov-operation-id will override operationId.
operationId: ping
x-eov-operation-handler: routes/ping
operationId: ping
x-eov-operation-handler: routes/ping
responses:
'200':
description: OK
Expand Down Expand Up @@ -199,6 +199,32 @@ paths:
success:
type: boolean

"/pets:lookup":
get:
description: |
Returns all pets
x-eov-operation-id: pets#list
x-eov-operation-handler: routes/pets
parameters:
- name: type
in: query
description: maximum number of results to return
required: true
schema:
type: string
enum:
- dog
- cat
responses:
'200':
description: pet response
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'

components:
schemas:
Pet:
Expand Down
13 changes: 5 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import * as _uniq from 'lodash.uniq';
import * as middlewares from './middlewares';
import { Application, Response, NextFunction, Router } from 'express';
import { OpenApiContext } from './framework/openapi.context';
import {
OpenApiSpecLoader,
Spec,
RouteMetadata,
} from './framework/openapi.spec.loader';
import { OpenApiSpecLoader, Spec } from './framework/openapi.spec.loader';
import {
OpenApiValidatorOpts,
ValidateRequestOpts,
Expand All @@ -18,8 +14,6 @@ import {
ValidateSecurityOpts,
} from './framework/types';
import { deprecationWarning } from './middlewares/util';
import * as path from 'path';
import { BasePath } from './framework/base.path';
import { defaultResolver } from './resolvers';
import { OperationHandlerOptions } from './framework/types';

Expand All @@ -34,7 +28,10 @@ export {
Forbidden,
} from './framework/types';

export * as resolvers from './resolvers';
import * as solvers from './resolvers';
export const resolvers = {
...solvers,
};

export class OpenApiValidator {
private readonly options: OpenApiValidatorOpts;
Expand Down
6 changes: 2 additions & 4 deletions test/path.order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe(packageJson.name, () => {
before(async () => {
// Set up the express app
const apiSpec = path.join('test', 'resources', 'path.order.yaml');
app = await createApp({ apiSpec }, 3005, app =>
app = await createApp({ apiSpec }, 3005, (app) =>
app.use(
`${app.basePath}`,
express
Expand All @@ -28,9 +28,7 @@ describe(packageJson.name, () => {
});

it('should match on users test', async () =>
request(app)
.get(`${app.basePath}/users/test`)
.expect(200));
request(app).get(`${app.basePath}/users/test`).expect(200));

it('static routes should be matched before dynamic routes', async () =>
request(app)
Expand Down
19 changes: 19 additions & 0 deletions test/path.params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ describe('path params', () => {
id: req.params.id,
});
});
app.get(`${app.basePath}/users:lookup`, (req, res) => {
res.json([
{
id: req.query.name,
},
]);
});
app.get(`${app.basePath}/multi_users/:ids?`, (req, res) => {
res.json({
ids: req.params.ids,
});
});
app.use((err, req, res, next) => {
console.error(err)
res.status(err.status ?? 500).json({
message: err.message,
code: err.status ?? 500,
Expand All @@ -36,6 +44,7 @@ describe('path params', () => {
},
false,
);
return app
});

after(() => {
Expand All @@ -57,4 +66,14 @@ describe('path params', () => {
.then((r) => {
expect(r.body.ids).to.deep.equal(['aa', 'bb', 'cc']);
}));

it("should handle :'s in path parameters", async () =>
request(app)
.get(`${app.basePath}/users:lookup`)
.query({ name: 'carmine' })
.expect(200)
.then((r) => {
expect(r.body).to.be.an('array');
expect(r.body[0].id).to.equal('carmine');
}));
});
17 changes: 17 additions & 0 deletions test/resources/path.params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ paths:
application/json:
schema:
type: object
"/users:lookup":
get:
parameters:
- name: name
in: query
required: true
schema:
type: string
responses:
200:
description: ""
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/User"
components:
schemas:
User:
Expand Down
2 changes: 1 addition & 1 deletion test/sync.install.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe(packageJson.name, () => {
});

after(() => {
setTimeout(() => process.exit(), 100);
setTimeout(() => process.exit(), 1000);
});

it('should validate /v1/pets and return 400', async () =>
Expand Down

0 comments on commit 9e8e14f

Please sign in to comment.