Created
May 16, 2009 07:08
-
-
Save faultier/112607 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env ruby | |
# coding: utf-8 | |
require 'net/http' | |
require 'time' | |
def show(path, res) | |
puts "---- #{path} ----" | |
puts "Response: #{res.code} #{res.message}" | |
puts "Content-Length: #{res['Content-Length']}" | |
puts "Last-Modified: #{res['Last-Modified']}" if res['Last-Modified'] | |
puts "Allow: #{res['Allow']}" if res['Allow'] | |
puts "(Body Length): #{res.body ? res.body.length : nil}" | |
end | |
Net::HTTP.start('localhost', 9292) do |http| | |
path = '/resource/kanojo' | |
puts "===== GET =====" | |
[path, "#{path}?mode=prev", "#{path}?mode=next", "#{path}?mode=error", "#{path}/invalid"].each do |_path| | |
show _path, http.get(_path) | |
end | |
show path, http.get(path, 'If-Modified-Since' => Time.now.httpdate) | |
puts | |
puts "===== HEAD =====" | |
[path, "#{path}?mode=prev", "#{path}?mode=next"].each do |_path| | |
show _path, http.head(_path) | |
end | |
show path, http.head(path, 'If-Modified-Since' => Time.now.httpdate) | |
puts | |
puts "===== POST =====" | |
show path, http.post(path, '') | |
show path, http.post(path, 'name=hoge&email=fuga&imageurl=piyo') | |
puts | |
puts "===== PUT =====" | |
show path, http.put(path, '') | |
puts | |
puts "===== DELETE =====" | |
show path, http.delete(path) | |
puts | |
puts "===== OPTIONS =====" | |
show path, http.options(path) | |
end |
This file contains 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
# vim: filetype=ruby fileencoding=utf-8 | |
require 'kanojo_app' | |
#use RewindableInputWrapper | |
map '/resource/kanojo' do | |
run KanojoApp.new | |
end |
This file contains 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
!!! XML UTF-8 | |
!!! Strict | |
%html | |
%head | |
%title 501 Not Implemented | |
%body | |
%h1 彼女を捨てる機能は未実装です | |
.description | |
%p 存在しないものに対する操作は実装されません。 |
This file contains 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
!!! XML UTF-8 | |
!!! Strict | |
%html | |
%head | |
%title 500 Internal Server Error | |
%body | |
%h1 やべぇ、なんかしくった | |
.description | |
%p サーバが割となんかアレな状態っぽいです | |
%p< | |
%a{:href => 'http://twitter.com/faultier'}> faultier | |
に、「なんか内なる剣の反逆"インターナルセイバーエラー"が出てるよ」と教えてあげて下さい。 |
This file contains 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
!!! XML UTF-8 | |
!!! Strict | |
%html | |
- case status | |
- when 402 | |
%head | |
%title 402 Payment Required | |
%body | |
%h1 付き合い続けるために料金が発生します | |
.description | |
%p え、ちょ、ここからは有料なの?マジで? | |
- when 418 | |
%head | |
%title 418 I'm a teapod | |
%body | |
%h1 彼女だと思っていたものはティーポットでした | |
.description | |
%p そうだ、コーヒー飲みたい。喫茶店行ってこよう。 | |
- else | |
%head | |
%title 404 Not Found | |
%body | |
%h1 彼女が見つかりません | |
.description | |
%p< | |
%a{:href => 'http://www.google.co.jp/search?q=faultier%E3%81%AE%E5%BD%BC%E5%A5%B3'}> 俺の代わりにどこかで探してきてくれる | |
か、彼女になってください。 | |
%form{:action => '', :method => 'post'} | |
%input{:type => 'submit', :value => '彼女になりたい!'} |
This file contains 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
!!! XML UTF-8 | |
!!! Strict | |
%html | |
%head | |
%title 404 Not Found | |
%body | |
%h1 そんなの無いよ | |
.description | |
%p< | |
=path | |
ってのが無いみたいだよ |
This file contains 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
# coding: utf-8 | |
require 'rack/request' | |
require 'rack/response' | |
require 'haml' | |
require 'time' | |
class RewindableInputWrapper | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
unless env['rack.input'].respond_to?(:rewind) | |
env['rack.input'] = StringIO.new(env['rack.input'].read) | |
end | |
@app.call(env) | |
end | |
end | |
class KanojoApp | |
@@allowed_methods = %w(get head post put delete options) | |
@@last_modified = Time.parse('1985-09-11') | |
def call(env) | |
req = Rack::Request.new(env) | |
begin | |
status, header, body = dispatch(req) | |
rescue | |
warn $!.inspect | |
status = 500 | |
header = {} | |
body = render(:error) | |
end | |
res = Rack::Response.new | |
res.status = status | |
header.each_pair {|key,value| res[key] = value} | |
if !!body && body.length > 0 | |
res['Content-Type'] = 'text/html;charset=utf-8' | |
res.write body | |
end | |
res.finish | |
end | |
def dispatch(req) | |
action = req.path_info.sub(/\A\//, '') | |
action = req.request_method.downcase unless !!action && action != '' | |
if @@allowed_methods.include?(action) | |
send(action.intern, req) | |
else | |
[404, {}, render(:invalid, :path => req.path_info)] | |
end | |
end | |
def render(action, locals = {}) | |
file = File.join(File.dirname(__FILE__), "#{action}.haml") | |
Haml::Engine.new(File.read(file)).render(Object.new, locals) | |
end | |
def get(req) | |
if !!req.env['HTTP_IF_MODIFIED_SINCE'] && Time.parse(req.env['HTTP_IF_MODIFIED_SINCE']) >= @@last_modified | |
return [304, {'Last-Modified' => @@last_modified.httpdate}] | |
end | |
status = case req['mode'] | |
when 'prev' then 418 | |
when 'next' then 402 | |
when 'error' then raise 'わざと例外起こすよ' | |
else 404 end | |
[status, {'Last-Modified' => @@last_modified.httpdate}, render(:get, :status => status)] | |
end | |
alias :head :get | |
def post(req) | |
status = ((req['name'] && req['name'].length > 0)\ | |
&& (req['email'] && req['email'].length > 0)\ | |
&& (req['imageurl'] && req['imageurl'].length > 0)) ? 503 : 400 | |
[status, {}, render(:post, :status => status)] | |
end | |
def put(req) | |
[202, {}, render(:put)] | |
end | |
def delete(req) | |
[501, {}, render(:delete)] | |
end | |
def options(req) | |
[200, {'Allow' => @@allowed_methods.map{|m|m.upcase}.join(', ')}] | |
end | |
end |
This file contains 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
# coding: utf-8 | |
require 'rack/test' | |
require 'kanojo_app' | |
include Rack::Test::Methods | |
def app | |
KanojoApp.new | |
end | |
describe KanojoApp do | |
describe 'GET' do | |
it 'should return "Not Found"' do | |
res = get '/' | |
res.status.should be_eql(404) | |
res = get '/get' | |
res.status.should be_eql(404) | |
end | |
it 'should return "I\'m a teapot" with mode="prev"' do | |
res = get '/', { :mode => 'prev' } | |
res.status.should be_eql(418) | |
end | |
it 'should return "Payment Required" with mode="next"' do | |
res = get '/', { :mode => 'next' } | |
res.status.should be_eql(402) | |
end | |
end | |
describe 'HEAD' do | |
it 'should return "Not Found"' do | |
res = head '/' | |
res.status.should be_eql(404) | |
res = head '/head' | |
res.status.should be_eql(404) | |
end | |
it 'should return "I\'m a teapod" with mode="prev"' do | |
res = head '/', { :mode => 'prev' } | |
res.status.should be_eql(418) | |
end | |
it 'should return "Payment Required" whit mode="next"' do | |
res = head '/', { :mode => 'next' } | |
res.status.should be_eql(402) | |
end | |
end | |
describe 'POST' do | |
it 'should return "Service Unavailable" when using http method' do | |
res = post '/', { :name => 'Hibiki Ganaha', :email => '[email protected]', :imageurl => 'http://image.example.org/hibiki.jpg' } | |
res.status.should be_eql(503) | |
end | |
it 'should return "Service Unavailable" code when using action url' do | |
res = get '/post', { :name => 'Hibiki Ganaha', :email => '[email protected]', :imageurl => 'http://image.example.org/hibiki.jpg' } | |
res.status.should be_eql(503) | |
end | |
it 'should retrun "Bad Request" if parameter is empty' do | |
res = post '/' | |
res.status.should be_eql(400) | |
end | |
end | |
describe 'PUT' do | |
it 'should return "Accept" when using http method' do | |
res = put '/' | |
res.status.should be_eql(202) | |
end | |
it 'should return "Accept" code when using action url' do | |
res = get '/put' | |
res.status.should be_eql(202) | |
end | |
end | |
describe 'DELETE' do | |
it 'should return "Not Implemented" when using http method' do | |
res = delete '/' | |
res.status.should be_eql(501) | |
end | |
it 'should return "Not Implemented" code when using action url' do | |
res = delete '/delete' | |
res.status.should be_eql(501) | |
end | |
end | |
describe 'Invalid action' do | |
it 'should return "Not Found"' do | |
res = get '/invalid' | |
res.status.should be_eql(404) | |
end | |
end | |
end |
This file contains 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
!!! XML UTF-8 | |
!!! Strict | |
%html | |
- if status == 503 | |
%head | |
%title 503 Service Unavailable | |
%body | |
%h1 都合によりエントリーは受け付けられないです | |
.description | |
%p まさか本当に彼女になりたい人がいるなんて思いもしなかったので…。 | |
%p もっと別な形で出会えていれば、俺たち、分かりあえたのかもな…。 | |
- else | |
%head | |
%title 400 Bad Request | |
%body | |
%h1 必要な情報がありません | |
.description | |
%p 名前も連絡先も教えずに彼女になりたいとかなんなの?馬鹿なの?死ぬの? | |
%p{:style => 'font-size:0.7em;color:#333'}< | |
一応言っておくけど、ネタだしこれ入力しても別になにもしてないので安心してエントリーするといいよ! | |
%br/ | |
まぁログくらいは見ようと思えば見れるので真面目に入れない方がいいとは思うけどね。 | |
%form{:action => '', :method => :post} | |
%dl | |
%dt< | |
%label{:for => :name} 名前(必須) | |
%dd< | |
%input{:id => :name, :name => :name, :type => :text} | |
%dt< | |
%label{:for => :email} メールアドレス(必須) | |
%dd< | |
%input{:id => :email, :name => :email, :type => :text} | |
%dt< | |
%label{:for => :imageurl} 顔写真のURL(必須) | |
%dd< | |
%input{:id => :imageurl, :name => :imageurl, :type => :text} | |
%input{:type => :submit, :value => '彼女になりたい!'} |
This file contains 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
!!! XML UTF-8 | |
!!! Strict | |
%html | |
%head | |
%title 202 Accept | |
%body | |
%h1 彼女を置いていくのはやめてください | |
.description | |
%p 自分の彼女が要らないからって、流石にそれは可哀想だと思いませんか。 | |
%p ど、どうしよう。あ、あのう、僕はどうすれば。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment