Skip to content

Commit 5ac5e7e

Browse files
committed
Update Readme
1 parent 763f211 commit 5ac5e7e

1 file changed

Lines changed: 36 additions & 17 deletions

File tree

README.md

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
# openapi_first
22

3-
openapi_first is a Ruby gem for request / response validation and contract-testing against an [OpenAPI](https://www.openapis.org/) 3.0 or 3.1 API description. It makes an APIFirst workflow easy and reliable.
3+
openapi_first is a Ruby gem for request / response validation and contract-testing against an [OpenAPI](https://www.openapis.org/) 3.0 or 3.1 Openapi API description (OAD). It makes an APIFirst workflow easy and reliable.
44

5-
You can use openapi_first on production for [request validation](#request-validation) and in your [tests](#contract-testing) to avoid API drift with it's request/response validation and coverage features.
5+
## Usage
6+
7+
Use an OAD to validate incoming requests in production:
8+
```ruby
9+
use OpenapiFirst::Middlewares::RequestValidation, 'openapi/openapi.yaml'
10+
```
11+
12+
Turn your request tests into contract tests against an OAD:
13+
```ruby
14+
# spec_helper.rb
15+
require 'openapi_first'
16+
OpenapiFirst::Test.setup do |config|
17+
config.register('openapi/openapi.yaml')
18+
end
19+
require 'application' # Load Application code
20+
OpenapiFirst::Test.observe(Application)
21+
```
622

723
## Contents
824

@@ -37,23 +53,26 @@ Here is how to set it up:
3753
require 'openapi_first'
3854
OpenapiFirst::Test.setup do |config|
3955
config.register('openapi/openapi.yaml')
40-
# Optional: Skip certain responses, which are described in your API description, but need no test coverage
41-
config.skip_response_coverage { |response_definition| response_definition.status.to_s == '500' }
42-
end
43-
```
44-
2. Add an `app` method to your tests by including a Module. This `app` method wraps your application with silent request / response validation. This validates all requests/responses in your test run. (✷1)
45-
```ruby
46-
RSpec.configure do |config|
47-
config.include OpenapiFirst::Test::Methods[MyApp], type: :request
48-
end
49-
```
50-
Or add the `app` method yourself:
51-
52-
```ruby
53-
def app
54-
OpenapiFirst::Test.app(MyApp)
5556
end
5657
```
58+
2. Observe your application. You can do this in one of two ways:
59+
- Inject a Module to wrap (prepend) the `call` method of your Rack app Class.
60+
```ruby
61+
OpenapiFirst::Test.observe(MyApplication)
62+
```
63+
- Or add an `app` method to your tests, which wraps your application with silent request / response validation. (✷1)
64+
```ruby
65+
RSpec.configure do |config|
66+
config.include OpenapiFirst::Test::Methods[MyApp], type: :request
67+
end
68+
```
69+
Or add the `app` method yourself:
70+
71+
```ruby
72+
def app
73+
OpenapiFirst::Test.app(MyApp)
74+
end
75+
```
5776
3. Run your tests. The Coverage feature will tell you about missing or invalid requests/responses.
5877

5978
(✷1): It does not matter what method of openapi_first you use to validate requests/responses. Instead of using `OpenapiFirstTest.app` to wrap your application, you could also use the [middlewares](#rack-middlewares) or [test assertion method](#test-assertions), but you would have to do that for all requests/responses defined in your API description to make coverage work.

0 commit comments

Comments
 (0)