Skip to content

Commit a504f94

Browse files
committed
Merge pull request #118 from Pact/header-filters
Filter request and/or response headers by configuration
2 parents 5224387 + b217268 commit a504f94

File tree

9 files changed

+132
-17
lines changed

9 files changed

+132
-17
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/combined_json.feature

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Feature: Combined text
2424
RspecApiDocumentation.configure do |config|
2525
config.app = App
2626
config.format = :combined_json
27+
config.request_headers_to_include = %w[Host]
28+
config.response_headers_to_include = %w[Content-Type]
2729
end
2830
2931
resource "Greetings" do
@@ -84,8 +86,7 @@ Feature: Combined text
8486
"request_path": "/greetings?target=rspec_api_documentation",
8587
"request_body": null,
8688
"request_headers": {
87-
"Host": "example.org",
88-
"Cookie": ""
89+
"Host": "example.org"
8990
},
9091
"request_query_parameters": {
9192
"target": "rspec_api_documentation"
@@ -95,8 +96,7 @@ Feature: Combined text
9596
"response_status_text": "OK",
9697
"response_body": "Hello, rspec_api_documentation!",
9798
"response_headers": {
98-
"Content-Type": "text/plain",
99-
"Content-Length": "31"
99+
"Content-Type": "text/plain"
100100
},
101101
"response_content_type": "text/plain",
102102
"curl": null
@@ -121,8 +121,7 @@ Feature: Combined text
121121
"request_path": "/greetings?target=Sam+%26+Eric",
122122
"request_body": null,
123123
"request_headers": {
124-
"Host": "example.org",
125-
"Cookie": ""
124+
"Host": "example.org"
126125
},
127126
"request_query_parameters": {
128127
"target": "Sam & Eric"
@@ -132,8 +131,7 @@ Feature: Combined text
132131
"response_status_text": "OK",
133132
"response_body": "Hello, Sam & Eric!",
134133
"response_headers": {
135-
"Content-Type": "text/plain",
136-
"Content-Length": "18"
134+
"Content-Type": "text/plain"
137135
},
138136
"response_content_type": "text/plain",
139137
"curl": null

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

features/textile_documentation.feature

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Feature: Generate Textile documentation from test examples
4545
config.app = App
4646
config.api_name = "Example API"
4747
config.format = :textile
48+
config.request_headers_to_include = %w[Content-Type Host]
49+
config.response_headers_to_include = %w[Content-Type Content-Length]
4850
end
4951
5052
resource 'Orders' do
@@ -180,8 +182,7 @@ Feature: Generate Textile documentation from test examples
180182
h4. Headers
181183
182184
<pre>Host: example.org
183-
Content-Type: application/x-www-form-urlencoded
184-
Cookie: </pre>
185+
Content-Type: application/x-www-form-urlencoded</pre>
185186
186187
h4. Route
187188
@@ -198,10 +199,7 @@ Feature: Generate Textile documentation from test examples
198199
h4. Headers
199200
200201
<pre>Content-Type: text/html;charset=utf-8
201-
Content-Length: 0
202-
X-XSS-Protection: 1; mode=block
203-
X-Content-Type-Options: nosniff
204-
X-Frame-Options: SAMEORIGIN</pre>
202+
Content-Length: 0</pre>
205203
206204
h4. Status
207205

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/example.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,25 @@ def explanation
4343
end
4444

4545
def requests
46-
metadata[:requests] || []
46+
filter_headers(metadata[:requests]) || []
47+
end
48+
49+
private
50+
51+
def filter_headers(requests)
52+
requests = remap_headers(requests, :request_headers, configuration.request_headers_to_include)
53+
requests = remap_headers(requests, :response_headers, configuration.response_headers_to_include)
54+
requests
55+
end
56+
57+
def remap_headers(requests, key, headers_to_include)
58+
return requests unless headers_to_include
59+
requests.each.with_index do |request_hash, index|
60+
next unless request_hash.key?(key)
61+
headers = request_hash[key]
62+
request_hash[key] = headers.select{ |key, _| headers_to_include.include?(key) }
63+
requests[index] = request_hash
64+
end
4765
end
4866
end
4967
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

spec/example_spec.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,82 @@
159159
example.explanation.should == nil
160160
end
161161
end
162+
163+
describe "request headers can be filtered" do
164+
before do
165+
configuration.request_headers_to_include = %w[Included]
166+
metadata[:requests] = [
167+
{
168+
:request_headers => {
169+
"Included" => "data",
170+
"Filtered" => "not seen"
171+
},
172+
:request_method => "GET"
173+
},
174+
{
175+
:request_headers => {
176+
"Included" => "data",
177+
"Other" => "not seen"
178+
},
179+
:request_method => "GET"
180+
}
181+
]
182+
end
183+
184+
it "should filter out anything not explicitly mentioned" do
185+
subject.requests.should == [
186+
{
187+
:request_headers => {
188+
"Included" => "data",
189+
},
190+
:request_method => "GET"
191+
},
192+
{
193+
:request_headers => {
194+
"Included" => "data",
195+
},
196+
:request_method => "GET"
197+
}
198+
]
199+
end
200+
end
201+
202+
describe "response headers can be filtered" do
203+
before do
204+
configuration.response_headers_to_include = %w[Included]
205+
metadata[:requests] = [
206+
{
207+
:response_headers => {
208+
"Included" => "data",
209+
"Filtered" => "not seen"
210+
},
211+
:request_method => "GET"
212+
},
213+
{
214+
:response_headers => {
215+
"Included" => "data",
216+
"Other" => "not seen"
217+
},
218+
:request_method => "GET"
219+
}
220+
]
221+
end
222+
223+
it "should filter out anything not explicitly mentioned" do
224+
subject.requests.should == [
225+
{
226+
:response_headers => {
227+
"Included" => "data",
228+
},
229+
:request_method => "GET"
230+
},
231+
{
232+
:response_headers => {
233+
"Included" => "data",
234+
},
235+
:request_method => "GET"
236+
}
237+
]
238+
end
239+
end
162240
end

0 commit comments

Comments
 (0)