Instantly share code, notes, and snippets.

Embed
What would you like to do?
a bundle of `config.ru`, testing rack middlewares.
use Rack::Auth::Basic, 'app' do |usr, pwd|
usr == 'wendi' && pwd == '123123'
end
# => Request Headers
# Authorization:Basic d2VuZGk6MTIzMTIz
# realm = 'Hello, wrold'
# opaque = '1234567890'
# use Rack::Auth::Digest::MD5, realm, opaque do |password|
# 'secret'
# end
#
# => Request Headers
# Authorization:Digest username="wendi", realm="Hello, wrold", nonce="MTQxMTQ1NTA3NyBmODlkYWNjNGQ4ZGVkMDFmYjYxZDcwZTBiMWE1OGMxMA==", uri="/", response="5ef8a4dd410fd510e0fee15bb3563815", opaque="f5bb0c8de146c67b44babbf4e6584cc0", qop=auth, nc=00000003, cnonce="66a95969acfd54bf"
run proc {
[200, {'Content-Type' => 'text/html'}, ['Hello, world']]
}
require 'rack/lobster'
use Rack::ContentType
class BenchMarker
def initialize(app, runs = 100)
@app, @runs = app, runs
end
def call(env)
t = Time.now
result = nil
@runs.times { result = @app.call(env) }
t2 = Time.now - t
STDOUT.puts <<OUTPUT
Benchmark:
#{@runs} runs
#{t2.to_f} seconds total
#{t2.to_f * 1000.0 / @runs} milisec/run
OUTPUT
result
end
end
use BenchMarker, 10_000
run Rack::Lobster.new
# Benchmark:
# 10000 runs
# 0.284061 seconds total
# 0.028406100000000004 milisec/run
class Foo
def initialize(app, arg = '')
puts '--> Foo#init'
@app = app
@arg = arg
puts '--> Foo#initend'
end
def call(env)
puts '--> Foo#call'
status, headers, content = @app.call(env)
content[0] += "#{@arg}"
puts '--> Foo#callend'
[ status, headers, content ]
end
end
class Bar
def initialize(app, arg = '')
puts '--> Bar#init'
@app = app
@arg = arg
puts '--> Bar#initend'
end
def call(env)
puts '--> Bar#call'
status, headers, content = @app.call(env)
content[0] += "#{@arg}"
puts '--> Bar#callend'
[ status, headers, content ]
end
end
use Foo, ', foo'
use Bar, ', bar'
run proc {
puts '--> main#call'
[200, {'Content-Type' => 'text/html'}, ['Hello, world']]
}
# $ rackup
# --> Bar#init
# --> Bar#initend
# --> Foo#init
# --> Foo#initend
# Thin web server (v1.6.1 codename Death Proof)
# Maximum connections set to 1024
# Listening on 0.0.0.0:9292, CTRL+C to stop
#
# --> Foo#call
# --> Bar#call
# --> main#call
# --> Bar#callend
# --> Foo#callend
# 127.0.0.1 - wendi [23/Sep/2014 15:56:20] "GET / HTTP/1.1" 200 - 0.0013
require 'rack/lobster'
use Rack::ContentType
map '/lobster' do
use Rack::ShowExceptions
run Rack::Lobster.new
end
map '/lobster/but_not' do
run proc {
[200, {}, ["Really not a lobster"]]
}
end
run proc {
[200, {}, ["Not a lobster"]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment