Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug where ActiveResource::HttpMock would overwrite Accept/Content…
…-Type header to application/xml [#4939 state:resolved] Signed-off-by: José Valim <jose.valim@gmail.com>
- Loading branch information
Showing
2 changed files
with
85 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
require 'abstract_unit' | ||
|
||
class HttpMockTest < ActiveSupport::TestCase | ||
setup do | ||
@http = ActiveResource::HttpMock.new("http://example.com") | ||
end | ||
|
||
FORMAT_HEADER = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES | ||
|
||
[:post, :put, :get, :delete, :head].each do |method| | ||
test "responds to simple #{method} request" do | ||
ActiveResource::HttpMock.respond_to do |mock| | ||
mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "Response") | ||
end | ||
|
||
assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body | ||
end | ||
|
||
test "adds format header by default to #{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/xml").body | ||
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| | ||
mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "XML") | ||
mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/json"}, "Json") | ||
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 | ||
end | ||
|
||
def request(method, path, headers = {}, body = nil) | ||
if [:put, :post].include? method | ||
@http.send(method, path, body, headers) | ||
else | ||
@http.send(method, path, headers) | ||
end | ||
end | ||
end |