Skip to content

Commit

Permalink
implemented are more complete first version
Browse files Browse the repository at this point in the history
  • Loading branch information
kronn committed Sep 16, 2010
1 parent 5f9c264 commit 3903c24
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 71 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
source :rubygems

gemspec

gem 'gem-release'
gem 'redgreen'
38 changes: 1 addition & 37 deletions Rakefile
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
19 changes: 0 additions & 19 deletions lib/rack-speling.rb

This file was deleted.

57 changes: 57 additions & 0 deletions lib/rack/speling.rb
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.
3 changes: 1 addition & 2 deletions rack-speling.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# vim:ft=ruby:fileencoding=utf-8
require 'lib/rack-speling/version.rb'
require 'lib/rack/speling/version.rb'

spec = Gem::Specification.new do |s|
s.name = 'rack-speling'
Expand All @@ -18,6 +18,5 @@ spec = Gem::Specification.new do |s|

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files test`.split("\n")
# s.executables = `git ls-files bin`.split("\\n")
s.require_path = 'lib'
end
3 changes: 3 additions & 0 deletions test/all.rb
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
8 changes: 4 additions & 4 deletions test/helper.rb
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'
43 changes: 34 additions & 9 deletions test/test_rack-speling.rb
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

0 comments on commit 3903c24

Please sign in to comment.