forked from rails/activeresource
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
body
and headers
to request.active_resource
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
1 parent
9c8a2ee
commit 1770501
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |