Skip to content

Commit

Permalink
Warning Duplicate definition of 'Content-Type' header
Browse files Browse the repository at this point in the history
- Remove Content-Type header from the Header sections
  • Loading branch information
joel committed Mar 13, 2018
1 parent 2f0a216 commit 561552d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 35 deletions.
11 changes: 0 additions & 11 deletions features/api_blueprint_documentation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ Feature: Generate API Blueprint documentation from test examples
+ Headers
Content-Type: text/html;charset=utf-8
Content-Length: 57
+ Body
Expand All @@ -288,7 +287,6 @@ Feature: Generate API Blueprint documentation from test examples
+ Headers
Content-Type: application/json
Host: example.org
+ Body
Expand All @@ -308,7 +306,6 @@ Feature: Generate API Blueprint documentation from test examples
+ Headers
Content-Type: application/json
Content-Length: 73
+ Body
Expand All @@ -333,7 +330,6 @@ Feature: Generate API Blueprint documentation from test examples
+ Headers
Content-Type: application/vnd.api+json
Content-Length: 137
+ Body
Expand Down Expand Up @@ -372,13 +368,11 @@ Feature: Generate API Blueprint documentation from test examples
+ Headers
Host: example.org
Content-Type: application/x-www-form-urlencoded
+ Response 200 (text/html;charset=utf-8)
+ Headers
Content-Type: text/html;charset=utf-8
Content-Length: 0
### Returns a single order [GET]
Expand All @@ -393,7 +387,6 @@ Feature: Generate API Blueprint documentation from test examples
+ Headers
Content-Type: application/json
Content-Length: 73
+ Body
Expand All @@ -412,21 +405,18 @@ Feature: Generate API Blueprint documentation from test examples
+ Headers
Content-Type: application/json; charset=utf-16
Host: example.org
+ Response 400 (application/json)
+ Headers
Content-Type: application/json
Content-Length: 0
+ Request Update an order (application/json; charset=utf-16)
+ Headers
Content-Type: application/json; charset=utf-16
Host: example.org
+ Body
Expand All @@ -445,7 +435,6 @@ Feature: Generate API Blueprint documentation from test examples
+ Headers
Content-Type: application/json
Content-Length: 111
+ Body
Expand Down
16 changes: 14 additions & 2 deletions lib/rspec_api_documentation/views/api_blueprint_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ def parameters

def requests
super.map do |request|
request[:request_headers_text] = remove_utf8_for_json(request[:request_headers_text])
request[:request_headers_text] = remove_utf8_for_json(remove_content_type(request[:request_headers_text]))
request[:request_headers_text] = indent(request[:request_headers_text])
request[:request_content_type] = content_type(request[:request_headers])
request[:request_content_type] = remove_utf8_for_json(request[:request_content_type])
request[:request_body] = body_to_json(request, :request)
request[:request_body] = indent(request[:request_body])

request[:response_headers_text] = remove_utf8_for_json(request[:response_headers_text])
request[:response_headers_text] = remove_utf8_for_json(remove_content_type(request[:response_headers_text]))
request[:response_headers_text] = indent(request[:response_headers_text])
request[:response_content_type] = content_type(request[:response_headers])
request[:response_content_type] = remove_utf8_for_json(request[:response_content_type])
Expand All @@ -46,6 +46,18 @@ def extension

private

# `Content-Type` header is removed because the information would be duplicated
# since it's already present in `request[:request_content_type]`.
def remove_content_type(headers)
return unless headers
headers
.split("\n")
.reject { |header|
header.start_with?('Content-Type:')
}
.join("\n")
end

def has_request?(metadata)
metadata.any? do |key, value|
[:request_body, :request_headers, :request_content_type].include?(key) && value
Expand Down
28 changes: 6 additions & 22 deletions spec/views/api_blueprint_example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,9 @@
describe 'request_headers_text' do
subject { view.requests[0][:request_headers_text] }

context 'when charset=utf-8 is present' do
it "just strips that because it's the default for json" do
expect(subject).to eq "Content-Type: application/json\n Another: header; charset=utf-8"
end
end

context 'when charset=utf-16 is present' do
let(:content_type) { "application/json; charset=utf-16" }

it "keeps that because it's NOT the default for json" do
expect(subject).to eq "Content-Type: application/json; charset=utf-16\n Another: header; charset=utf-8"
context 'when Content-Type is present' do
it "removes it" do
expect(subject).to eq "Another: header; charset=utf-8"
end
end
end
Expand All @@ -93,17 +85,9 @@
describe 'response_headers_text' do
subject { view.requests[0][:response_headers_text] }

context 'when charset=utf-8 is present' do
it "just strips that because it's the default for json" do
expect(subject).to eq "Content-Type: application/json\n Another: header; charset=utf-8"
end
end

context 'when charset=utf-16 is present' do
let(:content_type) { "application/json; charset=utf-16" }

it "keeps that because it's NOT the default for json" do
expect(subject).to eq "Content-Type: application/json; charset=utf-16\n Another: header; charset=utf-8"
context 'when Content-Type is present' do
it "removes it" do
expect(subject).to eq "Another: header; charset=utf-8"
end
end
end
Expand Down

0 comments on commit 561552d

Please sign in to comment.