Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Try handlers one by one until one matches

  • Loading branch information...
commit d334e9c96b64b4e14ff04860c97ca12a3a7bbde8 1 parent d449058
@ttencate authored
View
9 lib/blimp/handler.rb
@@ -1,6 +1,11 @@
module Blimp
class Handler
- class NotFound < StandardError; end
+ class HandlerNotFound < StandardError; end
+ class SourceNotFound < StandardError; end
+
+ def name
+ return @NAME
+ end
class << self
def find_by_name(name)
@@ -8,7 +13,7 @@ def find_by_name(name)
handler = Blimp::Handlers.const_get(constant)
return handler if handler < Blimp::Handler && handler::NAME == name
end
- raise NotFound, "No handler found for name #{name}"
+ raise HandlerNotFound, "No handler found for name #{name}"
end
end
View
6 lib/blimp/handlers/page_handler.rb
@@ -4,7 +4,11 @@ class PageHandler < Blimp::Handler
NAME = "page"
def handle(source, theme, path, params = {})
- resource = Page.from_path(path, source)
+ begin
+ resource = Page.from_path(path, source)
+ rescue Page::NotFound
+ raise SourceNotFound
+ end
headers = {"Content-Type" => "text/html"}
body = theme.render(resource.body)
View
6 lib/blimp/handlers/static_handler.rb
@@ -4,7 +4,11 @@ class StaticHandler < Blimp::Handler
NAME = "static"
def handle(source, theme, path, params = {})
- resource = Static.from_path(path, source)
+ begin
+ resource = Static.from_path(path, source)
+ rescue Static::NotFound
+ raise SourceNotFound
+ end
headers = {"Content-Type" => resource.mimetype}
body = resource.contents
View
19 lib/blimp/site.rb
@@ -2,7 +2,7 @@
class Site
class InvalidConfig < StandardError; end
- class NoHandler < StandardError; end
+ class NotFound < StandardError; end
CONFIG_FILE_PATH = "/_blimp.yaml"
attr_reader :key
@@ -23,12 +23,17 @@ def initialize(key, source, options = {})
@router = Router.new(config["handlers"])
end
- def handlers_for_path(path)
- router.handlers_for_path(path)
- end
-
- def find_page(path)
- Page.from_path(path, source)
+ def handle_request(path, params = {})
+ handlers = router.handlers_for_path(path)
+ for handler in handlers do
+ begin
+ response = handler.handle(source, theme, path, params)
+ rescue Blimp::Handler::SourceNotFound
+ raise NotFound, "Handler #{handler.name} matched but could not find source for #{path}"
+ end
+ return response if response
+ end
+ raise NotFound, "No handler handled URL path #{path}"
end
def has_domain?(domain)
View
8 lib/blimp/web_server.rb
@@ -16,9 +16,11 @@ class WebServer < Sinatra::Base
end
get '*' do
- handlers = site.handlers_for_path(request.path_info)
- # TODO try them one by one, raise Sinatra::NotFound if all fail
- response_headers, response_body = handlers[0].handle(site.source, site.theme, request.path_info, params)
+ begin
+ response_headers, response_body = site.handle_request(request.path_info, params)
+ rescue Site::NotFound
+ raise Sinatra::NotFound
+ end
headers response_headers
body response_body
View
2  spec/blimp/handler_spec.rb
@@ -9,6 +9,6 @@
it 'should raise on unknown handlers' do
expect {
Blimp::Handler.find_by_name("unknown")
- }.to raise_error(Blimp::Handler::NotFound)
+ }.to raise_error(Blimp::Handler::HandlerNotFound)
end
end
View
14 spec/blimp/handlers/page_handler_spec.rb
@@ -49,5 +49,19 @@
body.should include("<li>My list</li>")
end
end
+
+ context "for a handlable URL whose file does not exist" do
+ it "should raise" do
+ expect {
+ handler.handle(source, theme, "/nonexistent.html")
+ }.to raise_error(Blimp::Handler::SourceNotFound)
+ end
+ end
+
+ context "for URLs that it cannot handle" do
+ it "should return nil" do
+ handler.handle(source, theme, "/image.jpg").should be_nil
+ end
+ end
end
end
View
28 spec/blimp/handlers/static_handler_spec.rb
@@ -6,19 +6,29 @@
let(:handler) { Blimp::Handlers::StaticHandler.new("/") }
describe "#handle" do
- let(:headers) { handler.handle(source, theme, "/image.jpg")[0] }
- let(:body) { handler.handle(source, theme, "/image.jpg")[1] }
+ context "for existing files" do
+ let(:headers) { handler.handle(source, theme, "/image.jpg")[0] }
+ let(:body) { handler.handle(source, theme, "/image.jpg")[1] }
- it "returns the contents for the path" do
- body.should == "A kitten"
- end
+ it "returns the contents for the path" do
+ body.should == "A kitten"
+ end
+
+ it "returns headers" do
+ headers.should be_a(Hash)
+ end
- it "returns headers" do
- headers.should be_a(Hash)
+ it "returns a content type" do
+ headers["Content-Type"].should == "image/jpeg"
+ end
end
- it "returns a content type" do
- headers["Content-Type"].should == "image/jpeg"
+ context "for nonexistent files" do
+ it "should raise" do
+ expect {
+ handler.handle(source, theme, "/nonexistent.jpg")
+ }.to raise_error(Blimp::Handler::SourceNotFound)
+ end
end
end
end
View
26 spec/blimp/site_spec.rb
@@ -1,7 +1,10 @@
require "spec_helper"
describe Site do
- let(:source) { Blimp::Sources::FakeSource.new({ "templates" => { "layout.liquid" => "{{ content }}" } }) }
+ let(:source) { Blimp::Sources::FakeSource.new({
+ "index.html" => "<h1>Hello world!</h1>",
+ "templates" => { "layout.liquid" => "{{ content }}" }
+ }) }
let(:site) { Site.new("my-site", source) }
it "should be initializable" do
@@ -29,12 +32,21 @@
end
end
- describe "#find_page" do
- it "should find a page" do
- path = stub
- page = stub
- Page.stub(:from_path).with(path, source) { page }
- site.find_page(path).should == page
+ describe "#handle_request" do
+ it "should return a response for existing URLs" do
+ site.handle_request("/index.html").should be
+ end
+
+ it "should raise for URLs that are matched by a handler but don't exist" do
+ expect {
+ site.handle_request("/nonexistent.html")
+ }.to raise_error(Site::NotFound)
+ end
+
+ it "should raise for URLs that don't match any handler" do
+ expect {
+ site.handle_request("/nonexistent")
+ }.to raise_error(Site::NotFound)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.