rack-rewrite 1.3.2 → 1.3.3

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/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.2
1
+ 1.3.3
@@ -11,18 +11,18 @@ module Rack
11
11
  protected
12
12
  # We're explicitly defining private functions for our DSL rather than
13
13
  # using method_missing
14
-
14
+
15
15
  # Creates a rewrite rule that will simply rewrite the REQUEST_URI,
16
- # PATH_INFO, and QUERY_STRING headers of the Rack environment. The
16
+ # PATH_INFO, and QUERY_STRING headers of the Rack environment. The
17
17
  # user's browser will continue to show the initially requested URL.
18
- #
18
+ #
19
19
  # rewrite '/wiki/John_Trupiano', '/john'
20
20
  # rewrite %r{/wiki/(\w+)_\w+}, '/$1'
21
21
  # rewrite %r{(.*)}, '/maintenance.html', :if => lambda { File.exists?('maintenance.html') }
22
22
  def rewrite(*args)
23
23
  add_rule :rewrite, *args
24
24
  end
25
-
25
+
26
26
  # Creates a redirect rule that will send a 301 when matching.
27
27
  #
28
28
  # r301 '/wiki/John_Trupiano', '/john'
@@ -32,10 +32,10 @@ module Rack
32
32
  def r301(*args)
33
33
  add_rule :r301, *args
34
34
  end
35
-
35
+
36
36
  alias :moved_permanently :r301
37
37
  alias :p :r301
38
-
38
+
39
39
  # Creates a redirect rule that will send a 302 when matching.
40
40
  #
41
41
  # r302 '/wiki/John_Trupiano', '/john'
@@ -45,9 +45,9 @@ module Rack
45
45
  def r302(*args)
46
46
  add_rule :r302, *args
47
47
  end
48
-
48
+
49
49
  alias :found :r302
50
-
50
+
51
51
  # Creates a redirect rule that will send a 303 when matching.
52
52
  #
53
53
  # r303 '/wiki/John_Trupiano', '/john'
@@ -57,9 +57,9 @@ module Rack
57
57
  def r303(*args)
58
58
  add_rule :r303, *args
59
59
  end
60
-
60
+
61
61
  alias :see_other :r303
62
-
62
+
63
63
  # Creates a redirect rule that will send a 307 when matching.
64
64
  #
65
65
  # r307 '/wiki/John_Trupiano', '/john'
@@ -69,32 +69,32 @@ module Rack
69
69
  def r307(*args)
70
70
  add_rule :r307, *args
71
71
  end
72
-
72
+
73
73
  alias :temporary_redirect :r307
74
74
  alias :t :r307
75
-
75
+
76
76
  # Creates a rule that will render a file if matched.
77
77
  #
78
- # send_file /*/, 'public/system/maintenance.html',
78
+ # send_file /*/, 'public/system/maintenance.html',
79
79
  # :if => Proc.new { File.exists?('public/system/maintenance.html') }
80
80
  def send_file(*args)
81
81
  add_rule :send_file, *args
82
82
  end
83
-
83
+
84
84
  # Creates a rule that will render a file using x-send-file
85
85
  # if matched.
86
86
  #
87
- # x_send_file /*/, 'public/system/maintenance.html',
87
+ # x_send_file /*/, 'public/system/maintenance.html',
88
88
  # :if => Proc.new { File.exists?('public/system/maintenance.html') }
89
89
  def x_send_file(*args)
90
90
  add_rule :x_send_file, *args
91
91
  end
92
-
92
+
93
93
  private
94
94
  def add_rule(method, from, to, options = {}) #:nodoc:
95
95
  @rules << Rule.new(method.to_sym, from, to, options)
96
96
  end
97
-
97
+
98
98
  end
99
99
 
100
100
  # TODO: Break rules into subclasses
@@ -107,7 +107,7 @@ module Rack
107
107
  def matches?(rack_env) #:nodoc:
108
108
  return false if options[:if].respond_to?(:call) && !options[:if].call(rack_env)
109
109
  path = build_path_from_env(rack_env)
110
-
110
+
111
111
  self.match_options?(rack_env) && string_matches?(path, self.from)
112
112
  end
113
113
 
@@ -123,6 +123,7 @@ module Rack
123
123
  additional_headers = @options[:headers] || {}
124
124
  end
125
125
  end
126
+ status = @options[:status] || 200
126
127
  case self.rule_type
127
128
  when :r301
128
129
  [301, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}.merge!(additional_headers), [redirect_message(interpreted_to)]]
@@ -144,12 +145,12 @@ module Rack
144
145
  end
145
146
  true
146
147
  when :send_file
147
- [200, {
148
+ [status, {
148
149
  'Content-Length' => ::File.size(interpreted_to).to_s,
149
150
  'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))
150
151
  }.merge!(additional_headers), [::File.read(interpreted_to)]]
151
152
  when :x_send_file
152
- [200, {
153
+ [status, {
153
154
  'X-Sendfile' => interpreted_to,
154
155
  'Content-Length' => ::File.size(interpreted_to).to_s,
155
156
  'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))
@@ -158,7 +159,7 @@ module Rack
158
159
  raise Exception.new("Unsupported rule: #{self.rule_type}")
159
160
  end
160
161
  end
161
-
162
+
162
163
  protected
163
164
  def interpret_to(env) #:nodoc:
164
165
  path = build_path_from_env(env)
@@ -166,32 +167,33 @@ module Rack
166
167
  return computed_to(path) if compute_to?(path)
167
168
  self.to
168
169
  end
169
-
170
+
170
171
  def is_a_regexp?(obj)
171
172
  obj.is_a?(Regexp) || (Object.const_defined?(:Oniguruma) && obj.is_a?(Oniguruma::ORegexp))
172
173
  end
173
-
174
+
174
175
  def match_options?(env, path = build_path_from_env(env))
175
176
  matches = []
176
177
  request = Rack::Request.new(env)
177
178
 
178
179
  # negative matches
179
180
  matches << !string_matches?(path, options[:not]) if options[:not]
180
-
181
+
181
182
  # possitive matches
182
183
  matches << string_matches?(env['REQUEST_METHOD'], options[:method]) if options[:method]
183
184
  matches << string_matches?(request.host, options[:host]) if options[:host]
184
-
185
+ matches << string_matches?(request.scheme, options[:scheme]) if options[:scheme]
186
+
185
187
  matches.all?
186
188
  end
187
-
189
+
188
190
  private
189
191
  def normalize_options(arg)
190
192
  options = arg.respond_to?(:call) ? {:if => arg} : arg
191
193
  options.symbolize_keys! if options.respond_to? :symbolize_keys!
192
194
  options.freeze
193
195
  end
194
-
196
+
195
197
  def interpret_to_proc(path, env)
196
198
  return self.to.call(match(path), env) if self.from.is_a?(Regexp)
197
199
  self.to.call(self.from, env)
@@ -201,15 +203,17 @@ module Rack
201
203
  self.is_a_regexp?(from) && match(path)
202
204
  end
203
205
 
204
- def match(path)
206
+ def match(path)
205
207
  self.from.match(path)
206
208
  end
207
-
209
+
208
210
  def string_matches?(string, matcher)
209
211
  if self.is_a_regexp?(matcher)
210
212
  string =~ matcher
211
213
  elsif matcher.is_a?(String)
212
214
  string == matcher
215
+ elsif matcher.is_a?(Symbol)
216
+ string.downcase == matcher.to_s.downcase
213
217
  else
214
218
  false
215
219
  end
@@ -224,14 +228,14 @@ module Rack
224
228
  end
225
229
  return computed_to
226
230
  end
227
-
231
+
228
232
  # Construct the URL (without domain) from PATH_INFO and QUERY_STRING
229
233
  def build_path_from_env(env)
230
- path = env['PATH_INFO']
234
+ path = env['PATH_INFO'] || ''
231
235
  path += "?#{env['QUERY_STRING']}" unless env['QUERY_STRING'].nil? || env['QUERY_STRING'].empty?
232
236
  path
233
237
  end
234
-
238
+
235
239
  def redirect_message(location)
236
240
  %Q(Redirecting to <a href="#{location}">#{location}</a>)
237
241
  end
@@ -18,7 +18,6 @@ Gem::Specification.new do |s|
18
18
  "Rakefile",
19
19
  "VERSION",
20
20
  "Gemfile",
21
- "Gemfile.lock",
22
21
  "lib/rack-rewrite.rb",
23
22
  "lib/rack/rewrite.rb",
24
23
  "lib/rack/rewrite/rule.rb",
@@ -185,6 +185,16 @@ class RuleTest < Test::Unit::TestCase
185
185
  assert_equal [File.read(@file)], @response[2]
186
186
  end
187
187
  end
188
+
189
+ should 'return proper status for send_file or x_send_file if specified' do
190
+ [:send_file, :x_send_file].each do |rule_type|
191
+ file = File.join(TEST_ROOT, 'geminstaller.yml')
192
+ rule = Rack::Rewrite::Rule.new(rule_type, /.*/, file, :status => 503)
193
+ env = {'PATH_INFO' => '/abc'}
194
+ assert_equal 503, rule.apply!(env)[0]
195
+ end
196
+ end
197
+
188
198
  end
189
199
 
190
200
  context 'Rule#matches' do
@@ -254,9 +264,17 @@ class RuleTest < Test::Unit::TestCase
254
264
  end
255
265
  end
256
266
 
257
- should 'match with the ^ operator for regexps' do
258
- rule = Rack::Rewrite::Rule.new(:rewrite, %r{^/jason}, '/steve')
259
- assert rule.matches?(rack_env_for('/jason'))
267
+ context 'Given a rule with the ^ operator' do
268
+ setup do
269
+ @rule = Rack::Rewrite::Rule.new(:rewrite, %r{^/jason}, '/steve')
270
+ end
271
+ should 'match with the ^ operator if match is at the beginning of the path' do
272
+ assert @rule.matches?(rack_env_for('/jason'))
273
+ end
274
+
275
+ should 'not match with the ^ operator when match is deeply nested' do
276
+ assert !@rule.matches?(rack_env_for('/foo/bar/jason'))
277
+ end
260
278
  end
261
279
 
262
280
  context 'Given any rule with a "from" regular expression of /features(.*)' do
@@ -397,6 +415,13 @@ class RuleTest < Test::Unit::TestCase
397
415
  end
398
416
  end
399
417
 
418
+ context 'Mongel 1.2.0.pre2 edge case: root url with a query string' do
419
+ should 'handle a nil PATH_INFO variable without errors' do
420
+ rule = Rack::Rewrite::Rule.new(:r301, '/a', '/')
421
+ assert_equal '?exists', rule.send(:build_path_from_env, {'QUERY_STRING' => 'exists'})
422
+ end
423
+ end
424
+
400
425
  def rack_env_for(url, options = {})
401
426
  components = url.split('?')
402
427
  {'PATH_INFO' => components[0], 'QUERY_STRING' => components[1] || ''}.merge(options)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-rewrite
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.3.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-12-10 00:00:00.000000000 Z
13
+ date: 2013-01-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -91,7 +91,6 @@ files:
91
91
  - Rakefile
92
92
  - VERSION
93
93
  - Gemfile
94
- - Gemfile.lock
95
94
  - lib/rack-rewrite.rb
96
95
  - lib/rack/rewrite.rb
97
96
  - lib/rack/rewrite/rule.rb
@@ -1,23 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- rack-rewrite (1.3.1)
5
-
6
- GEM
7
- remote: http://rubygems.org/
8
- specs:
9
- mocha (0.9.12)
10
- rack (1.4.1)
11
- rake (10.0.2)
12
- shoulda (2.10.3)
13
-
14
- PLATFORMS
15
- ruby
16
-
17
- DEPENDENCIES
18
- bundler
19
- mocha (~> 0.9.7)
20
- rack
21
- rack-rewrite!
22
- rake
23
- shoulda (~> 2.10.2)