Skip to content

Commit c8bb71c

Browse files
committed
Add body and headers to request.active_resource
Closes #391 Expand the data broadcast by the `request.active_resource` Active Support Notification instrumentation event. For `GET` and `OPTION` requests, omit the `:body` value from the payload. For all other requests, include the `:body`.
1 parent 9c8a2ee commit c8bb71c

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ The `payload` is a `Hash` with the following keys:
296296

297297
* `method` as a `Symbol`
298298
* `request_uri` as a `String`
299+
* `headers` as a `Hash`
300+
* `body` as a `String` when available
299301
* `result` as an `Net::HTTPResponse`
300302

301303
## License

lib/active_resource/connection.rb

+4
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,13 @@ def head(path, headers = {})
116116
private
117117
# Makes a request to the remote service.
118118
def request(method, path, *arguments)
119+
body, headers = arguments
120+
headers, body = body, nil if headers.nil?
119121
result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload|
120122
payload[:method] = method
121123
payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
124+
payload[:headers] = headers
125+
payload[:body] = body
122126
payload[:result] = http.send(method, path, *arguments)
123127
end
124128
handle_response(result)

test/cases/notifications_test.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require "abstract_unit"
4+
require "fixtures/person"
5+
6+
class NotificationsTest < ActiveSupport::TestCase
7+
def setup
8+
matz = { person: { id: 1, name: "Matz" } }
9+
@people = { people: [ matz ] }.to_json
10+
@default_request_headers = { "Content-Type" => "application/json" }
11+
12+
ActiveResource::HttpMock.respond_to do |mock|
13+
mock.get "/people.json?name=Matz", { "Accept" => "application/json" }, @people
14+
mock.post "/people.json", { "Content-Type" => "application/json" }, nil, 201, "Location" => "/people/5.json"
15+
end
16+
end
17+
18+
def test_get_request_with_params
19+
payload = capture_notifications { Person.where(name: "Matz") }
20+
21+
assert_equal :get, payload[:method]
22+
assert_equal "http://37s.sunrise.i:3000/people.json?name=Matz", payload[:request_uri]
23+
assert_equal({ "Accept" => "application/json" }, payload[:headers])
24+
assert_nil payload[:body]
25+
assert_kind_of ActiveResource::Response, payload[:result]
26+
end
27+
28+
def test_post_request_with_body
29+
payload = capture_notifications { Person.create!(name: "Matz") }
30+
31+
assert_equal :post, payload[:method]
32+
assert_equal "http://37s.sunrise.i:3000/people.json", payload[:request_uri]
33+
assert_equal({ "Content-Type" => "application/json" }, payload[:headers])
34+
assert_equal({ "person" => { "name" => "Matz" } }.to_json, payload[:body])
35+
assert_kind_of ActiveResource::Response, payload[:result]
36+
end
37+
38+
def capture_notifications(&block)
39+
payload = nil
40+
ActiveSupport::Notifications.subscribed ->(event) { payload = event.payload }, "request.active_resource", &block
41+
payload
42+
end
43+
end

0 commit comments

Comments
 (0)