Skip to content

Instantly share code, notes, and snippets.

@capotej
Created August 10, 2009 06:36
Show Gist options
  • Save capotej/165033 to your computer and use it in GitHub Desktop.
Save capotej/165033 to your computer and use it in GitHub Desktop.
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
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