cachable_url 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +5 -0
- data/cachable_url.gemspec +24 -0
- data/lib/cachable_url/middleware.rb +20 -0
- data/lib/cachable_url/version.rb +3 -0
- data/lib/cachable_url.rb +11 -0
- data/spec/encoder_spec.rb +25 -0
- data/spec/middleware_spec.rb +47 -0
- data/spec/spec_helper.rb +4 -0
- metadata +118 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "cachable_url/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "cachable_url"
|
7
|
+
s.version = CachableUrl::VERSION
|
8
|
+
s.authors = ["Tom Lea"]
|
9
|
+
s.email = ["commit@tomlea.co.uk"]
|
10
|
+
s.homepage = "http://tomlea.co.uk"
|
11
|
+
s.summary = %q{Hide query strings so caches can't see them.'}
|
12
|
+
s.description = %q{Replace all '?'s in URLs with '%1F' (the ASCII unit separator char), and a middleware to undo it before your rack app sees.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "cachable_url"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rspec"
|
22
|
+
s.add_development_dependency "rake"
|
23
|
+
s.add_development_dependency "rack-test"
|
24
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "cachable_url"
|
2
|
+
|
3
|
+
class CachableUrl::Midleware
|
4
|
+
def initialize(app)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
if env["QUERY_STRING"] == ''
|
10
|
+
env = env.clone
|
11
|
+
path, query = env["PATH_INFO"].split("%1F", 2)
|
12
|
+
query = CachableUrl.decode(query || "")
|
13
|
+
env["PATH_INFO"] = path
|
14
|
+
env["QUERY_STRING"] = query
|
15
|
+
@app.call(env)
|
16
|
+
else
|
17
|
+
@app.call(env)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/cachable_url.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe CachableUrl do
|
4
|
+
|
5
|
+
EXAMPLES=[
|
6
|
+
"http://foo:bar@baz.com/wizz?bang=foo",
|
7
|
+
"http://foo:bar@baz.com/wi???bang=baz"
|
8
|
+
]
|
9
|
+
|
10
|
+
EXAMPLES.each do |example|
|
11
|
+
|
12
|
+
it "symmetrically encodes/decodes #{example}" do
|
13
|
+
original_value = example
|
14
|
+
encoded_value = CachableUrl.encode(example)
|
15
|
+
decoded_value = CachableUrl.decode(encoded_value)
|
16
|
+
decoded_value.should == original_value
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should encode without any '?'s in it" do
|
20
|
+
CachableUrl.encode(example).should_not include("?")
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe CachableUrl::Midleware do
|
4
|
+
include Rack::Test::Methods
|
5
|
+
|
6
|
+
let(:requests_received){ [] }
|
7
|
+
|
8
|
+
def app
|
9
|
+
logger_app = lambda{|env| requests_received << env; [200, {"Content-Type" => 'text/plain'}, ["Hello world"]] }
|
10
|
+
CachableUrl::Midleware.new(logger_app)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should pass through requests with query strings" do
|
14
|
+
get "/fooooooo%1F?foo=bar"
|
15
|
+
|
16
|
+
requests_received.size.should == 1
|
17
|
+
requests_received.last["QUERY_STRING"].should == "foo=bar"
|
18
|
+
requests_received.last["PATH_INFO"].should == "/fooooooo%1F"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should decode requests with no query strings" do
|
22
|
+
get "/fooooooo%1Ffoo=bar"
|
23
|
+
|
24
|
+
requests_received.size.should == 1
|
25
|
+
requests_received.last["QUERY_STRING"].should == "foo=bar"
|
26
|
+
requests_received.last["PATH_INFO"].should == "/fooooooo"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should decode requests with no query strings to the same thing as the query string based request" do
|
30
|
+
get "/fooooooo%1Ffoo=bar"
|
31
|
+
get "/fooooooo?foo=bar"
|
32
|
+
|
33
|
+
requests_received.size.should == 2
|
34
|
+
a,b = clense_responses(*requests_received.last(2))
|
35
|
+
a.should == b
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
def clense_responses(*args)
|
40
|
+
args.map{|r|
|
41
|
+
r.delete_if{|key, value|
|
42
|
+
["rack.input", "rack.errors"].include? key
|
43
|
+
}
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cachable_url
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Tom Lea
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-02-20 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
22
|
+
none: false
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
hash: 3
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
version_requirements: *id001
|
31
|
+
name: rspec
|
32
|
+
type: :development
|
33
|
+
prerelease: false
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
hash: 3
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
version_requirements: *id002
|
45
|
+
name: rake
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
hash: 3
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
version_requirements: *id003
|
59
|
+
name: rack-test
|
60
|
+
type: :development
|
61
|
+
prerelease: false
|
62
|
+
description: Replace all '?'s in URLs with '%1F' (the ASCII unit separator char), and a middleware to undo it before your rack app sees.
|
63
|
+
email:
|
64
|
+
- commit@tomlea.co.uk
|
65
|
+
executables: []
|
66
|
+
|
67
|
+
extensions: []
|
68
|
+
|
69
|
+
extra_rdoc_files: []
|
70
|
+
|
71
|
+
files:
|
72
|
+
- .gitignore
|
73
|
+
- Gemfile
|
74
|
+
- Rakefile
|
75
|
+
- cachable_url.gemspec
|
76
|
+
- lib/cachable_url.rb
|
77
|
+
- lib/cachable_url/middleware.rb
|
78
|
+
- lib/cachable_url/version.rb
|
79
|
+
- spec/encoder_spec.rb
|
80
|
+
- spec/middleware_spec.rb
|
81
|
+
- spec/spec_helper.rb
|
82
|
+
homepage: http://tomlea.co.uk
|
83
|
+
licenses: []
|
84
|
+
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
hash: 3
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
version: "0"
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
hash: 3
|
105
|
+
segments:
|
106
|
+
- 0
|
107
|
+
version: "0"
|
108
|
+
requirements: []
|
109
|
+
|
110
|
+
rubyforge_project: cachable_url
|
111
|
+
rubygems_version: 1.8.15
|
112
|
+
signing_key:
|
113
|
+
specification_version: 3
|
114
|
+
summary: Hide query strings so caches can't see them.'
|
115
|
+
test_files:
|
116
|
+
- spec/encoder_spec.rb
|
117
|
+
- spec/middleware_spec.rb
|
118
|
+
- spec/spec_helper.rb
|