Skip to content

Commit f51707b

Browse files
author
Ken Mayer
committed
Filter request and/or response headers by configuration
New configuration variables: - response_headers_to_include - request_headers_to_include
1 parent 2b0e4f7 commit f51707b

File tree

6 files changed

+34
-4
lines changed

6 files changed

+34
-4
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,13 @@ RspecApiDocumentation.configure do |config|
102102
# Allows you to filter out headers that are not needed in the cURL request,
103103
# such as "Host" and "Cookie". Set as an array.
104104
config.curl_headers_to_filter = nil
105-
105+
106+
# By default, when these settings are nil, all headers are shown,
107+
# which is sometimes too chatty. Setting the parameters to an
108+
# array of headers will render *only* those headers.
109+
config.request_headers_to_include = nil
110+
config.response_headers_to_include = nil
111+
106112
# By default examples and resources are ordered by description. Set to true keep
107113
# the source order.
108114
config.keep_source_order = false

features/html_documentation.feature

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Feature: Generate HTML documentation from test examples
2121
RspecApiDocumentation.configure do |config|
2222
config.app = App
2323
config.api_name = "Example API"
24+
config.request_headers_to_include = %w[Cookie]
25+
config.response_headers_to_include = %w[Content-Type]
2426
end
2527
2628
resource "Greetings" do
@@ -70,8 +72,9 @@ Feature: Generate HTML documentation from test examples
7072
And I navigate to "Greeting your favorite gem"
7173
Then I should see the route is "GET /greetings?target=rspec_api_documentation"
7274
And I should see the following request headers:
73-
| Host | example.org |
7475
| Cookie | |
76+
And I should not see the following request headers:
77+
| Host | example.org |
7578
And I should see the following query parameters:
7679
| target | rspec_api_documentation |
7780

@@ -81,6 +84,7 @@ Feature: Generate HTML documentation from test examples
8184
Then I should see the response status is "200 OK"
8285
And I should see the following response headers:
8386
| Content-Type | application/json |
87+
And I should not see the following response headers:
8488
| Content-Length | 35 |
8589
And I should see the following response body:
8690
"""

features/step_definitions/html_steps.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
end
2727
end
2828

29+
Then /^I should not see the following (request|response) headers:$/ do |part, table|
30+
actual_headers = page.find("pre.#{part}.headers").text
31+
expected_headers = table.raw.map { |row| row.join(": ") }
32+
33+
expected_headers.each do |row|
34+
actual_headers.should_not include(row.strip)
35+
end
36+
end
37+
2938
Then /^I should see the route is "([^"]*)"$/ do |route|
3039
page.should have_css(".request.route", :text => route)
3140
end

lib/rspec_api_documentation/configuration.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def self.add_setting(name, opts = {})
5959
add_setting :keep_source_order, :default => false
6060
add_setting :api_name, :default => "API Documentation"
6161
add_setting :io_docs_protocol, :default => "http"
62+
add_setting :request_headers_to_include, :default => nil
63+
add_setting :response_headers_to_include, :default => nil
6264

6365
def client_method=(new_client_method)
6466
RspecApiDocumentation::DSL::Resource.module_eval <<-RUBY

lib/rspec_api_documentation/views/markup_example.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ def initialize(example, configuration)
77
@example = example
88
@host = configuration.curl_host
99
@filter_headers = configuration.curl_headers_to_filter
10+
@request_headers_to_include = configuration.request_headers_to_include
11+
@response_headers_to_include = configuration.response_headers_to_include
1012
self.template_path = configuration.template_path
1113
end
1214

@@ -30,9 +32,9 @@ def filename
3032

3133
def requests
3234
super.map do |hash|
33-
hash[:request_headers_text] = format_hash(hash[:request_headers])
35+
hash[:request_headers_text] = format_hash(filter_hash(hash[:request_headers], @request_headers_to_include))
3436
hash[:request_query_parameters_text] = format_hash(hash[:request_query_parameters])
35-
hash[:response_headers_text] = format_hash(hash[:response_headers])
37+
hash[:response_headers_text] = format_hash(filter_hash(hash[:response_headers], @response_headers_to_include))
3638
if @host
3739
if hash[:curl].is_a? RspecApiDocumentation::Curl
3840
hash[:curl] = hash[:curl].output(@host, @filter_headers)
@@ -56,6 +58,11 @@ def format_hash(hash = {})
5658
"#{k}: #{v}"
5759
end.join("\n")
5860
end
61+
62+
def filter_hash(hash = {}, selection_set = nil)
63+
return hash unless selection_set
64+
hash.select{ |key, value| selection_set.include?(key) }
65+
end
5966
end
6067
end
6168
end

spec/configuration_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
its(:api_name) { should == "API Documentation" }
5555
its(:client_method) { should == :client }
5656
its(:io_docs_protocol) { should == "http" }
57+
its(:request_headers_to_include) { should be_nil }
58+
its(:response_headers_to_include) { should be_nil }
5759
end
5860

5961
describe "#define_groups" do

0 commit comments

Comments
 (0)