Skip to content

Commit dd71da1

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 f51707b commit dd71da1

File tree

5 files changed

+113
-24
lines changed

5 files changed

+113
-24
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,29 @@ 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+
requests.each.with_index do |request_hash, index|
59+
next unless request_hash.key?(key)
60+
headers = request_hash[key]
61+
request_hash[key] = filter_hash(headers, headers_to_include)
62+
requests[index] = request_hash
63+
end
64+
end
65+
66+
def filter_hash(hash = {}, selection_set = nil)
67+
return hash unless selection_set
68+
hash.select{ |key, _| selection_set.include?(key) }
4769
end
4870
end
4971
end

lib/rspec_api_documentation/views/markup_example.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ 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
1210
self.template_path = configuration.template_path
1311
end
1412

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

3331
def requests
3432
super.map do |hash|
35-
hash[:request_headers_text] = format_hash(filter_hash(hash[:request_headers], @request_headers_to_include))
33+
hash[:request_headers_text] = format_hash(hash[:request_headers])
3634
hash[:request_query_parameters_text] = format_hash(hash[:request_query_parameters])
37-
hash[:response_headers_text] = format_hash(filter_hash(hash[:response_headers], @response_headers_to_include))
35+
hash[:response_headers_text] = format_hash(hash[:response_headers])
3836
if @host
3937
if hash[:curl].is_a? RspecApiDocumentation::Curl
4038
hash[:curl] = hash[:curl].output(@host, @filter_headers)
@@ -58,11 +56,6 @@ def format_hash(hash = {})
5856
"#{k}: #{v}"
5957
end.join("\n")
6058
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
6659
end
6760
end
6861
end

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)