Skip to content

Commit

Permalink
Add body and headers to request.active_resource
Browse files Browse the repository at this point in the history
Closes rails#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`.
  • Loading branch information
seanpdoyle committed Jan 5, 2025
1 parent 9c8a2ee commit 1770501
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ The `payload` is a `Hash` with the following keys:

* `method` as a `Symbol`
* `request_uri` as a `String`
* `headers` as a `Hash`
* `body` as a `String` when available
* `result` as an `Net::HTTPResponse`

## License
Expand Down
4 changes: 4 additions & 0 deletions lib/active_resource/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,13 @@ def head(path, headers = {})
private
# Makes a request to the remote service.
def request(method, path, *arguments)
body, headers = arguments
headers, body = body, nil if headers.nil?
result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload|
payload[:method] = method
payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
payload[:headers] = headers
payload[:body] = body
payload[:result] = http.send(method, path, *arguments)
end
handle_response(result)
Expand Down
43 changes: 43 additions & 0 deletions test/cases/notifications_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require "abstract_unit"
require "fixtures/person"

class NotificationsTest < ActiveSupport::TestCase
def setup
matz = { person: { id: 1, name: "Matz" } }
@people = { people: [ matz ] }.to_json
@default_request_headers = { "Content-Type" => "application/json" }

ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people.json?name=Matz", { "Accept" => "application/json" }, @people
mock.post "/people.json", { "Content-Type" => "application/json" }, nil, 201, "Location" => "/people/5.json"
end
end

def test_get_request_with_params
payload = capture_notifications { Person.where(name: "Matz") }

assert_equal :get, payload[:method]
assert_equal "http://37s.sunrise.i:3000/people.json?name=Matz", payload[:request_uri]
assert_equal({ "Accept" => "application/json" }, payload[:headers])
assert_nil payload[:body]
assert_kind_of ActiveResource::Response, payload[:result]
end

def test_post_request_with_body
payload = capture_notifications { Person.create!(name: "Matz") }

assert_equal :post, payload[:method]
assert_equal "http://37s.sunrise.i:3000/people.json", payload[:request_uri]
assert_equal({ "Content-Type" => "application/json" }, payload[:headers])
assert_equal({ "person" => { "name" => "Matz" } }.to_json, payload[:body])
assert_kind_of ActiveResource::Response, payload[:result]
end

def capture_notifications(&block)
payload = nil
ActiveSupport::Notifications.subscribed ->(event) { payload = event.payload }, "request.active_resource", &block
payload
end
end

0 comments on commit 1770501

Please sign in to comment.