Skip to content

Commit 067c8bb

Browse files
committed
If test client encounters JSON in the body, pretty print it.
Closes #212
1 parent 8e670ea commit 067c8bb

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

features/html_documentation.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,5 @@ Feature: Generate HTML documentation from test examples
9797
| Content-Length | 35 |
9898
And I should see the following response body:
9999
"""
100-
{"hello":"rspec_api_documentation"}
100+
{ "hello": "rspec_api_documentation" }
101101
"""

features/markdown_documentation.feature

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,21 @@ Feature: Generate Markdown documentation from test examples
199199
200200
#### Body
201201
202-
<pre>{"page":1,"orders":[{"name":"Order 1","amount":9.99,"description":null},{"name":"Order 2","amount":100.0,"description":"A great order"}]}</pre>
202+
<pre>{
203+
"page": 1,
204+
"orders": [
205+
{
206+
"name": "Order 1",
207+
"amount": 9.99,
208+
"description": null
209+
},
210+
{
211+
"name": "Order 2",
212+
"amount": 100.0,
213+
"description": "A great order"
214+
}
215+
]
216+
}</pre>
203217
204218
"""
205219

features/textile_documentation.feature

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,21 @@ Feature: Generate Textile documentation from test examples
199199
200200
h4. Body
201201
202-
<pre>{"page":1,"orders":[{"name":"Order 1","amount":9.99,"description":null},{"name":"Order 2","amount":100.0,"description":"A great order"}]}</pre>
202+
<pre>{
203+
"page": 1,
204+
"orders": [
205+
{
206+
"name": "Order 1",
207+
"amount": 9.99,
208+
"description": null
209+
},
210+
{
211+
"name": "Order 2",
212+
"amount": 100.0,
213+
"description": "A great order"
214+
}
215+
]
216+
}</pre>
203217
204218
205219
"""

lib/rspec_api_documentation/client_base.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def document_example(method, path)
6868
request_metadata[:request_content_type] = request_content_type
6969
request_metadata[:response_status] = status
7070
request_metadata[:response_status_text] = Rack::Utils::HTTP_STATUS_CODES[status]
71-
request_metadata[:response_body] = record_response_body(response_body)
71+
request_metadata[:response_body] = record_response_body(response_content_type, response_body)
7272
request_metadata[:response_headers] = response_headers
7373
request_metadata[:response_content_type] = response_content_type
7474
request_metadata[:curl] = Curl.new(method, path, request_body, request_headers)
@@ -85,10 +85,12 @@ def headers(method, path, params, request_headers)
8585
request_headers || {}
8686
end
8787

88-
def record_response_body(response_body)
88+
def record_response_body(response_content_type, response_body)
8989
return nil if response_body.empty?
9090
if response_body.encoding == Encoding::ASCII_8BIT
9191
"[binary data]"
92+
elsif response_content_type =~ /application\/json/
93+
JSON.pretty_generate(JSON.parse(response_body))
9294
else
9395
response_body
9496
end

spec/rack_test_client_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@
9999
expect(metadata[:response_body]).to eq("[binary data]")
100100
end
101101

102+
specify "fetching json data" do |example|
103+
metadata = example.metadata[:requests].first
104+
expect(metadata[:response_body]).to eq(JSON.pretty_generate({
105+
:hello => "nurse",
106+
}))
107+
end
108+
102109
context "when post data is not json" do
103110
let(:post_data) { { :target => "nurse", :email => "email@example.com" } }
104111

0 commit comments

Comments
 (0)