Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7a424bb

Browse files
XVincentXlag-of-death
andauthoredSep 27, 2019
feat: contract testing: add missing output checks (stoplightio#650)
* feat: add range status code checks * feat: add media type check * test: update * fix: no error if contet is not defined * test: add media type mismatch tests * feat: log validation errors * docs: add * docs * Apply suggestions from code review Co-Authored-By: Phil Sturgeon <phil@stoplight.io> * docs: it's pull not pulls * Apply suggestions from code review Co-Authored-By: lag-of-death <mateuszwit21@gmail.com> * refactor: prefer inRange * fix: handle info and hint * Apply suggestions from code review Co-Authored-By: lag-of-death <mateuszwit21@gmail.com>
1 parent 6407716 commit 7a424bb

File tree

9 files changed

+304
-123
lines changed

9 files changed

+304
-123
lines changed
 

‎CHANGELOG.md

+18-17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1010

1111
- Support for encoding > allowReserved flag when validating application/x-www-form-urlencoded body [#630](https://github.com/stoplightio/prism/pull/630)
1212
- Validating output status code against available response specs [#648](https://github.com/stoplightio/prism/pull/648)
13+
- Support for Contract Testing [#650](https://github.com/stoplightio/prism/pull/650)
1314

1415
## Fixed
1516

@@ -19,42 +20,42 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1920

2021
## Fixed
2122

22-
- Prism is now giving precedence to `application/json` instead of using it as a "fallback" serializer, fixing some conditions where it wouldn't get triggered correctly. [#604](https://github.com/stoplightio/prism/pulls/604)
23-
- Prism is now taking in consideration the `required` properties for combined schemas (`oneOf, allOf`). This is coming through an update to the Json Schema Faker Library [#623](https://github.com/stoplightio/prism/pulls/623)
24-
- Prism will never have enough information to return a `403` status code; all these occurences have been now replaced with a `401` status code which is more appropriate [#625](https://github.com/stoplightio/prism/pulls/625)
25-
- Prism is now negotiating the error response dynamically based on the validation result (security or schema validation) instead of always returning a static order of responses [#628](https://github.com/stoplightio/prism/pulls/628)
23+
- Prism is now giving precedence to `application/json` instead of using it as a "fallback" serializer, fixing some conditions where it wouldn't get triggered correctly. [#604](https://github.com/stoplightio/prism/pull/604)
24+
- Prism is now taking in consideration the `required` properties for combined schemas (`oneOf, allOf`). This is coming through an update to the Json Schema Faker Library [#623](https://github.com/stoplightio/prism/pull/623)
25+
- Prism will never have enough information to return a `403` status code; all these occurences have been now replaced with a `401` status code which is more appropriate [#625](https://github.com/stoplightio/prism/pull/625)
26+
- Prism is now negotiating the error response dynamically based on the validation result (security or schema validation) instead of always returning a static order of responses [#628](https://github.com/stoplightio/prism/pull/628)
2627
- Prism is now selecting proper serializer when Accept header contains content type which is missing in spec. This is a result of simplifying serializer selection approach. [#620](https://github.com/stoplightio/prism/pull/620)
2728
- HEAD requests no longer fail with 406 Not Acceptable [#603](https://github.com/stoplightio/prism/pull/603)
2829

2930
# 3.1.0 (2019-09-03)
3031

3132
## Added
3233

33-
- Prism is now able to validate the security specification of the loaded document [#484](https://github.com/stoplightio/prism/pulls/484)
34+
- Prism is now able to validate the security specification of the loaded document [#484](https://github.com/stoplightio/prism/pull/484)
3435

3536
## Fixed
3637

37-
- Prism is not crashing anymore when referencing the same model multiple times in the specification document [#552](https://github.com/stoplightio/prism/pulls/552)
38-
- Prism will now correctly use the `example` keyword for a Schema Object in OpenAPI 3.0 documents [#560](https://github.com/stoplightio/prism/pulls/560)
39-
- Prism won't return 406 when users request a `text/plain` response whose content is a primitive (string, number) [#560](https://github.com/stoplightio/prism/pulls/560)
40-
- Prism's router is now able to correctly handle a path ending with a parameter, such as `/test.{format}`, while it would previously not match with anything. [#561](https://github.com/stoplightio/prism/pulls/561)
41-
- Prism is correctly handling the `allowEmptyValue` property in OAS2 documents [#569](https://github.com/stoplightio/prism/pulls/569)
42-
- Prism is correctly handling the `csv` collection format argument property in OAS2 documents [#577](https://github.com/stoplightio/prism/pulls/577)
43-
- Prism is correctly returning the response when the request has `*/*` as Accept header [#578](https://github.com/stoplightio/prism/pulls/578)
44-
- Prism is correctly returning a single root node with the payload for XML data [#578](https://github.com/stoplightio/prism/pulls/578)
38+
- Prism is not crashing anymore when referencing the same model multiple times in the specification document [#552](https://github.com/stoplightio/prism/pull/552)
39+
- Prism will now correctly use the `example` keyword for a Schema Object in OpenAPI 3.0 documents [#560](https://github.com/stoplightio/prism/pull/560)
40+
- Prism won't return 406 when users request a `text/plain` response whose content is a primitive (string, number) [#560](https://github.com/stoplightio/prism/pull/560)
41+
- Prism's router is now able to correctly handle a path ending with a parameter, such as `/test.{format}`, while it would previously not match with anything. [#561](https://github.com/stoplightio/prism/pull/561)
42+
- Prism is correctly handling the `allowEmptyValue` property in OAS2 documents [#569](https://github.com/stoplightio/prism/pull/569)
43+
- Prism is correctly handling the `csv` collection format argument property in OAS2 documents [#577](https://github.com/stoplightio/prism/pull/577)
44+
- Prism is correctly returning the response when the request has `*/*` as Accept header [#578](https://github.com/stoplightio/prism/pull/578)
45+
- Prism is correctly returning a single root node with the payload for XML data [#578](https://github.com/stoplightio/prism/pull/578)
4546
- Prism is correctly returning payload-less responses #606
4647

4748
# 3.0.4 (2019-08-20)
4849

4950
## Added
5051

51-
- Prism is now returning CORS headers by default and responding to all the preflights requests. You can disable this behaviour by running Prism with the `--cors` flag set to false [#525](https://github.com/stoplightio/prism/pulls/525)
52+
- Prism is now returning CORS headers by default and responding to all the preflights requests. You can disable this behaviour by running Prism with the `--cors` flag set to false [#525](https://github.com/stoplightio/prism/pull/525)
5253

5354
## Fixed
5455

55-
- Prism now respects the `nullable` value for OpenAPI 3.x documents when generating examples [#506](https://github.com/stoplightio/prism/pulls/506)
56-
- Prism now loads correctly OpenAPI 3.x documents with `encodings` with non specified `style` property [#507](https://github.com/stoplightio/prism/pulls/507)
57-
- Prism got rid of some big internal dependencies that now aren't required anymore, making it faster and lighter. [#490](https://github.com/stoplightio/prism/pulls/490)
56+
- Prism now respects the `nullable` value for OpenAPI 3.x documents when generating examples [#506](https://github.com/stoplightio/prism/pull/506)
57+
- Prism now loads correctly OpenAPI 3.x documents with `encodings` with non specified `style` property [#507](https://github.com/stoplightio/prism/pull/507)
58+
- Prism got rid of some big internal dependencies that now aren't required anymore, making it faster and lighter. [#490](https://github.com/stoplightio/prism/pull/490)
5859
- Prism now correctly validates OAS2 `application/x-www-urlencoded` (form data) params (#483)
5960

6061
# 3.0.3 (2019-07-25)

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ After [installation](https://stoplight.io/p/docs/gh/stoplightio/prism/docs/getti
4242
**Cannot access mock server when using docker?**
4343

4444
Prism uses localhost by default, which usually means 127.0.0.1. When using docker the mock server will
45-
be unreachable outside of the container unless you run the mock command with `-h 0.0.0.0`.
45+
be unreachable outside of the container unless you run the mock command with `-h 0.0.0.0`.
4646

4747
**Why am I getting 404 errors when I include my basePath?**
4848

‎docs/guides/client.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Prism Client
2+
3+
Prism includes a fully-featured HTTP Client that you can use to seamlessly perform requests to both a real server and a mocked document. The client is modeled after Axios so it may feel familiar.
4+
5+
### Create from a filename or http resource
6+
7+
```ts
8+
const client = await createClientFromResource('examples/petstore.oas2.yaml', {
9+
mock: true,
10+
validateRequest: true,
11+
validateResponse: true,
12+
});
13+
```
14+
15+
### Create from OpenAPI string
16+
17+
```ts
18+
const descriptionDoc = `
19+
openapi: 3.0.2
20+
paths:
21+
/hello:
22+
get:
23+
responses:
24+
200:
25+
description: hello
26+
`;
27+
28+
const client = await createClientFromString(descriptionDoc, {
29+
mock: true,
30+
validateRequest: true,
31+
validateResponse: true,
32+
});
33+
```
34+
35+
### Create From Manual Http Operations
36+
37+
```ts
38+
const client = createClientFromOperations(
39+
[
40+
{
41+
method: 'get',
42+
path: '/hello',
43+
id: 'n1',
44+
responses: [{ code: '200' }],
45+
},
46+
],
47+
{ mock: true, validateRequest: true, validateResponse: true }
48+
);
49+
```
50+
51+
---
52+
53+
Once you've got a client instance:
54+
55+
1. You can perform the request using the generic method:
56+
57+
```ts
58+
client.request('https://google.it', { method: 'get' }).then(response => console.log(response));
59+
```
60+
61+
The response object has all the information you need, including the used configuration object.
62+
63+
2. You can override the configuration object on the request level if you prefer
64+
65+
```ts
66+
client
67+
.request('https://google.it', { method: 'get' }, { validateResponse: false })
68+
.then(response => console.log(response));
69+
```
70+
71+
This disables response validation _only for the current request_
72+
73+
3. You can do the same thing using the shortcut methods
74+
75+
```ts
76+
client.get('https://google.it', { mock: false }).then(response => console.log(response));
77+
```
78+
79+
For the shortcut methods (since the only mandatory option is intrinsic in the function name) the option parameter can be omitted
80+
81+
```ts
82+
client.get('https://google.it', { validateRequest: false }).then(response => console.log(response));
83+
```

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"tslint -p packages/tsconfig.test.json --fix",
6969
"git add"
7070
],
71-
"*.json, *.md": [
71+
"*.{json,md}": [
7272
"prettier --write",
7373
"git add"
7474
]

‎packages/http-server/src/server.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createLogger } from '@stoplight/prism-core';
22
import { createInstance, IHttpConfig, IHttpMethod, PrismHttpInstance, ProblemJsonError } from '@stoplight/prism-http';
3-
import { IHttpOperation } from '@stoplight/types';
3+
import { DiagnosticSeverity, IHttpOperation } from '@stoplight/types';
44
import * as fastify from 'fastify';
55
import * as fastifyCors from 'fastify-cors';
66
import { IncomingMessage, ServerResponse } from 'http';
@@ -108,6 +108,16 @@ export const createServer = (operations: IHttpOperation[], opts: IPrismHttpServe
108108
reply.headers(output.headers);
109109
}
110110

111+
response.validations.output.forEach(validation => {
112+
if (validation.severity === DiagnosticSeverity.Error) {
113+
request.log.error(validation.message);
114+
} else if (validation.severity === DiagnosticSeverity.Warning) {
115+
request.log.warn(validation.message);
116+
} else {
117+
request.log.info(validation.message);
118+
}
119+
});
120+
111121
reply.serializer((payload: unknown) => serialize(payload, reply.getHeader('content-type'))).send(output.body);
112122
} else {
113123
throw new Error('Unable to find any decent response for the current request.');

‎packages/http/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ In essence it's an HTTP client (similar to axios - in fact, we use axios to make
1010

1111
The goal of this document is to provide you with some basic code examples to get you started and to cover some of the advanced scenarios.
1212

13+
## Important
14+
15+
If you're a regular user and not a PRO, you might to want to use the [User facing client](../../docs/guides/client.md) which provides a better an higher lever API
16+
1317
# Table of Contents
1418

1519
- [Installation](#installation)

‎packages/http/src/validator/__tests__/__snapshots__/functional.spec.ts.snap

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Array [
3333

3434
exports[`HttpValidator validateOutput() all validations are turned on returns validation errors for whole request structure 1`] = `
3535
Array [
36+
Object {
37+
"message": "The received media type does not match the one specified in the document",
38+
"severity": 0,
39+
},
3640
Object {
3741
"code": "type",
3842
"message": "should be boolean",
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.