Skip to content

Commit

Permalink
Added spec for rack routes. The 'uses API middleware' spec is failing…
Browse files Browse the repository at this point in the history
… because middleware for a routed API is not being used.
  • Loading branch information
alexkwolfe committed Apr 27, 2011
1 parent 9be3c62 commit 2aa199a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
19 changes: 18 additions & 1 deletion examples/rack_routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# encoding: utf-8
$:<< '../lib' << 'lib'

require 'goliath'
Expand All @@ -25,7 +26,19 @@ def response(env)
end
end

class Hola < Goliath::API
use Goliath::Rack::Params
use Goliath::Rack::ValidationError
use Goliath::Rack::Validation::RequestMethod, %w(GET)

def response(env)
[200, {}, "¡hola #{env.params['name']}!"]
end
end

class RackRoutes < Goliath::API


map '/version' do
run Proc.new { |env| [200, {"Content-Type" => "text/html"}, ["Version 0.1"]] }
end
Expand All @@ -37,8 +50,12 @@ class RackRoutes < Goliath::API
map "/bonjour" do
run Bonjour.new
end

map "/hola" do
run Hola.new
end

map '/' do
run Proc.new { |env| [404, {"Content-Type" => "text/html"}, ["Try /version /hello_world or /bonjour"]] }
run Proc.new { |env| [404, {"Content-Type" => "text/html"}, ["Try /version /hello_world, /bonjour, or /hola"]] }
end
end
4 changes: 4 additions & 0 deletions lib/goliath/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,13 @@ def call(env)
end

rescue Goliath::Validation::Error => e
puts e.message
puts e.backtrace
env[ASYNC_CALLBACK].call([e.status_code, {}, {:error => e.message}])

rescue Exception => e
puts e.message
puts e.backtrace
env.logger.error(e.message)
env.logger.error(e.backtrace.join("\n"))

Expand Down
13 changes: 12 additions & 1 deletion lib/goliath/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,18 @@ def build_app(klass)
klass.middlewares.each do |mw|
use(*(mw[0..1].compact), &mw[2])
end
run klass.new

# If you use map you can't use run as
# the rack builder will blowup.
if klass.maps.empty?
run klass.new
else
klass.maps.each do |mp|
map(mp.first, &mp.last)
end
end

klass
end
end

Expand Down
35 changes: 35 additions & 0 deletions spec/integration/rack_routes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# encoding: utf-8
require 'spec_helper'
require File.join(File.dirname(__FILE__), '../../', 'examples/rack_routes')

describe RackRoutes do
let(:err) { Proc.new { fail "API request failed" } }

it 'routes to the correct API' do
with_api(RackRoutes) do
get_request({:path => '/bonjour'}, err) do |c|
c.response_header.status.should == 200
c.response.should == 'bonjour!'
end
end
end

it 'routes to the default' do
with_api(RackRoutes) do
get_request({:path => '/donkey'}, err) do |c|
c.response_header.status.should == 404
c.response.should == 'Try /version /hello_world, /bonjour, or /hola'
end
end
end

it 'uses API middleware' do
with_api(RackRoutes) do
post_request({:path => '/hola'}, err) do |c|
# the /hola route only supports GET requests
c.response_header.status.should == 400
c.response.should == '[:error, "Invalid request method"]'
end
end
end
end

0 comments on commit 2aa199a

Please sign in to comment.