Skip to content

Commit ff4cd94

Browse files
committed
Merge branch 'master' into change-json
2 parents 9863b7c + a504f94 commit ff4cd94

File tree

12 files changed

+146
-22
lines changed

12 files changed

+146
-22
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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ 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
2729
get "/greetings" do
2830
parameter :target, "The thing you want to greet"
31+
parameter :scoped, "This is a scoped variable", :scope => :scope
2932
3033
example "Greeting your favorite gem" do
3134
do_request :target => "rspec_api_documentation"
@@ -60,16 +63,18 @@ Feature: Generate HTML documentation from test examples
6063
When I open the index
6164
And I navigate to "Greeting your favorite gem"
6265
Then I should see the following parameters:
63-
| name | description |
64-
| target | The thing you want to greet |
66+
| name | description |
67+
| target | The thing you want to greet |
68+
| scope[scoped] | This is a scoped variable |
6569

6670
Scenario: Example HTML documentation includes the request information
6771
When I open the index
6872
And I navigate to "Greeting your favorite gem"
6973
Then I should see the route is "GET /greetings?target=rspec_api_documentation"
7074
And I should see the following request headers:
71-
| Host | example.org |
7275
| Cookie | |
76+
And I should not see the following request headers:
77+
| Host | example.org |
7378
And I should see the following query parameters:
7479
| target | rspec_api_documentation |
7580

@@ -79,6 +84,7 @@ Feature: Generate HTML documentation from test examples
7984
Then I should see the response status is "200 OK"
8085
And I should see the following response headers:
8186
| Content-Type | application/json |
87+
And I should not see the following response headers:
8288
| Content-Length | 35 |
8389
And I should see the following response body:
8490
"""

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
@@ -61,6 +61,8 @@ def self.add_setting(name, opts = {})
6161
add_setting :keep_source_order, :default => false
6262
add_setting :api_name, :default => "API Documentation"
6363
add_setting :io_docs_protocol, :default => "http"
64+
add_setting :request_headers_to_include, :default => nil
65+
add_setting :response_headers_to_include, :default => nil
6466

6567
def client_method=(new_client_method)
6668
RspecApiDocumentation::DSL::Resource.module_eval <<-RUBY

lib/rspec_api_documentation/curl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def format_header(header)
6969
end
7070

7171
def format_full_header(header, value)
72-
formatted_value = value.gsub(/"/, "\\\"")
72+
formatted_value = value ? value.gsub(/"/, "\\\"") : ''
7373
"#{format_header(header)}: #{formatted_value}"
7474
end
7575

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"HTTP_X_HEADER" => "header",
1616
"HTTP_AUTHORIZATION" => %{Token token="mytoken"},
1717
"HTTP_HOST" => "example.org",
18-
"HTTP_COOKIES" => ""
18+
"HTTP_COOKIES" => "",
19+
"HTTP_SERVER" => nil
1920
}
2021
end
2122

@@ -26,6 +27,7 @@
2627
it { should =~ /-H "Accept: application\/json"/ }
2728
it { should =~ /-H "X-Header: header"/ }
2829
it { should =~ /-H "Authorization: Token token=\\"mytoken\\""/ }
30+
it { should =~ /-H "Server: "/ }
2931
it { should_not =~ /-H "Host: example\.org"/ }
3032
it { should_not =~ /-H "Cookies: "/ }
3133

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

templates/rspec_api_documentation/html_example.mustache

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@
5353
{{# parameters }}
5454
<tr>
5555
<td{{# required }} class="required"{{/ required }}>
56-
<span class="name">{{ name }}</span>
56+
{{# scope }}
57+
<span class="name">{{ scope }}[{{ name }}]</span>
58+
{{/ scope }}
59+
{{^ scope }}
60+
<span class="name">{{ name }}</span>
61+
{{/ scope }}
5762
</td>
5863
<td>
5964
<span class="description">{{ description }}</span>

0 commit comments

Comments
 (0)