diff --git a/features/combined_json.feature b/features/combined_json.feature index 55c791b8..5ff81e7f 100644 --- a/features/combined_json.feature +++ b/features/combined_json.feature @@ -96,6 +96,7 @@ 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!", @@ -103,6 +104,7 @@ Feature: Combined text "Content-Type": "text/plain" }, "response_content_type": "text/plain", + "response_format": null, "curl": null } ] @@ -133,6 +135,7 @@ 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!", @@ -140,6 +143,7 @@ Feature: Combined text "Content-Type": "text/plain" }, "response_content_type": "text/plain", + "response_format": null, "curl": null } ] diff --git a/features/combined_text.feature b/features/combined_text.feature index 7556fe07..15c165ff 100644 --- a/features/combined_text.feature +++ b/features/combined_text.feature @@ -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 @@ -101,6 +102,7 @@ Feature: Combined text GET /greetings?target=Sam+%26+Eric Cookie: Host: example.org + Version: HTTP/1.0 target=Sam & Eric diff --git a/features/markdown_documentation.feature b/features/markdown_documentation.feature index d77e10bd..d145b392 100644 --- a/features/markdown_documentation.feature +++ b/features/markdown_documentation.feature @@ -185,26 +185,31 @@ Feature: Generate Markdown documentation from test examples #### Headers -
Host: example.org+ ``` + Host: example.org + ``` #### Route -
GET /orders+ `GET /orders` ### Response #### Headers -
Content-Type: application/json - Content-Length: 137+ ``` + Content-Type: application/json + Content-Length: 137 + ``` #### Status -
200 OK+ `200 OK` #### Body -
{
+ ```json
+ {
"page": 1,
"orders": [
{
@@ -218,7 +223,8 @@ Feature: Generate Markdown documentation from test examples
"description": "A great order"
}
]
- }
+ }
+ ```
"""
Scenario: Example 'Creating an order' file should look like we expect
@@ -242,27 +248,33 @@ Feature: Generate Markdown documentation from test examples
#### Headers
- Host: example.org - Content-Type: application/x-www-form-urlencoded+ ``` + Host: example.org + Content-Type: application/x-www-form-urlencoded + ``` #### Route -
POST /orders+ `POST /orders` #### Body -
name=Order+3&amount=33.0+ ``` + name=Order+3&amount=33.0 + ``` ### Response #### Headers -
Content-Type: text/html;charset=utf-8 - Content-Length: 0+ ``` + Content-Type: text/html;charset=utf-8 + Content-Length: 0 + ``` #### Status -
201 Created+ `201 Created` """ Scenario: Example 'Deleting an order' file should be created diff --git a/features/slate_documentation.feature b/features/slate_documentation.feature index 0dcd026c..76361080 100644 --- a/features/slate_documentation.feature +++ b/features/slate_documentation.feature @@ -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: " ``` @@ -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: " diff --git a/lib/rspec_api_documentation/client_base.rb b/lib/rspec_api_documentation/client_base.rb index db0560a3..6d8ab1a2 100644 --- a/lib/rspec_api_documentation/client_base.rb +++ b/lib/rspec_api_documentation/client_base.rb @@ -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] ||= [] @@ -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 diff --git a/lib/rspec_api_documentation/configuration.rb b/lib/rspec_api_documentation/configuration.rb index cd50524f..8ea44be7 100644 --- a/lib/rspec_api_documentation/configuration.rb +++ b/lib/rspec_api_documentation/configuration.rb @@ -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 diff --git a/rspec_api_documentation.gemspec b/rspec_api_documentation.gemspec index ece82e4c..a3c3ac22 100644 --- a/rspec_api_documentation.gemspec +++ b/rspec_api_documentation.gemspec @@ -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] @@ -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" diff --git a/spec/rack_test_client_spec.rb b/spec/rack_test_client_spec.rb index e3a9b53c..e119208e 100644 --- a/spec/rack_test_client_spec.rb +++ b/spec/rack_test_client_spec.rb @@ -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", @@ -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| diff --git a/templates/rspec_api_documentation/markdown_example.mustache b/templates/rspec_api_documentation/markdown_example.mustache index b539b32e..8f7b4e86 100644 --- a/templates/rspec_api_documentation/markdown_example.mustache +++ b/templates/rspec_api_documentation/markdown_example.mustache @@ -38,22 +38,28 @@ #### Headers -
{{ request_headers_text }}
+```
+{{ request_headers_text }}
+```
#### Route
-{{ request_method }} {{ request_path }}
+`{{ request_method }} {{ request_path }}`
{{# request_query_parameters_text }}
#### Query Parameters
-{{ request_query_parameters_text }}
+```
+{{ request_query_parameters_text }}
+```
{{/ request_query_parameters_text }}
{{# request_body }}
#### Body
-{{{ request_body }}}
+```{{{ request_format }}}
+{{{ request_body }}}
+```
{{/ request_body }}
{{# curl }}
@@ -67,16 +73,20 @@
#### Headers
-{{ response_headers_text }}
+```
+{{ response_headers_text }}
+```
#### Status
-{{ response_status }} {{ response_status_text}}
+`{{ response_status }} {{ response_status_text}}`
{{# response_body }}
#### Body
-{{{ response_body }}}
+```{{{ response_format }}}
+{{{ response_body }}}
+```
{{/ response_body }}
{{/ response_status }}
{{/ requests }}