Skip to content

Filter request and/or response headers by configuration #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 7, 2014
Merged
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ RspecApiDocumentation.configure do |config|
# Allows you to filter out headers that are not needed in the cURL request,
# such as "Host" and "Cookie". Set as an array.
config.curl_headers_to_filter = nil


# By default, when these settings are nil, all headers are shown,
# which is sometimes too chatty. Setting the parameters to an
# array of headers will render *only* those headers.
config.request_headers_to_include = nil
config.response_headers_to_include = nil

# By default examples and resources are ordered by description. Set to true keep
# the source order.
config.keep_source_order = false
Expand Down
14 changes: 6 additions & 8 deletions features/combined_json.feature
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Feature: Combined text
RspecApiDocumentation.configure do |config|
config.app = App
config.format = :combined_json
config.request_headers_to_include = %w[Host]
config.response_headers_to_include = %w[Content-Type]
end

resource "Greetings" do
Expand Down Expand Up @@ -84,8 +86,7 @@ Feature: Combined text
"request_path": "/greetings?target=rspec_api_documentation",
"request_body": null,
"request_headers": {
"Host": "example.org",
"Cookie": ""
"Host": "example.org"
},
"request_query_parameters": {
"target": "rspec_api_documentation"
Expand All @@ -95,8 +96,7 @@ Feature: Combined text
"response_status_text": "OK",
"response_body": "Hello, rspec_api_documentation!",
"response_headers": {
"Content-Type": "text/plain",
"Content-Length": "31"
"Content-Type": "text/plain"
},
"response_content_type": "text/plain",
"curl": null
Expand All @@ -121,8 +121,7 @@ Feature: Combined text
"request_path": "/greetings?target=Sam+%26+Eric",
"request_body": null,
"request_headers": {
"Host": "example.org",
"Cookie": ""
"Host": "example.org"
},
"request_query_parameters": {
"target": "Sam & Eric"
Expand All @@ -132,8 +131,7 @@ Feature: Combined text
"response_status_text": "OK",
"response_body": "Hello, Sam & Eric!",
"response_headers": {
"Content-Type": "text/plain",
"Content-Length": "18"
"Content-Type": "text/plain"
},
"response_content_type": "text/plain",
"curl": null
Expand Down
6 changes: 5 additions & 1 deletion features/html_documentation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Feature: Generate HTML documentation from test examples
RspecApiDocumentation.configure do |config|
config.app = App
config.api_name = "Example API"
config.request_headers_to_include = %w[Cookie]
config.response_headers_to_include = %w[Content-Type]
end

resource "Greetings" do
Expand Down Expand Up @@ -70,8 +72,9 @@ Feature: Generate HTML documentation from test examples
And I navigate to "Greeting your favorite gem"
Then I should see the route is "GET /greetings?target=rspec_api_documentation"
And I should see the following request headers:
| Host | example.org |
| Cookie | |
And I should not see the following request headers:
| Host | example.org |
And I should see the following query parameters:
| target | rspec_api_documentation |

Expand All @@ -81,6 +84,7 @@ Feature: Generate HTML documentation from test examples
Then I should see the response status is "200 OK"
And I should see the following response headers:
| Content-Type | application/json |
And I should not see the following response headers:
| Content-Length | 35 |
And I should see the following response body:
"""
Expand Down
9 changes: 9 additions & 0 deletions features/step_definitions/html_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
end
end

Then /^I should not see the following (request|response) headers:$/ do |part, table|
actual_headers = page.find("pre.#{part}.headers").text
expected_headers = table.raw.map { |row| row.join(": ") }

expected_headers.each do |row|
actual_headers.should_not include(row.strip)
end
end

Then /^I should see the route is "([^"]*)"$/ do |route|
page.should have_css(".request.route", :text => route)
end
Expand Down
10 changes: 4 additions & 6 deletions features/textile_documentation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Feature: Generate Textile documentation from test examples
config.app = App
config.api_name = "Example API"
config.format = :textile
config.request_headers_to_include = %w[Content-Type Host]
config.response_headers_to_include = %w[Content-Type Content-Length]
end

resource 'Orders' do
Expand Down Expand Up @@ -180,8 +182,7 @@ Feature: Generate Textile documentation from test examples
h4. Headers

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

h4. Route

Expand All @@ -198,10 +199,7 @@ Feature: Generate Textile documentation from test examples
h4. Headers

<pre>Content-Type: text/html;charset=utf-8
Content-Length: 0
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN</pre>
Content-Length: 0</pre>

h4. Status

Expand Down
2 changes: 2 additions & 0 deletions lib/rspec_api_documentation/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def self.add_setting(name, opts = {})
add_setting :keep_source_order, :default => false
add_setting :api_name, :default => "API Documentation"
add_setting :io_docs_protocol, :default => "http"
add_setting :request_headers_to_include, :default => nil
add_setting :response_headers_to_include, :default => nil

def client_method=(new_client_method)
RspecApiDocumentation::DSL::Resource.module_eval <<-RUBY
Expand Down
20 changes: 19 additions & 1 deletion lib/rspec_api_documentation/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,25 @@ def explanation
end

def requests
metadata[:requests] || []
filter_headers(metadata[:requests]) || []
end

private

def filter_headers(requests)
requests = remap_headers(requests, :request_headers, configuration.request_headers_to_include)
requests = remap_headers(requests, :response_headers, configuration.response_headers_to_include)
requests
end

def remap_headers(requests, key, headers_to_include)
return requests unless headers_to_include
requests.each.with_index do |request_hash, index|
next unless request_hash.key?(key)
headers = request_hash[key]
request_hash[key] = headers.select{ |key, _| headers_to_include.include?(key) }
requests[index] = request_hash
end
end
end
end
2 changes: 2 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
its(:api_name) { should == "API Documentation" }
its(:client_method) { should == :client }
its(:io_docs_protocol) { should == "http" }
its(:request_headers_to_include) { should be_nil }
its(:response_headers_to_include) { should be_nil }
end

describe "#define_groups" do
Expand Down
78 changes: 78 additions & 0 deletions spec/example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,82 @@
example.explanation.should == nil
end
end

describe "request headers can be filtered" do
before do
configuration.request_headers_to_include = %w[Included]
metadata[:requests] = [
{
:request_headers => {
"Included" => "data",
"Filtered" => "not seen"
},
:request_method => "GET"
},
{
:request_headers => {
"Included" => "data",
"Other" => "not seen"
},
:request_method => "GET"
}
]
end

it "should filter out anything not explicitly mentioned" do
subject.requests.should == [
{
:request_headers => {
"Included" => "data",
},
:request_method => "GET"
},
{
:request_headers => {
"Included" => "data",
},
:request_method => "GET"
}
]
end
end

describe "response headers can be filtered" do
before do
configuration.response_headers_to_include = %w[Included]
metadata[:requests] = [
{
:response_headers => {
"Included" => "data",
"Filtered" => "not seen"
},
:request_method => "GET"
},
{
:response_headers => {
"Included" => "data",
"Other" => "not seen"
},
:request_method => "GET"
}
]
end

it "should filter out anything not explicitly mentioned" do
subject.requests.should == [
{
:response_headers => {
"Included" => "data",
},
:request_method => "GET"
},
{
:response_headers => {
"Included" => "data",
},
:request_method => "GET"
}
]
end
end
end