2010-06-22 18:50:15 -04:00
|
|
|
require 'abstract_unit'
|
2011-04-10 12:52:42 -04:00
|
|
|
require 'active_support/core_ext/object/inclusion'
|
2010-06-22 18:50:15 -04:00
|
|
|
|
|
|
|
class HttpMockTest < ActiveSupport::TestCase
|
|
|
|
setup do
|
|
|
|
@http = ActiveResource::HttpMock.new("http://example.com")
|
|
|
|
end
|
|
|
|
|
|
|
|
FORMAT_HEADER = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES
|
|
|
|
|
2011-05-06 17:03:55 -04:00
|
|
|
[:post, :patch, :put, :get, :delete, :head].each do |method|
|
2010-06-22 18:50:15 -04:00
|
|
|
test "responds to simple #{method} request" do
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/json" }, "Response")
|
2010-06-22 18:50:15 -04:00
|
|
|
end
|
|
|
|
|
2011-05-17 19:50:34 -04:00
|
|
|
assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
|
2010-06-22 18:50:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "adds format header by default to #{method} request" do
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
|
|
|
mock.send(method, "/people/1", {}, "Response")
|
|
|
|
end
|
|
|
|
|
2011-05-17 19:50:34 -04:00
|
|
|
assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
|
2010-06-22 18:50:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "respond only when headers match header by default to #{method} request" do
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
|
|
|
mock.send(method, "/people/1", {"X-Header" => "X"}, "Response")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "Response", request(method, "/people/1", "X-Header" => "X").body
|
|
|
|
assert_raise(ActiveResource::InvalidRequestError) { request(method, "/people/1") }
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not overwrite format header to #{method} request" do
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
|
|
|
mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/json"}, "Response")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
|
|
|
|
end
|
|
|
|
|
|
|
|
test "ignores format header when there is only one response to same url in a #{method} request" do
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
|
|
|
mock.send(method, "/people/1", {}, "Response")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
|
|
|
|
assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body
|
|
|
|
end
|
|
|
|
|
|
|
|
test "responds correctly when format header is given to #{method} request" do
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/xml" }, "XML")
|
|
|
|
mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/json" }, "Json")
|
2010-06-22 18:50:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "XML", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body
|
|
|
|
assert_equal "Json", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
|
|
|
|
end
|
2010-08-24 22:05:23 -04:00
|
|
|
|
|
|
|
test "raises InvalidRequestError if no response found for the #{method} request" do
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/json" }, "json")
|
2010-08-24 22:05:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise(::ActiveResource::InvalidRequestError) do
|
2011-05-17 19:50:34 -04:00
|
|
|
request(method, "/people/1", FORMAT_HEADER[method] => "application/xml")
|
2010-08-24 22:05:23 -04:00
|
|
|
end
|
|
|
|
end
|
2010-10-06 18:14:12 -04:00
|
|
|
|
2010-06-22 18:50:15 -04:00
|
|
|
end
|
|
|
|
|
2010-08-30 01:35:15 -04:00
|
|
|
test "allows you to send in pairs directly to the respond_to method" do
|
2011-05-17 19:50:34 -04:00
|
|
|
matz = { :person => { :id => 1, :name => "Matz" } }.to_json
|
2010-10-06 18:14:12 -04:00
|
|
|
|
2011-05-17 19:50:34 -04:00
|
|
|
create_matz = ActiveResource::Request.new(:post, '/people.json', matz, {})
|
|
|
|
created_response = ActiveResource::Response.new("", 201, { "Location" => "/people/1.json" })
|
|
|
|
get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil)
|
2010-08-30 01:35:15 -04:00
|
|
|
ok_response = ActiveResource::Response.new(matz, 200, {})
|
2010-10-06 18:14:12 -04:00
|
|
|
|
2010-08-30 01:35:15 -04:00
|
|
|
pairs = {create_matz => created_response, get_matz => ok_response}
|
2010-10-06 18:14:12 -04:00
|
|
|
|
2010-08-30 01:35:15 -04:00
|
|
|
ActiveResource::HttpMock.respond_to(pairs)
|
|
|
|
assert_equal 2, ActiveResource::HttpMock.responses.length
|
|
|
|
assert_equal "", ActiveResource::HttpMock.responses.assoc(create_matz)[1].body
|
|
|
|
assert_equal matz, ActiveResource::HttpMock.responses.assoc(get_matz)[1].body
|
|
|
|
end
|
|
|
|
|
2010-08-30 01:35:36 -04:00
|
|
|
test "resets all mocked responses on each call to respond_to with a block by default" do
|
2010-08-30 01:35:15 -04:00
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(:get, "/people/1", {}, "JSON1")
|
2010-08-30 01:35:15 -04:00
|
|
|
end
|
|
|
|
assert_equal 1, ActiveResource::HttpMock.responses.length
|
|
|
|
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(:get, "/people/2", {}, "JSON2")
|
2010-08-30 01:35:15 -04:00
|
|
|
end
|
|
|
|
assert_equal 1, ActiveResource::HttpMock.responses.length
|
|
|
|
end
|
|
|
|
|
2010-08-30 01:35:36 -04:00
|
|
|
test "resets all mocked responses on each call to respond_to by passing pairs by default" do
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(:get, "/people/1", {}, "JSON1")
|
2010-08-30 01:35:36 -04:00
|
|
|
end
|
|
|
|
assert_equal 1, ActiveResource::HttpMock.responses.length
|
|
|
|
|
2011-05-17 19:50:34 -04:00
|
|
|
matz = { :person => { :id => 1, :name => "Matz" } }.to_json
|
|
|
|
get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil)
|
2010-08-30 01:35:36 -04:00
|
|
|
ok_response = ActiveResource::Response.new(matz, 200, {})
|
|
|
|
ActiveResource::HttpMock.respond_to({get_matz => ok_response})
|
|
|
|
|
|
|
|
assert_equal 1, ActiveResource::HttpMock.responses.length
|
|
|
|
end
|
|
|
|
|
|
|
|
test "allows you to add new responses to the existing responses by calling a block" do
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(:get, "/people/1", {}, "JSON1")
|
2010-08-30 01:35:36 -04:00
|
|
|
end
|
|
|
|
assert_equal 1, ActiveResource::HttpMock.responses.length
|
|
|
|
|
|
|
|
ActiveResource::HttpMock.respond_to(false) do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(:get, "/people/2", {}, "JSON2")
|
2010-08-30 01:35:36 -04:00
|
|
|
end
|
|
|
|
assert_equal 2, ActiveResource::HttpMock.responses.length
|
|
|
|
end
|
|
|
|
|
|
|
|
test "allows you to add new responses to the existing responses by passing pairs" do
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(:get, "/people/1", {}, "JSON1")
|
2010-08-30 01:35:36 -04:00
|
|
|
end
|
|
|
|
assert_equal 1, ActiveResource::HttpMock.responses.length
|
|
|
|
|
2011-05-17 19:50:34 -04:00
|
|
|
matz = { :person => { :id => 1, :name => "Matz" } }.to_json
|
|
|
|
get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil)
|
2010-08-30 01:35:36 -04:00
|
|
|
ok_response = ActiveResource::Response.new(matz, 200, {})
|
|
|
|
ActiveResource::HttpMock.respond_to({get_matz => ok_response}, false)
|
|
|
|
|
|
|
|
assert_equal 2, ActiveResource::HttpMock.responses.length
|
|
|
|
end
|
|
|
|
|
2011-01-18 03:21:16 -05:00
|
|
|
test "allows you to replace the existing reponse with the same request by calling a block" do
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(:get, "/people/1", {}, "JSON1")
|
2011-01-18 03:21:16 -05:00
|
|
|
end
|
|
|
|
assert_equal 1, ActiveResource::HttpMock.responses.length
|
|
|
|
|
|
|
|
ActiveResource::HttpMock.respond_to(false) do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(:get, "/people/1", {}, "JSON2")
|
2011-01-18 03:21:16 -05:00
|
|
|
end
|
|
|
|
assert_equal 1, ActiveResource::HttpMock.responses.length
|
|
|
|
end
|
|
|
|
|
|
|
|
test "allows you to replace the existing reponse with the same request by passing pairs" do
|
2010-10-06 18:14:12 -04:00
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(:get, "/people/1", {}, "JSON1")
|
2010-10-06 18:14:12 -04:00
|
|
|
end
|
|
|
|
assert_equal 1, ActiveResource::HttpMock.responses.length
|
|
|
|
|
2011-05-17 19:50:34 -04:00
|
|
|
matz = { :person => { :id => 1, :name => "Matz" } }.to_json
|
2010-10-06 18:14:12 -04:00
|
|
|
get_matz = ActiveResource::Request.new(:get, '/people/1', nil)
|
|
|
|
ok_response = ActiveResource::Response.new(matz, 200, {})
|
|
|
|
|
|
|
|
ActiveResource::HttpMock.respond_to({get_matz => ok_response}, false)
|
2011-01-18 03:21:16 -05:00
|
|
|
assert_equal 1, ActiveResource::HttpMock.responses.length
|
|
|
|
end
|
2010-10-06 18:14:12 -04:00
|
|
|
|
2011-01-18 03:21:16 -05:00
|
|
|
test "do not replace the response with the same path but different method by calling a block" do
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(:get, "/people/1", {}, "JSON1")
|
2011-01-18 03:21:16 -05:00
|
|
|
end
|
2010-10-06 18:14:12 -04:00
|
|
|
assert_equal 1, ActiveResource::HttpMock.responses.length
|
2011-01-18 03:21:16 -05:00
|
|
|
|
|
|
|
ActiveResource::HttpMock.respond_to(false) do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(:put, "/people/1", {}, "JSON2")
|
2011-01-18 03:21:16 -05:00
|
|
|
end
|
|
|
|
assert_equal 2, ActiveResource::HttpMock.responses.length
|
2010-10-06 18:14:12 -04:00
|
|
|
end
|
|
|
|
|
2011-01-18 03:21:16 -05:00
|
|
|
test "do not replace the response with the same path but different method by passing pairs" do
|
2010-10-06 18:21:01 -04:00
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:50:34 -04:00
|
|
|
mock.send(:get, "/people/1", {}, "JSON1")
|
2010-10-06 18:21:01 -04:00
|
|
|
end
|
|
|
|
assert_equal 1, ActiveResource::HttpMock.responses.length
|
|
|
|
|
|
|
|
put_matz = ActiveResource::Request.new(:put, '/people/1', nil)
|
|
|
|
ok_response = ActiveResource::Response.new("", 200, {})
|
|
|
|
|
|
|
|
ActiveResource::HttpMock.respond_to({put_matz => ok_response}, false)
|
|
|
|
assert_equal 2, ActiveResource::HttpMock.responses.length
|
|
|
|
end
|
|
|
|
|
2010-06-22 18:50:15 -04:00
|
|
|
def request(method, path, headers = {}, body = nil)
|
2011-05-06 17:03:55 -04:00
|
|
|
if method.in?([:patch, :put, :post])
|
2010-06-22 18:50:15 -04:00
|
|
|
@http.send(method, path, body, headers)
|
|
|
|
else
|
|
|
|
@http.send(method, path, headers)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|