Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion test/exercise/rackup/inatra.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
module Inatra
@routes = {}

class << self
def routes
def routes(&block)
instance_eval(&block)
end

def method_missing(missing_method_name, *args, &block)
if args.length == 1
@routes[missing_method_name.to_s.upcase] ||= {}
@routes[missing_method_name.to_s.upcase][args[0]] = block
else
super
end
end

def respond_to_missing?(method_name, *_args)
@routes.include?(method_name.to_s.upcase)
end

def call(env)
request_method = env['REQUEST_METHOD']
path = env['PATH_INFO']
func = @routes[request_method][path]
func ? func.call : [404, {}, ['Not Found']]
end
end
end
8 changes: 8 additions & 0 deletions test/exercise/rackup/my_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@
get '/hello' do
[200, {}, ['Hello World']]
end

get '/ping' do
[200, {}, ['PONG']]
end

post '/bye' do
[200, {}, ['Bye Bye']]
end
end
31 changes: 25 additions & 6 deletions test/exercise/rackup/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,31 @@
require_relative './my_app'

class RackTest < Test::Unit::TestCase
def setup
@browser = Rack::Test::Session.new(Rack::MockSession.new(Inatra))
end

def test_it_says_hello_world
omit do
browser = Rack::Test::Session.new(Rack::MockSession.new(Inatra))
browser.get '/hello'
assert browser.last_response.ok?
assert_equal 'Hello World', browser.last_response.body
end
@browser.get '/hello'
assert @browser.last_response.ok?
assert_equal 'Hello World', @browser.last_response.body
end

def test_it_pongs
@browser.get '/ping'
assert @browser.last_response.ok?
assert_equal 'PONG', @browser.last_response.body
end

def test_it_says_bye
@browser.post '/bye'
assert @browser.last_response.ok?
assert_equal 'Bye Bye', @browser.last_response.body
end

def test_it_handles_404
@browser.get '/missing_method'
assert @browser.last_response.not_found?
assert_equal 'Not Found', @browser.last_response.body
end
end