Created
August 10, 2009 06:36
-
-
Save capotej/165033 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rack/test' | |
require 'lib/bento/router' | |
class RandomApp | |
def initialize(app = nil) | |
@app = app | |
end | |
def call(env) | |
[200, { 'Content-Type' => 'text/plain', 'Content-Length' => "5" }, "hello"] | |
end | |
end | |
class TestRouter < Test::Unit::TestCase | |
include Rack::Test::Methods | |
def app | |
Bento::Router.new do |r| | |
r.swap RandomApp.new | |
end | |
end | |
def test_output | |
get "/" | |
assert last_response.body.include?("hello") | |
end | |
end | |
class TestRouterConcat < Test::Unit::TestCase | |
include Rack::Test::Methods | |
def app | |
Bento::Router.new do |r| | |
r.swap RandomApp.new | |
r.swap RandomApp.new | |
end | |
end | |
def test_concat | |
get "/" | |
assert last_response.body.include?("hellohello") | |
end | |
end | |
class TestRouterWrapping < Test::Unit::TestCase | |
include Rack::Test::Methods | |
def app | |
Bento::Router.new do |r| | |
r.swap 'content' => RandomApp.new | |
r.swap RandomApp.new | |
end | |
end | |
def test_wrap | |
get "/" | |
assert last_response.body.include?('<div id="content">hello</div>hello') | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Bento | |
class Router | |
class Interface | |
def initialize(env) | |
@env = env | |
@arr = [] | |
end | |
def swap(obj) | |
if obj.class == Hash | |
div = obj.keys.first | |
if div.class == Symbol | |
div = div.to_s | |
end | |
status, headers, body = obj.values.first.call(@env) | |
@arr << [status, headers, '<div id="' << div << '">' << body << '</div>'] | |
else | |
@arr << obj.call(@env) | |
end | |
end | |
end | |
def initialize(app = nil, &block) | |
@app = app | |
@block = block | |
end | |
def call(env) | |
output = "" | |
obj = Interface.new(env) | |
results = @block.call(obj) | |
results.each do |result| | |
status,headers,body = result | |
output << body | |
end | |
[200, {"Content-Type"=>"text/plain", "Content-Length"=> output.to_i.size.to_s}, output] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment