rack_slashless 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/README.md +2 -0
- data/lib/rack_slashless.rb +23 -0
- data/rack_slashless.gemspec +15 -0
- data/spec/rack_slashless_spec.rb +45 -0
- data/spec/spec_helper.rb +7 -0
- metadata +11 -3
data/.gitignore
ADDED
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module Rack
|
2
|
+
class Slashless
|
3
|
+
|
4
|
+
def initialize(app)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
if env['REQUEST_METHOD'] == "GET" && env['QUERY_STRING'].empty? && env['PATH_INFO'].end_with?("/")
|
10
|
+
parts = env['SERVER_NAME'].split('.')
|
11
|
+
suffix, chunk, prefix = parts.pop, parts.pop, parts.pop
|
12
|
+
|
13
|
+
destination = "#{env['rack.url_scheme']}://#{prefix}.#{chunk}.#{suffix}"
|
14
|
+
destination << "#{env['PATH_INFO']}"[0..-2]
|
15
|
+
|
16
|
+
[301, {'Location' => destination}, ['Redirecting to the same url but with the ending /']]
|
17
|
+
else
|
18
|
+
@app.call(env)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.authors = ["Gregory Marcilhacy"]
|
3
|
+
gem.email = ["g.marcilhacy@gmail.com"]
|
4
|
+
gem.description = gem.summary = "Performs a 301 redirection on every url ending by a /"
|
5
|
+
gem.homepage = "https://github.com/gregorym/rack_slashless"
|
6
|
+
|
7
|
+
gem.files = `git ls-files | grep -v myapp`.split("\n")
|
8
|
+
gem.test_files = `git ls-files -- spec/*`.split("\n")
|
9
|
+
gem.name = "rack_slashless"
|
10
|
+
gem.require_paths = ["lib"]
|
11
|
+
gem.version = "0.0.2"
|
12
|
+
|
13
|
+
gem.add_development_dependency 'rspec'
|
14
|
+
gem.add_development_dependency 'rake-test'
|
15
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Rack::Slashless do
|
4
|
+
def app
|
5
|
+
@app ||= Rack::Builder.new do
|
6
|
+
use Rack::Slashless
|
7
|
+
run lambda { |env| [200, { 'Content-Type' => 'text/plain' }, ['Hello World'] ] }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "/" do
|
12
|
+
it "should redirect to server name" do
|
13
|
+
get '/', {}, {'SERVER_NAME' => 'www.example.org'}
|
14
|
+
|
15
|
+
last_response.status.should == 301
|
16
|
+
last_response['Location'].should eql('http://www.example.org')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "/blog/" do
|
21
|
+
it "should redirect to /blog" do
|
22
|
+
get '/blog/', {}, {'SERVER_NAME' => 'www.example.org'}
|
23
|
+
|
24
|
+
last_response.status.should == 301
|
25
|
+
last_response['Location'].should eql('http://www.example.org/blog')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "/blog?article=1" do
|
30
|
+
it "should not redirect" do
|
31
|
+
get '/blog?article=1', {}, {'SERVER_NAME' => 'www.example.org'}
|
32
|
+
|
33
|
+
last_response.status.should == 200
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "POST /blog" do
|
38
|
+
it "should not redirect" do
|
39
|
+
post '/blog/', {}, {'SERVER_NAME' => 'www.example.org'}
|
40
|
+
|
41
|
+
last_response.status.should == 200
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack_slashless
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -49,7 +49,13 @@ email:
|
|
49
49
|
executables: []
|
50
50
|
extensions: []
|
51
51
|
extra_rdoc_files: []
|
52
|
-
files:
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- README.md
|
55
|
+
- lib/rack_slashless.rb
|
56
|
+
- rack_slashless.gemspec
|
57
|
+
- spec/rack_slashless_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
53
59
|
homepage: https://github.com/gregorym/rack_slashless
|
54
60
|
licenses: []
|
55
61
|
post_install_message:
|
@@ -74,4 +80,6 @@ rubygems_version: 1.8.23
|
|
74
80
|
signing_key:
|
75
81
|
specification_version: 3
|
76
82
|
summary: Performs a 301 redirection on every url ending by a /
|
77
|
-
test_files:
|
83
|
+
test_files:
|
84
|
+
- spec/rack_slashless_spec.rb
|
85
|
+
- spec/spec_helper.rb
|