Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add body and headers to request.active_resource notification #413

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
42 changes: 42 additions & 0 deletions test/cases/notifications_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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

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]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion is a quirk of how ActiveResource::HttpMock.respond_to works. In reality, the payload[:result] kind is a Net::HTTPResponse, but in the test harness it's an ActiveResource::Response.

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
Loading