Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions features/combined_json.feature
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ Feature: Combined text
"target": "rspec_api_documentation"
},
"request_content_type": null,
"request_format": null,
"response_status": 200,
"response_status_text": "OK",
"response_body": "Hello, rspec_api_documentation!",
"response_headers": {
"Content-Type": "text/plain"
},
"response_content_type": "text/plain",
"response_format": null,
"curl": null
}
]
Expand Down Expand Up @@ -133,13 +135,15 @@ Feature: Combined text
"target": "Sam & Eric"
},
"request_content_type": null,
"request_format": null,
"response_status": 200,
"response_status_text": "OK",
"response_body": "Hello, Sam & Eric!",
"response_headers": {
"Content-Type": "text/plain"
},
"response_content_type": "text/plain",
"response_format": null,
"curl": null
}
]
Expand Down
2 changes: 2 additions & 0 deletions features/combined_text.feature
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Feature: Combined text
GET /greetings?target=rspec_api_documentation
Cookie:
Host: example.org
Version: HTTP/1.0

target=rspec_api_documentation

Expand All @@ -101,6 +102,7 @@ Feature: Combined text
GET /greetings?target=Sam+%26+Eric
Cookie:
Host: example.org
Version: HTTP/1.0

target=Sam & Eric

Expand Down
40 changes: 26 additions & 14 deletions features/markdown_documentation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -185,26 +185,31 @@ Feature: Generate Markdown documentation from test examples

#### Headers

<pre>Host: example.org</pre>
```
Host: example.org
```

#### Route

<pre>GET /orders</pre>
`GET /orders`

### Response

#### Headers

<pre>Content-Type: application/json
Content-Length: 137</pre>
```
Content-Type: application/json
Content-Length: 137
```

#### Status

<pre>200 OK</pre>
`200 OK`

#### Body

<pre>{
```json
{
"page": 1,
"orders": [
{
Expand All @@ -218,7 +223,8 @@ Feature: Generate Markdown documentation from test examples
"description": "A great order"
}
]
}</pre>
}
```
"""

Scenario: Example 'Creating an order' file should look like we expect
Expand All @@ -242,27 +248,33 @@ Feature: Generate Markdown documentation from test examples

#### Headers

<pre>Host: example.org
Content-Type: application/x-www-form-urlencoded</pre>
```
Host: example.org
Content-Type: application/x-www-form-urlencoded
```

#### Route

<pre>POST /orders</pre>
`POST /orders`

#### Body

<pre>name=Order+3&amount=33.0</pre>
```
name=Order+3&amount=33.0
```

### Response

#### Headers

<pre>Content-Type: text/html;charset=utf-8
Content-Length: 0</pre>
```
Content-Type: text/html;charset=utf-8
Content-Length: 0
```

#### Status

<pre>201 Created</pre>
`201 Created`
"""

Scenario: Example 'Deleting an order' file should be created
Expand Down
2 changes: 2 additions & 0 deletions features/slate_documentation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Feature: Generate Slate documentation from test examples

```shell
curl -g "http://localhost:3000/orders" -X GET \
-H "Version: HTTP/1.0" \
-H "Host: example.org" \
-H "Cookie: "
```
Expand Down Expand Up @@ -219,6 +220,7 @@ Feature: Generate Slate documentation from test examples

```shell
curl "http://localhost:3000/orders" -d 'name=Order+3&amount=33.0' -X POST \
-H "Version: HTTP/1.0" \
-H "Host: example.org" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Cookie: "
Expand Down
11 changes: 11 additions & 0 deletions lib/rspec_api_documentation/client_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ def document_example(method, path)
request_metadata[:request_headers] = request_headers
request_metadata[:request_query_parameters] = query_hash
request_metadata[:request_content_type] = request_content_type
request_metadata[:request_format] = format_type(request_content_type)
request_metadata[:response_status] = status
request_metadata[:response_status_text] = Rack::Utils::HTTP_STATUS_CODES[status]
request_metadata[:response_body] = record_response_body(response_content_type, response_body)
request_metadata[:response_headers] = response_headers
request_metadata[:response_content_type] = response_content_type
request_metadata[:response_format] = format_type(response_content_type)
request_metadata[:curl] = Curl.new(method, path, request_body, request_headers)

metadata[:requests] ||= []
Expand All @@ -92,6 +94,15 @@ def record_response_body(response_content_type, response_body)
formatter.call(response_content_type, response_body)
end

def format_type(content_type)
case content_type
when /^application\/json/ then 'json'
when /^text\/html/ then 'html'
else
nil
end
end

def clean_out_uploaded_data(params, request_body)
params.each do |value|
if [Hash, Array].member? value.class
Expand Down
6 changes: 3 additions & 3 deletions lib/rspec_api_documentation/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ def self.add_setting(name, opts = {})
# See RspecApiDocumentation::DSL::Endpoint#do_request
add_setting :response_body_formatter, default: Proc.new { |_, _|
Proc.new do |content_type, response_body|
if response_body.encoding == Encoding::ASCII_8BIT
"[binary data]"
elsif content_type =~ /application\/.*json/
if content_type =~ /application\/.*json/
JSON.pretty_generate(JSON.parse(response_body))
elsif response_body.encoding == Encoding::ASCII_8BIT
"[binary data]"
else
response_body
end
Expand Down
15 changes: 7 additions & 8 deletions rspec_api_documentation.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $:.unshift lib unless $:.include?(lib)

Gem::Specification.new do |s|
s.name = "rspec_api_documentation"
s.version = "6.1.1"
s.version = "6.2.0"
s.platform = Gem::Platform::RUBY
s.authors = ["Chris Cahoon", "Sam Goldman", "Eric Oestrich", "Lucas Keune"]
s.email = %w[chris@smartlogicsolutions.com sam@smartlogicsolutions.com eric@smartlogicsolutions.com lucas.keune@qurasoft.de]
Expand All @@ -18,23 +18,22 @@ Gem::Specification.new do |s|
s.add_runtime_dependency "activesupport", ">= 3.0.0"
s.add_runtime_dependency "mustache", "~> 1.0", ">= 0.99.4"

s.add_development_dependency "activesupport", "< 4"
s.add_development_dependency "activesupport", "< 4.3"
s.add_development_dependency "bundler", ">= 1.16"
s.add_development_dependency "fakefs", ">= 0.6.0"
s.add_development_dependency "sinatra", "~> 1.4.7"
s.add_development_dependency "aruba", "~> 0.13.0"
s.add_development_dependency "sinatra", "~> 2"
s.add_development_dependency "aruba", "~> 1"
s.add_development_dependency "capybara", "~> 2.6.2"
s.add_development_dependency "rake", ">= 10"
s.add_development_dependency "rack-test", "~> 0.6.3"
s.add_development_dependency "rack-oauth2", "~> 1.2.2"
s.add_development_dependency "rack-test", "~> 2"
s.add_development_dependency "rack-oauth2", "~> 2"
s.add_development_dependency "webmock", "~> 3.8.3"
s.add_development_dependency "rspec-its", "~> 1.2.0"
s.add_development_dependency "faraday"
s.add_development_dependency "nokogiri", "~> 1.8"
s.add_development_dependency "yard", "~> 0.9.15"
s.add_development_dependency "inch", "~> 0.8.0"
s.add_development_dependency "minitest", "~> 5.8.4"
s.add_development_dependency "contracts", "~> 0.13.0"
s.add_development_dependency "minitest", "~> 5.8"
s.add_development_dependency "gherkin", "~> 3.2.0"
s.add_development_dependency "multi_json", "~> 1.11.2"
s.add_development_dependency "rspec", "~> 3.4.0"
Expand Down
5 changes: 4 additions & 1 deletion spec/rack_test_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

it "should contain all the headers" do
expect(test_client.request_headers).to eq({
"Version"=>"HTTP/1.0",
"Accept" => "application/json",
"Content-Type" => "application/json",
"Host" => "example.org",
Expand Down Expand Up @@ -85,12 +86,14 @@
expect(metadata[:request_headers]).to include({'X-Custom-Header' => 'custom header value'})
expect(metadata[:request_query_parameters]).to eq({"query" => "test query"})
expect(metadata[:request_content_type]).to match(/application\/json/)
expect(metadata[:request_format]).to eq('json')
expect(metadata[:response_status]).to eq(200)
expect(metadata[:response_body]).to be_present
expect(metadata[:response_headers]['Content-Type']).to match(/application\/json/)
expect(metadata[:response_headers]['Content-Length']).to eq('17')
expect(metadata[:response_content_type]).to match(/application\/json/)
expect(metadata[:curl]).to eq(RspecApiDocumentation::Curl.new("POST", "/greet?query=test+query", post_data, {"Content-Type" => "application/json;charset=utf-8", "X-Custom-Header" => "custom header value", "Host" => "example.org", "Cookie" => ""}))
expect(metadata[:response_format]).to eq('json')
expect(metadata[:curl]).to eq(RspecApiDocumentation::Curl.new("POST", "/greet?query=test+query", post_data, {"Version"=>"HTTP/1.0", "Content-Type" => "application/json;charset=utf-8", "X-Custom-Header" => "custom header value", "Host" => "example.org", "Cookie" => ""}))
end

specify "fetching binary data" do |example|
Expand Down
24 changes: 17 additions & 7 deletions templates/rspec_api_documentation/markdown_example.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,28 @@

#### Headers

<pre>{{ request_headers_text }}</pre>
```
{{ request_headers_text }}
```

#### Route

<pre>{{ request_method }} {{ request_path }}</pre>
`{{ request_method }} {{ request_path }}`
{{# request_query_parameters_text }}

#### Query Parameters

<pre>{{ request_query_parameters_text }}</pre>
```
{{ request_query_parameters_text }}
```
{{/ request_query_parameters_text }}
{{# request_body }}

#### Body

<pre>{{{ request_body }}}</pre>
```{{{ request_format }}}
{{{ request_body }}}
```
{{/ request_body }}
{{# curl }}

Expand All @@ -67,16 +73,20 @@

#### Headers

<pre>{{ response_headers_text }}</pre>
```
{{ response_headers_text }}
```

#### Status

<pre>{{ response_status }} {{ response_status_text}}</pre>
`{{ response_status }} {{ response_status_text}}`

{{# response_body }}
#### Body

<pre>{{{ response_body }}}</pre>
```{{{ response_format }}}
{{{ response_body }}}
```
{{/ response_body }}
{{/ response_status }}
{{/ requests }}