Skip to content

Commit faebdcb

Browse files
committed
Add request_method, request_headers, and request_body for callback assertions.
1 parent 8f987ee commit faebdcb

File tree

4 files changed

+22
-72
lines changed

4 files changed

+22
-72
lines changed

features/callbacks.feature

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
Feature: Document callbacks
22

33
Background:
4-
Given a file named "app.rb" with:
4+
Given a file named "app_spec.rb" with:
55
"""
6-
class App
7-
def self.call(env)
6+
require "rspec_api_documentation"
7+
require "rspec_api_documentation/dsl"
8+
9+
RspecApiDocumentation.configure do |config|
10+
config.app = lambda do
811
uri = URI.parse("http://example.net/callback")
912
Net::HTTP.start(uri.host, uri.port) do |http|
1013
request = Net::HTTP::Post.new(uri.path)
@@ -16,31 +19,26 @@ Feature: Document callbacks
1619
[200, {}, []]
1720
end
1821
end
19-
"""
20-
And a file named "app_spec.rb" with:
21-
"""
22-
require "rspec_api_documentation"
23-
require "rspec_api_documentation/dsl"
24-
25-
RspecApiDocumentation.configure do |config|
26-
config.app = App
27-
end
2822
2923
resource "Interesting Thing" do
3024
callback "/interesting_thing" do
3125
let(:callback_url) { "http://example.net/callback" }
3226
3327
trigger_callback do
34-
app.call({})
28+
app.call
3529
end
3630
3731
example "Receiving a callback when interesting things happen" do
3832
do_callback
33+
request_method.should eq("POST")
34+
request_headers["Content-Type"].should eq("application/json")
35+
request_headers["User-Agent"].should eq("InterestingThingApp")
36+
request_body.should eq('{"message":"Something interesting happened!"}')
3937
end
4038
end
4139
end
4240
"""
43-
When I run `rspec app_spec.rb --require ./app.rb --format RspecApiDocumentation::ApiFormatter`
41+
When I run `rspec app_spec.rb --format RspecApiDocumentation::ApiFormatter`
4442

4543
Scenario: Output helpful progress to the console
4644
Then the output should contain:

lib/rspec_api_documentation/dsl.rb

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

99
delegate :last_response_headers, :status, :response_body, :to => :client
10+
delegate :request_method, :request_headers, :request_body, :to => :destination
1011

1112
module ClassMethods
1213
def self.define_action(method)

lib/rspec_api_documentation/test_server.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@ class TestServer < Struct.new(:context)
33
include Headers
44
include Syntax
55

6-
delegate :example, :last_request, :last_response, :to => :context
6+
delegate :example, :to => :context
77
delegate :metadata, :to => :example
88

9+
attr_reader :request_method, :request_headers, :request_body
10+
911
def call(env)
1012
input = env["rack.input"]
1113
input.rewind
12-
request_body = input.read
1314

14-
headers = env_to_headers(env)
15+
@request_method = env["REQUEST_METHOD"]
16+
@request_headers = env_to_headers(env)
17+
@request_body = input.read
1518

1619
request_metadata = {}
1720

18-
request_metadata[:request_method] = env["REQUEST_METHOD"]
21+
request_metadata[:request_method] = request_method
1922
request_metadata[:request_path] = env["PATH_INFO"]
20-
request_metadata[:request_body] = highlight_syntax(request_body, headers["Content-Type"], true)
21-
request_metadata[:request_headers] = format_headers(headers)
23+
request_metadata[:request_body] = highlight_syntax(request_body, request_headers["Content-Type"], true)
24+
request_metadata[:request_headers] = format_headers(@request_headers)
2225

2326
metadata[:requests] ||= []
2427
metadata[:requests] << request_metadata

spec/test_server_spec.rb

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)