aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEileen M. Uchitelle <eileencodes@users.noreply.github.com>2018-12-17 10:24:38 -0500
committerGitHub <noreply@github.com>2018-12-17 10:24:38 -0500
commit02b931c764cca4c3f67b1decfc046bfb46dc510c (patch)
tree04b25f1e0daf1e3f4bf71705bd08fa5dfa51c870
parent07ec8062e605ba4e9bd153e1d264b02ac4ab8a0f (diff)
parent048e3172f51db1fddd03b89f676d96a443539a13 (diff)
downloadrails-02b931c764cca4c3f67b1decfc046bfb46dc510c.tar.gz
rails-02b931c764cca4c3f67b1decfc046bfb46dc510c.tar.bz2
rails-02b931c764cca4c3f67b1decfc046bfb46dc510c.zip
Merge branch 'master' into host-authorization
-rw-r--r--actionpack/CHANGELOG.md18
-rw-r--r--actionpack/lib/action_dispatch/testing/test_response.rb11
-rw-r--r--actionpack/test/controller/test_case_test.rb14
-rw-r--r--activesupport/lib/active_support/notifications.rb9
-rw-r--r--activesupport/test/notifications_test.rb2
-rw-r--r--guides/source/active_support_instrumentation.md14
6 files changed, 59 insertions, 9 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md
index 066e92fec3..13fbbafc0c 100644
--- a/actionpack/CHANGELOG.md
+++ b/actionpack/CHANGELOG.md
@@ -8,6 +8,24 @@
*Genadi Samokovarov*
+* Allow using `parsed_body` in `ActionController::TestCase`.
+
+ In addition to `ActionDispatch::IntegrationTest`, allow using
+ `parsed_body` in `ActionController::TestCase`:
+
+ ```
+ class SomeControllerTest < ActionController::TestCase
+ def test_some_action
+ post :action, body: { foo: 'bar' }
+ assert_equal({ "foo" => "bar" }, response.parsed_body)
+ end
+ end
+ ```
+
+ Fixes #34676.
+
+ *Tobias Bühlmann*
+
* Raise an error on root route naming conflicts.
Raises an ArgumentError when multiple root routes are defined in the
diff --git a/actionpack/lib/action_dispatch/testing/test_response.rb b/actionpack/lib/action_dispatch/testing/test_response.rb
index 1e6b21f235..7c1202dc0e 100644
--- a/actionpack/lib/action_dispatch/testing/test_response.rb
+++ b/actionpack/lib/action_dispatch/testing/test_response.rb
@@ -14,11 +14,6 @@ module ActionDispatch
new response.status, response.headers, response.body
end
- def initialize(*) # :nodoc:
- super
- @response_parser = RequestEncoder.parser(content_type)
- end
-
# Was the response successful?
def success?
ActiveSupport::Deprecation.warn(<<-MSG.squish)
@@ -47,7 +42,11 @@ module ActionDispatch
end
def parsed_body
- @parsed_body ||= @response_parser.call(body)
+ @parsed_body ||= response_parser.call(body)
+ end
+
+ def response_parser
+ @response_parser ||= RequestEncoder.parser(content_type)
end
end
end
diff --git a/actionpack/test/controller/test_case_test.rb b/actionpack/test/controller/test_case_test.rb
index 6fc70d6248..c931e2daac 100644
--- a/actionpack/test/controller/test_case_test.rb
+++ b/actionpack/test/controller/test_case_test.rb
@@ -156,6 +156,10 @@ XML
render html: '<body class="foo"></body>'.html_safe
end
+ def render_json
+ render json: request.raw_post
+ end
+
def boom
raise "boom!"
end
@@ -965,6 +969,16 @@ XML
assert_equal "q=test2", @response.body
end
+
+ def test_parsed_body_without_as_option
+ post :render_json, body: { foo: "heyo" }
+ assert_equal({ "foo" => "heyo" }, response.parsed_body)
+ end
+
+ def test_parsed_body_with_as_option
+ post :render_json, body: { foo: "heyo" }, as: :json
+ assert_equal({ "foo" => "heyo" }, response.parsed_body)
+ end
end
class ResponseDefaultHeadersTest < ActionController::TestCase
diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb
index 01cc363e2b..0ff32bd810 100644
--- a/activesupport/lib/active_support/notifications.rb
+++ b/activesupport/lib/active_support/notifications.rb
@@ -177,7 +177,7 @@ module ActiveSupport
# names, or by passing a Regexp to match all events that match a pattern.
#
# ActiveSupport::Notifications.subscribe(/render/) do |*args|
- # ...
+ # @event = ActiveSupport::Notifications::Event.new(*args)
# end
#
# The +block+ will receive five parameters with information about the event:
@@ -189,6 +189,13 @@ module ActiveSupport
# id # => String, unique ID for the instrumenter that fired the event
# payload # => Hash, the payload
# end
+ #
+ # If the block passed to the method only takes one parameter,
+ # it will yield an event object to the block:
+ #
+ # ActiveSupport::Notifications.subscribe(/render/) do |event|
+ # @event = event
+ # end
def subscribe(*args, &block)
notifier.subscribe(*args, &block)
end
diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb
index 54fd4345fb..4e0aef2cc7 100644
--- a/activesupport/test/notifications_test.rb
+++ b/activesupport/test/notifications_test.rb
@@ -26,7 +26,7 @@ module Notifications
end
end
- class SubscribeEventObjects < TestCase
+ class SubscribeEventObjectsTest < TestCase
def test_subscribe_events
events = []
@notifier.subscribe do |event|
diff --git a/guides/source/active_support_instrumentation.md b/guides/source/active_support_instrumentation.md
index 64db141381..5e68b3f400 100644
--- a/guides/source/active_support_instrumentation.md
+++ b/guides/source/active_support_instrumentation.md
@@ -648,6 +648,18 @@ ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*a
end
```
+You may also pass block with only one argument, it will yield an event object to the block:
+
+```ruby
+ActiveSupport::Notifications.subscribe "process_action.action_controller" do |event|
+ event.name # => "process_action.action_controller"
+ event.duration # => 10 (in milliseconds)
+ event.payload # => {:extra=>information}
+
+ Rails.logger.info "#{event} Received!"
+end
+```
+
Most times you only care about the data itself. Here is a shortcut to just get the data.
```ruby
@@ -672,7 +684,7 @@ Creating custom events
Adding your own events is easy as well. `ActiveSupport::Notifications` will take care of
all the heavy lifting for you. Simply call `instrument` with a `name`, `payload` and a block.
The notification will be sent after the block returns. `ActiveSupport` will generate the start and end times
-and add the instrumenter's unique ID. All data passed into the `instrument` call will make
+and add the instrumenter's unique ID. All data passed into the `instrument` call will make
it into the payload.
Here's an example: