-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented are more complete first version
- Loading branch information
Showing
9 changed files
with
103 additions
and
71 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
source :rubygems | ||
|
||
gemspec | ||
|
||
gem 'gem-release' | ||
gem 'redgreen' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,9 @@ | ||
#!/usr/bin/env rake | ||
# vim:ft=ruby:fileencoding=utf-8 | ||
|
||
# # enable trace to get better error output | ||
# Rake.application.options.trace = true | ||
|
||
# # documentation tasks | ||
# begin | ||
# %w[ rake/rdoctask sdoc ].each { |lib| require lib } | ||
# Rake::RDocTask.new do |rdoc| | ||
# version = File.exist?('VERSION') ? File.read('VERSION') : "" | ||
# | ||
# rdoc.rdoc_dir = 'doc/rdoc' | ||
# rdoc.title = "to_pass #{version}" | ||
# rdoc.options << '--fmt=shtml' | ||
# rdoc.options << '--all' | ||
# rdoc.options << '--charset=utf-8' | ||
# rdoc.template = 'direct' | ||
# rdoc.rdoc_files.include('README*') | ||
# rdoc.rdoc_files.include('LICENSE') | ||
# rdoc.rdoc_files.include('TODO') | ||
# rdoc.rdoc_files.include('lib/**/*.rb') | ||
# rdoc.rdoc_files.include('data/**/*.rb') | ||
# end | ||
# rescue LoadError | ||
# end | ||
|
||
desc "run tests" | ||
task :test do | ||
# optional libraries | ||
%w[ redgreen ].each do |lib| | ||
begin | ||
require lib | ||
rescue LoadError | ||
end | ||
end | ||
|
||
require 'test/unit' | ||
|
||
( ['test/helper'] + Dir['test/test_*.rb'] ).each do |file| | ||
require File.expand_path("../#{file}", __FILE__) | ||
end | ||
require File.expand_path("../test/all", __FILE__) | ||
end | ||
|
||
task :default => :test |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# vim:ft=ruby:fileencoding=utf-8 | ||
|
||
module Rack | ||
class Speling | ||
attr_reader :app, :lookup, :env, :response | ||
|
||
# remember the application as we need to call before or after this | ||
# an lookup-object may be supplied for further 404-handling | ||
def initialize(app, options = {}) | ||
@app = app | ||
@lookup = options[:lookup] || {} | ||
end | ||
|
||
# downcase PATH_INFO and REQUEST_URI | ||
# furthermore, try to lookup 404s to react with a redirect | ||
def call(env) | ||
@env = env | ||
|
||
downcase_path | ||
|
||
@response = app.call(env) | ||
|
||
if response[0] == 404 && path_lookup | ||
correct_response | ||
else | ||
response | ||
end | ||
end | ||
|
||
private | ||
|
||
def correct_response | ||
headers = response[1].merge({ | ||
'Location' => new_location | ||
}) | ||
|
||
[302, headers, "Moved: #{headers['Location']}"] | ||
end | ||
|
||
def new_location | ||
path_lookup.start_with?('http:') ? path_lookup : "http://#{env['HTTP_HOST']}#{path_lookup}" | ||
end | ||
|
||
def path_lookup | ||
@path_lookup ||= lookup[path] | ||
end | ||
|
||
def path | ||
env['PATH_INFO'] | ||
end | ||
|
||
def downcase_path | ||
env['PATH_INFO'] = env['PATH_INFO'].downcase | ||
env['REQUEST_URI'] = env['REQUEST_URI'].downcase | ||
end | ||
end | ||
end |
File renamed without changes.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Dir[File.expand_path('../test_*.rb', __FILE__)].each do |file| | ||
require file | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
# vim:ft=ruby:fileencoding=utf-8 | ||
|
||
require 'test/unit/testcase' | ||
require 'test/unit' unless defined?(Test::Unit) | ||
require 'redgreen' rescue LoadError | ||
require 'test/unit' | ||
|
||
base_path = ( File.expand_path(File.dirname(__FILE__)+'/..') ) | ||
lib_path = "#{base_path}/lib" | ||
if File.exist?(base_path + '/lib/rack-speling') | ||
if File.exist?(base_path + '/lib/rack/speling') | ||
$LOAD_PATH << base_path unless $LOAD_PATH.include?(base_path) | ||
end | ||
if File.exist?( lib_path ) | ||
$LOAD_PATH << lib_path unless $LOAD_PATH.include?(lib_path) | ||
end | ||
|
||
require 'rack-speling' | ||
require 'rack/speling' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,52 @@ | ||
require File.expand_path("../helper", __FILE__) | ||
|
||
class SpelingTest < Test::Unit::TestCase | ||
def sut | ||
speling = Class.new do | ||
include Rack::Speling | ||
end | ||
app = lambda { |arg| return arg } | ||
|
||
speling.new(app) | ||
def sut(*args) | ||
Rack::Speling.new(*args) | ||
end | ||
|
||
def env | ||
{ 'PATH_INFO' => '/somepath', 'REQUEST_URI' => '/somepath' } | ||
end | ||
|
||
def status_app(status) | ||
lambda { |env| | ||
[status, env, ''] | ||
} | ||
end | ||
|
||
def echo_app | ||
lambda { |arg| return arg } | ||
end | ||
|
||
def test_downcasing_path_info | ||
result = sut.call( env.merge('PATH_INFO' => '/SOMEPATH') ) | ||
result = sut(echo_app).call( env.merge('PATH_INFO' => '/SOMEPATH') ) | ||
|
||
assert_equal env, result | ||
end | ||
|
||
def test_downcasing_request_uri | ||
result = sut.call( env.merge('REQUEST_URI' => '/SOMEPATH') ) | ||
result = sut(echo_app).call( env.merge('REQUEST_URI' => '/SOMEPATH') ) | ||
assert_equal env, result | ||
end | ||
|
||
def test_does_not_touch_200s | ||
result = sut(status_app(200)).call(env) | ||
assert_equal 200, result[0], result.inspect | ||
end | ||
|
||
def test_does_accept_an_optional_lookup_object | ||
assert_nothing_raised do | ||
sut(echo_app) | ||
sut(echo_app, :lookup => {}) | ||
end | ||
end | ||
|
||
def test_redirect_on_successful_lookup | ||
result = sut(status_app(404), :lookup => { | ||
'/somepath' => 'http://example.com/newpath' | ||
}).call(env) | ||
assert_equal 302, result[0] | ||
assert_equal 'http://example.com/newpath', result[1]['Location'] | ||
end | ||
end |