Skip to content

Instantly share code, notes, and snippets.

@simi
Created March 19, 2012 10:59
Show Gist options
  • Select an option

  • Save simi/2107564 to your computer and use it in GitHub Desktop.

Select an option

Save simi/2107564 to your computer and use it in GitHub Desktop.
jimson-custom test
class TestJimsonEom < MiniTest::Unit::TestCase
include Rack::Test::Methods
attr_accessor :handler
module TestMethods
extend Jimson::Eom::Methods
method :register_user do |call|
call.attributes :email, :password
call.respond_with call.arguments
end
method :only_token do |call|
call.respond_with call.access_token
end
end
class TestHandler
include TestMethods
extend Jimson::Handler
end
def app
Jimson::Server.new(TestHandler.new, :environment => "production")
end
def post_json(hash)
post '/', MultiJson.encode(hash), {'Content-Type' => 'application/json'}
end
def setup
self.handler = TestHandler.new
end
def test_valid_response
req = {
'jsonrpc' => '2.0',
'method' => 'register_user',
'params' => {:email => 'retro@ballgag.cz', :password => 'retro'},
'id' => 1
}
post_json(req)
assert_equal 200, last_response.status
response = {
'jsonrpc' => '2.0',
'result' => {'email' => 'retro@ballgag.cz', 'password' => 'retro'},
'id' => 1
}
assert_equal response, MultiJson.decode(last_response.body)
end
def test_method_only_token
arguments = {:accessToken => 'a'}
assert_equal 'a', handler.only_token(arguments)
end
def test_method_invalid_arguments
arguments = {:email => 'retro@ballgag.cz', :password_confirm => 'retro'}
assert_raises(RuntimeError) {handler.register_user(arguments)}
end
def test_method_valid_arguments
arguments = {:email => 'retro@ballgag.cz', :password => 'retro'}
assert_equal arguments, handler.register_user(arguments)
end
def test_with_access_token
arguments = {:email => 'retro@ballgag.cz', :password => 'retro'}
assert_equal arguments, handler.register_user(arguments.merge({:accessToken => 'A'}))
end
def test_nested_hash_format
arguments = {:email => {:userEmail => 'retro@ballgag.cz'}, :password => 'retro'}
expected_arguments = {:email => {:user_email => 'retro@ballgag.cz'}, :password => 'retro'}
assert_equal expected_arguments, handler.register_user(arguments)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment