|
| 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