Skip to content

Commit 8de9ded

Browse files
committed
Rename last_response_headers to response_headers. Change internal TestClient methods to match.
1 parent faebdcb commit 8de9ded

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

features/html_documentation.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Feature: Generate HTML documentation from test examples
2929
example "Greeting your favorite gem" do
3030
do_request :target => "rspec_api_documentation"
3131
32+
response_headers["Content-Type"].should eq("application/json")
3233
status.should eq(200)
3334
response_body.should eq('{"hello":"rspec_api_documentation"}')
3435
end

lib/rspec_api_documentation/dsl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module RspecApiDocumentation
66
module DSL
77
extend ActiveSupport::Concern
88

9-
delegate :last_response_headers, :status, :response_body, :to => :client
9+
delegate :response_headers, :status, :response_body, :to => :client
1010
delegate :request_method, :request_headers, :request_body, :to => :destination
1111

1212
module ClassMethods

lib/rspec_api_documentation/test_client.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "coderay"
2+
13
module RspecApiDocumentation
24
class TestClient < Struct.new(:context, :options)
35
include Headers
@@ -34,20 +36,20 @@ def delete(*args)
3436
process :delete, *args
3537
end
3638

37-
def last_request_headers
39+
def request_headers
3840
env_to_headers(last_request.env)
3941
end
4042

41-
def last_response_headers
43+
def response_headers
4244
last_response.headers
4345
end
4446

45-
def last_query_string
47+
def query_string
4648
last_request.env["QUERY_STRING"]
4749
end
4850

49-
def last_query_hash
50-
strings = last_query_string.split("&")
51+
def query_hash
52+
strings = query_string.split("&")
5153
arrays = strings.map do |segment|
5254
segment.split("=")
5355
end
@@ -89,13 +91,13 @@ def document_example(method, action, params)
8991
request_metadata[:request_method] = method.to_s.upcase
9092
request_metadata[:request_path] = action
9193
request_metadata[:request_body] = highlight_syntax(request_body, last_request.content_type, true)
92-
request_metadata[:request_headers] = format_headers(last_request_headers)
93-
request_metadata[:request_query_parameters] = format_query_hash(last_query_hash)
94-
request_metadata[:response_status] = last_response.status
95-
request_metadata[:response_status_text] = Rack::Utils::HTTP_STATUS_CODES[last_response.status]
96-
request_metadata[:response_body] = highlight_syntax(response_body, last_response_headers['Content-Type'])
97-
request_metadata[:response_headers] = format_headers(last_response_headers)
98-
request_metadata[:curl] = Curl.new(method.to_s, action, request_body, last_request_headers)
94+
request_metadata[:request_headers] = format_headers(request_headers)
95+
request_metadata[:request_query_parameters] = format_query_hash(query_hash)
96+
request_metadata[:response_status] = status
97+
request_metadata[:response_status_text] = Rack::Utils::HTTP_STATUS_CODES[status]
98+
request_metadata[:response_body] = highlight_syntax(response_body, response_headers['Content-Type'])
99+
request_metadata[:response_headers] = format_headers(response_headers)
100+
request_metadata[:curl] = Curl.new(method.to_s, action, request_body, request_headers)
99101

100102
metadata[:requests] ||= []
101103
metadata[:requests] << request_metadata

spec/test_client_spec.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,35 @@ class StubApp < Sinatra::Base
4747
end
4848

4949
it "should handle xml data" do
50-
test_client.last_response_headers["Content-Type"].should =~ /application\/xml/
50+
test_client.response_headers["Content-Type"].should =~ /application\/xml/
5151
end
5252

5353
it "should log the request" do
5454
example.metadata[:requests].first[:response_body].should be_present
5555
end
5656
end
5757

58-
describe "#last_query_string" do
58+
describe "#query_string" do
5959
before do
6060
test_client.get "/?query_string=true"
6161
end
6262

6363
it 'should contain the query_string' do
64-
test_client.last_query_string.should == "query_string=true"
64+
test_client.query_string.should == "query_string=true"
6565
end
6666
end
6767

68-
describe "#last_query_hash" do
68+
describe "#query_hash" do
6969
before do
7070
test_client.get "/?query_hash=true"
7171
end
7272

7373
it 'should contain the query_hash' do
74-
test_client.last_query_hash.should == { "query_hash" => "true" }
74+
test_client.query_hash.should == { "query_hash" => "true" }
7575
end
7676
end
7777

78-
describe "#last_request_headers" do
78+
describe "#request_headers" do
7979
before do
8080
test_client.options[:headers] = {
8181
"HTTP_ACCEPT" => "application/json",
@@ -85,7 +85,7 @@ class StubApp < Sinatra::Base
8585
end
8686

8787
it "should contain all the headers" do
88-
test_client.last_request_headers.should eq({
88+
test_client.request_headers.should eq({
8989
"Accept" => "application/json",
9090
"Content-Type" => "application/json",
9191
"Host" => "example.org",
@@ -101,22 +101,22 @@ class StubApp < Sinatra::Base
101101
end
102102

103103
it "can be overridden to add headers to the request" do
104-
test_client.last_request_headers["X-Custom-Header"].should eq("custom header value")
104+
test_client.request_headers["X-Custom-Header"].should eq("custom header value")
105105
end
106106
end
107107

108108
describe "setup default headers" do
109109
it "should let you set default headers when creating a new TestClient" do
110110
test_client = RspecApiDocumentation::TestClient.new(context, :headers => { "HTTP_MY_HEADER" => "hello" })
111111
test_client.get "/"
112-
test_client.last_request_headers["My-Header"].should == "hello"
113-
test_client.last_request_headers.should have(3).headers
112+
test_client.request_headers["My-Header"].should == "hello"
113+
test_client.request_headers.should have(3).headers
114114
end
115115

116116
it "should be blank if not set" do
117117
test_client = RspecApiDocumentation::TestClient.new(context)
118118
test_client.get "/"
119-
test_client.last_request_headers.should have(2).headers
119+
test_client.request_headers.should have(2).headers
120120
end
121121
end
122122

0 commit comments

Comments
 (0)