Skip to content

Commit

Permalink
fix: SL-2192 stringify examples (#205)
Browse files Browse the repository at this point in the history
* fix: SL-2191 repaired mock config switch

* chore: SL-2191 separated config from components

* fix: SL-2191 fixed cli tests

* fix: SL-2191 fixed tests

* fix: SL-2191 config as a separate parameter for prism

* fix: SL-2191 fixed tests

* fix: SL-2191 removed unneeded merge of defaultConfig

* fix: SL-2192 forced examples to be stringified

* fix: SL-2192 specific static examples fixture

* fix: SL-2192 trimmed static example
  • Loading branch information
karol-maciaszek authored Apr 2, 2019
1 parent 7621f8a commit bbf6492
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
32 changes: 32 additions & 0 deletions packages/http/src/__tests__/fixtures/static-examples.oas2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"swagger": "2.0",
"paths": {
"/todos": {
"get": {
"produces": [
"application/json"
],
"responses": {
"200": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"completed": {"type": ["boolean", "null"]}
},
"required": ["name", "completed"]
}
},
"examples": {
"application/json": [
{"id": 1, "name": "design the thingz", "completed": true}
]
}
}
}
}
}
}
}
17 changes: 17 additions & 0 deletions packages/http/src/__tests__/http-prism-instance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,21 @@ describe('Http Prism Instance function tests', () => {

expect(prism.resources).toHaveLength(5);
});

test('returns stringified static example when one defined in spec', async () => {
prism = createInstance();
await prism.load({
path: relative(process.cwd(), resolve(__dirname, 'fixtures', 'static-examples.oas2.json')),
});

const response = await prism.process({
method: 'get',
url: {
path: '/todos',
},
});

expect(response.output).toBeDefined();
expect(typeof response.output!.body).toBe('string');
});
});
2 changes: 1 addition & 1 deletion packages/http/src/mocker/HttpMocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Here is the original validation result instead: ${JSON.stringify(input.validatio
const example = negotiationResult.example;

if (example && 'value' in example && example.value !== undefined) {
body = example.value;
body = typeof example.value === 'string' ? example.value : JSON.stringify(example.value);
} else if (negotiationResult.schema) {
body = await this._exampleGenerator.generate(
negotiationResult.schema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

exports[`http mocker request is invalid returns 400 and static error response 1`] = `
Object {
"body": Array [
Object {
"message": "error",
},
],
"body": "[{\\"message\\":\\"error\\"}]",
"headers": Object {
"Content-type": "application/json",
},
Expand All @@ -16,13 +12,7 @@ Object {

exports[`http mocker request is valid HttpOperation contains example return lowest 2xx code and match response example to media type accepted by request 1`] = `
Object {
"body": Array [
Object {
"completed": true,
"id": 1,
"name": "make prism",
},
],
"body": "[{\\"id\\":1,\\"completed\\":true,\\"name\\":\\"make prism\\"}]",
"headers": Object {
"Content-type": "application/json",
},
Expand Down Expand Up @@ -54,13 +44,7 @@ Object {

exports[`http mocker request is valid given enforced example key should return application/json, 200 response 1`] = `
Object {
"body": Array [
Object {
"completed": false,
"id": 2,
"name": "make bears",
},
],
"body": "[{\\"id\\":2,\\"completed\\":false,\\"name\\":\\"make bears\\"}]",
"headers": Object {
"Content-type": "application/json",
},
Expand Down

0 comments on commit bbf6492

Please sign in to comment.