Skip to content

Commit

Permalink
add an assert_params helper
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Nov 17, 2011
1 parent e039922 commit 59212bd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
3 changes: 1 addition & 2 deletions services/bamboo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def authenticated
end

def login
res = http_post "api/rest/login.action",
"username=#{CGI.escape(username)}&password=#{CGI.escape(password)}"
res = http_post "api/rest/login.action", :username => username, :password => password
case res.status
when 200..204
XmlSimple.xml_in(res.body)['auth'].first
Expand Down
35 changes: 26 additions & 9 deletions test/bamboo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def setup

def test_triggers_build
@stubs.post "/api/rest/login.action" do |env|
assert_equal "username=admin&password=pwd", env[:body]
assert_params env[:body], :username => 'admin', :password => 'pwd'
[200, {}, '<response><auth>TOKEN123</auth></response>']
end
@stubs.post "/api/rest/executeBuild.action" do |env|
assert_equal "auth=TOKEN123&buildKey=ABC", env[:body]
assert_params env[:body], :auth => "TOKEN123", :buildKey => "ABC"
[200, {}, '<response></response>']
end
@stubs.post "/api/rest/logout.action" do |env|
Expand All @@ -34,15 +34,15 @@ def test_triggers_build

def test_triggers_compound_build
@stubs.post "/api/rest/login.action" do |env|
assert_equal "username=admin&password=pwd", env[:body]
assert_params env[:body], :username => 'admin', :password => 'pwd'
[200, {}, '<response><auth>TOKEN123</auth></response>']
end
@stubs.post "/api/rest/executeBuild.action" do |env|
assert_equal "auth=TOKEN123&buildKey=ABC", env[:body]
assert_params env[:body], :auth => "TOKEN123", :buildKey => "ABC"
[200, {}, '<response></response>']
end
@stubs.post "/api/rest/executeBuild.action" do |env|
assert_equal "auth=TOKEN123&buildKey=A", env[:body]
assert_params env[:body], :auth => "TOKEN123", :buildKey => "A"
[200, {}, '<response></response>']
end
@stubs.post "/api/rest/logout.action" do |env|
Expand All @@ -58,11 +58,11 @@ def test_triggers_compound_build

def test_triggers_build_with_context_path
@stubs.post "/context/api/rest/login.action" do |env|
assert_equal "username=admin&password=pwd", env[:body]
assert_params env[:body], :username => 'admin', :password => 'pwd'
[200, {}, '<response><auth>TOKEN123</auth></response>']
end
@stubs.post "/context/api/rest/executeBuild.action" do |env|
assert_equal "auth=TOKEN123&buildKey=ABC", env[:body]
assert_params env[:body], :auth => "TOKEN123", :buildKey => "ABC"
[200, {}, '<response></response>']
end
@stubs.post "/context/api/rest/logout.action" do |env|
Expand All @@ -79,11 +79,11 @@ def test_triggers_build_with_context_path

def test_passes_build_error
@stubs.post "/api/rest/login.action" do |env|
assert_equal "username=admin&password=pwd", env[:body]
assert_params env[:body], :username => 'admin', :password => 'pwd'
[200, {}, '<response><auth>TOKEN123</auth></response>']
end
@stubs.post "/api/rest/executeBuild.action" do |env|
assert_equal "auth=TOKEN123&buildKey=ABC", env[:body]
assert_params env[:body], :auth => "TOKEN123", :buildKey => "ABC"
[200, {}, '<response><error>oh hai</error></response>']
end
@stubs.post "/api/rest/logout.action" do |env|
Expand Down Expand Up @@ -188,6 +188,23 @@ def compound_data1
}
end

# Assert the value of the params.
#
# body - A String of form-encoded params: "a=1&b=2"
# expected - A Hash of String keys and values to match against the body.
#
# Raises Test::Unit::AssertionFailedError if the assertion doesn't match.
# Returns nothing.
def assert_params(body, expected)
params = Rack::Utils.parse_query(body)
expected.each do |key, expected_value|
assert value = params.delete(key.to_s), "#{key} not in #{params.inspect}"
assert_equal expected_value, value, "#{key} = #{value.inspect}, not #{expected_value.inspect}"
end

assert params.empty?, "params has other values: #{params.inspect}"
end

def service(*args)
super Service::Bamboo, *args
end
Expand Down

0 comments on commit 59212bd

Please sign in to comment.