rewritten 0.15.2 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,16 +1,14 @@
1
1
  require 'test_helper'
2
2
 
3
3
  describe Rack::Rewritten::Url do
4
-
5
- before {
4
+ before do
6
5
  Rewritten.add_translation '/foo/bar', '/products/1'
7
6
  Rewritten.add_translation '/foo/baz', '/products/1'
8
7
  Rewritten.add_translation '/foo/with/params', '/products/2?w=1'
9
- }
10
-
11
- describe "substition behavior" do
8
+ end
12
9
 
13
- before {
10
+ describe 'substition behavior' do
11
+ before do
14
12
  @html_body = <<-HTML
15
13
  <html>
16
14
  <body>
@@ -25,25 +23,22 @@ describe Rack::Rewritten::Url do
25
23
  </html>
26
24
  HTML
27
25
 
28
- @rack = Rack::Rewritten::Html.new(lambda{|env| [200, {}, [@html_body]]})
29
- }
26
+ @rack = Rack::Rewritten::Html.new(->(_env) { [200, {}, [@html_body]] })
27
+ end
30
28
 
31
- it "must ignore everything" do
32
- res,env,body = @rack.call({})
33
- html = body.join("")
29
+ it 'must ignore everything' do
30
+ _res, _env, body = @rack.call({})
31
+ html = body.join('')
34
32
  html.must_equal @html_body
35
33
  end
36
34
 
37
- it "must replace links with translations" do
38
- Rewritten.add_translation('/bar', '/foo')
39
- res,env,body = @rack.call({})
40
- html = body.join("")
41
- assert ! (html =~ (/foo\??["']/) ), "didn't replace all /foo links"
42
- assert html.include? "/bar"
43
- assert html.include?("/foolmenot"), "must not mess with similar links"
35
+ it 'must replace links with translations' do
36
+ Rewritten.add_translation('/bar', '/foo')
37
+ _res, _env, body = @rack.call({})
38
+ html = body.join('')
39
+ assert ! (html =~ (/foo\??["']/)), "didn't replace all /foo links"
40
+ assert html.include? '/bar'
41
+ assert html.include?('/foolmenot'), 'must not mess with similar links'
44
42
  end
45
-
46
43
  end
47
-
48
44
  end
49
-
@@ -1,88 +1,84 @@
1
1
  require 'test_helper'
2
2
 
3
3
  describe Rack::Rewritten::Url do
4
-
5
- def call_args(overrides={})
6
- {'HTTP_HOST' => 'www.example.org',
4
+ def call_args(overrides = {})
5
+ { 'HTTP_HOST' => 'www.example.org',
7
6
  'REQUEST_URI' => '/foo/with/params',
8
- 'SCRIPT_INFO'=> '',
7
+ 'SCRIPT_INFO' => '',
9
8
  'PATH_INFO' => '/foo/with/params',
10
9
  'QUERY_STRING' => '',
11
10
  'SERVER_PORT' => 80,
12
11
  'rack.input' => '',
13
- 'rack.url_scheme' => 'http'}.merge(overrides)
12
+ 'rack.url_scheme' => 'http' }.merge(overrides)
14
13
  end
15
14
 
16
- def request_url(url, params={})
17
- call_args.merge({'REQUEST_URI' => url, 'PATH_INFO' => url}.merge(params) )
15
+ def request_url(url, params = {})
16
+ call_args.merge({ 'REQUEST_URI' => url, 'PATH_INFO' => url }.merge(params))
18
17
  end
19
18
 
20
- before {
19
+ before do
21
20
  Rewritten.add_translation '/foo/bar', '/products/1'
22
21
  Rewritten.add_translation '/foo/baz', '/products/1'
23
22
  Rewritten.add_translation '/foo/with/params', '/products/2?w=1'
24
- }
25
-
26
- describe "redirection behavior" do
23
+ Rewritten.add_translation '/foo/with/params?w=1', '/embed/2'
24
+ end
27
25
 
28
- before {
26
+ describe 'redirection behavior' do
27
+ before do
29
28
  @app = MiniTest::Mock.new
30
29
  @rack = Rack::Rewritten::Url.new(@app)
31
30
 
32
31
  Rewritten.add_translation '/foo/bar', '/products/1'
33
32
  Rewritten.add_translation '/foo/baz', '/products/1'
34
33
  Rewritten.add_translation '/foo/with/params', '/products/2?w=1'
35
- }
34
+ end
36
35
 
37
- it "must not redirect if there are no entries" do
38
- @app.expect :call, [200, {'Content-Type' => 'text/plain'},[""]], [Hash]
36
+ it 'must not redirect if there are no entries' do
37
+ @app.expect :call, [200, { 'Content-Type' => 'text/plain' }, ['']], [Hash]
39
38
  @rack.call(call_args)
40
39
  @app.verify
41
40
  end
42
41
 
43
- it "must 301 redirect from old translation to latest translation" do
42
+ it 'must 301 redirect from old translation to latest translation' do
44
43
  ret = @rack.call request_url('/foo/bar')
45
44
  @app.verify
46
45
  ret[0].must_equal 301
47
- ret[1]['Location'].must_equal "http://www.example.org/foo/baz"
46
+ ret[1]['Location'].must_equal 'http://www.example.org/foo/baz'
48
47
  end
49
48
 
50
- it "must keep the query parameters in the 301 redirect" do
49
+ it 'must keep the query parameters in the 301 redirect' do
51
50
  ret = @rack.call request_url('/foo/bar', 'QUERY_STRING' => 'w=1')
52
51
  @app.verify
53
52
  ret[0].must_equal 301
54
- ret[1]['Location'].must_equal "http://www.example.org/foo/baz?w=1"
53
+ ret[1]['Location'].must_equal 'http://www.example.org/foo/baz?w=1'
55
54
  end
56
55
 
57
- it "must stay on latest translation" do
58
- @app.expect :call, [200, {'Content-Type' => 'text/plain'},[""]], [Hash]
56
+ it 'must stay on latest translation' do
57
+ @app.expect :call, [200, { 'Content-Type' => 'text/plain' }, ['']], [Hash]
59
58
  ret = @rack.call request_url('/foo/baz')
60
59
  @app.verify
61
60
  ret[0].must_equal 200
62
61
  end
63
62
 
64
63
  describe 'external redirection' do
65
-
66
- before {
64
+ before do
67
65
  @app = MiniTest::Mock.new
68
66
  @rack = Rack::Rewritten::Url.new(@app) do |config|
69
67
  config.base_url = 'http://www.example.org'
70
68
  end
71
69
 
72
70
  Rewritten.add_translation '/external/target', 'http://www.external.com'
73
- }
71
+ end
74
72
 
75
- it "must redirect to external target" do
73
+ it 'must redirect to external target' do
76
74
  ret = @rack.call request_url('/external/target')
77
75
  @app.verify
78
76
  ret[0].must_equal 301
79
- ret[1]['Location'].must_equal "http://www.external.com"
77
+ ret[1]['Location'].must_equal 'http://www.external.com'
80
78
  end
81
-
82
79
  end
83
80
 
84
- describe "partial translation" do
85
-
81
+ describe 'partial translation' do
86
82
  before do
87
83
  @request_str = '/foo/baz/with_tail'
88
84
  @env = request_url(@request_str)
@@ -93,73 +89,68 @@ describe Rack::Rewritten::Url do
93
89
  Rewritten.translate_partial = false
94
90
  end
95
91
 
96
- after{ Rewritten.translate_partial = false }
92
+ after { Rewritten.translate_partial = false }
97
93
 
98
- it "must not translate partials by default" do
94
+ it 'must not translate partials by default' do
99
95
  Rewritten.translate_partial = false
100
- @app.expect :call, [200, {'Content-Type' => 'text/plain'},[""]], [Hash]
101
- ret = @rack.call @env
96
+ @app.expect :call, [200, { 'Content-Type' => 'text/plain' }, ['']], [Hash]
97
+ @rack.call @env
102
98
  @app.verify
103
99
  @env['PATH_INFO'].must_equal @request_str
104
100
  end
105
101
 
106
- it "must translate partials if enabled" do
102
+ it 'must translate partials if enabled' do
107
103
  Rewritten.translate_partial = true
108
- @app.expect :call, [200, {'Content-Type' => 'text/html'},[]], [Hash]
109
- ret = @rack.call @env
104
+ @app.expect :call, [200, { 'Content-Type' => 'text/html' }, []], [Hash]
105
+ @rack.call @env
110
106
  @app.verify
111
107
  @env['PATH_INFO'].must_equal '/products/1/with_tail'
112
108
  end
113
109
 
114
- it "must work on long, non-translated urls with partial translation enabled" do
115
-
116
- @app.expect :call, [200, {'Content-Type' => 'text/html'},[]], [Hash]
110
+ it 'must work on long, non-translated urls with partial translation enabled' do
111
+ @app.expect :call, [200, { 'Content-Type' => 'text/html' }, []], [Hash]
117
112
 
118
113
  url = '/such/a/long/url/with/so/many/slashes/oh/my/god'
119
114
  @env = request_url(url)
120
115
 
121
- ret = @rack.call @env
116
+ @rack.call @env
122
117
  @app.verify
123
118
  @env['PATH_INFO'].must_equal url
124
119
  end
125
120
 
126
-
127
121
  it "won't translate segments not by separated by slashes" do
128
- @app.expect :call, [200, {'Content-Type' => 'text/plain'},[""]], [Hash]
129
- ret = @rack.call @env=request_url('/foo/bazzling')
122
+ @app.expect :call, [200, { 'Content-Type' => 'text/plain' }, ['']], [Hash]
123
+ @rack.call @env = request_url('/foo/bazzling')
130
124
  @app.verify
131
125
  @env['PATH_INFO'].must_equal '/foo/bazzling'
132
126
  end
133
127
 
134
- it "must carry on trail when redirecting" do
128
+ it 'must carry on trail when redirecting' do
135
129
  ret = @rack.call request_url('/foo/bar/with_tail', 'QUERY_STRING' => 'w=1')
136
130
  @app.verify
137
131
  ret[0].must_equal 301
138
- ret[1]['Location'].must_equal "http://www.example.org/foo/baz/with_tail?w=1"
132
+ ret[1]['Location'].must_equal 'http://www.example.org/foo/baz/with_tail?w=1'
139
133
  end
140
134
  end
141
135
 
142
- describe "/ behavior" do
143
-
144
- it "must 301 redirect paths with / in the end to their chomped version" do
136
+ describe '/ behavior' do
137
+ it 'must 301 redirect paths with / in the end to their chomped version' do
145
138
  ret = @rack.call request_url('/foo/bar/')
146
139
  @app.verify
147
140
  ret[0].must_equal 301
148
- ret[1]['Location'].must_equal "http://www.example.org/foo/baz"
141
+ ret[1]['Location'].must_equal 'http://www.example.org/foo/baz'
149
142
  end
150
-
151
143
  end
152
144
 
153
- describe "caps behavior" do
154
-
155
- it "must diferentiate caps" do
156
- @app.expect :call, [200, {'Content-Type' => 'text/plain'},[""]], [Hash]
145
+ describe 'caps behavior' do
146
+ it 'must diferentiate caps' do
147
+ @app.expect :call, [200, { 'Content-Type' => 'text/plain' }, ['']], [Hash]
157
148
  ret = @rack.call request_url('/Foo/Bar')
158
149
  @app.verify
159
150
  ret[0].must_equal 200
160
151
  end
161
152
 
162
- it "must ignore caps if wished" do
153
+ it 'must ignore caps if wished' do
163
154
  @rack = Rack::Rewritten::Url.new(@app) do
164
155
  self.downcase_before_lookup = true
165
156
  end
@@ -168,22 +159,19 @@ describe Rack::Rewritten::Url do
168
159
 
169
160
  @app.verify
170
161
  ret[0].must_equal 301
171
- ret[1]['Location'].must_equal "http://www.example.org/foo/baz"
162
+ ret[1]['Location'].must_equal 'http://www.example.org/foo/baz'
172
163
  end
173
-
174
164
  end
175
165
 
176
-
177
- describe "enforce nice urls" do
178
-
179
- it "must not redirect from resource url to nice url by default" do
180
- @app.expect :call, [200, {'Content-Type' => 'text/plain'},[""]], [Hash]
166
+ describe 'enforce nice urls' do
167
+ it 'must not redirect from resource url to nice url by default' do
168
+ @app.expect :call, [200, { 'Content-Type' => 'text/plain' }, ['']], [Hash]
181
169
  ret = @rack.call request_url('/products/1')
182
170
  @app.verify
183
171
  ret[0].must_equal 200
184
172
  end
185
173
 
186
- it "must redirect from resource url to nice url if enabled" do
174
+ it 'must redirect from resource url to nice url if enabled' do
187
175
  @rack = Rack::Rewritten::Url.new(@app) do
188
176
  self.translate_backwards = true
189
177
  end
@@ -191,78 +179,72 @@ describe Rack::Rewritten::Url do
191
179
  ret = @rack.call request_url('/products/1')
192
180
  @app.verify
193
181
  ret[0].must_equal 301
194
- ret[1]['Location'].must_equal "http://www.example.org/foo/baz"
182
+ ret[1]['Location'].must_equal 'http://www.example.org/foo/baz'
195
183
  end
196
-
197
184
  end
198
185
 
199
- describe "flag behavior" do
200
-
201
- before {
186
+ describe 'flag behavior' do
187
+ before do
202
188
  Rewritten.add_translation('/with/flags [L]', '/adwords/target')
203
189
  Rewritten.add_translation('/with/flags2 [L]', '/adwords/target')
204
190
  Rewritten.add_translation('/no/flags', '/adwords/target')
205
191
  Rewritten.add_translation('/final/line', '/adwords/target')
206
- }
192
+ end
207
193
 
208
- it "must stay on [L] flagged froms" do
209
- @app.expect :call, [200, {'Content-Type' => 'text/plain'},[""]], [Hash]
210
- @app.expect :call, [200, {'Content-Type' => 'text/plain'},[""]], [Hash]
194
+ it 'must stay on [L] flagged froms' do
195
+ @app.expect :call, [200, { 'Content-Type' => 'text/plain' }, ['']], [Hash]
196
+ @app.expect :call, [200, { 'Content-Type' => 'text/plain' }, ['']], [Hash]
211
197
 
212
- ret = @rack.call( request_url('/with/flags'))
198
+ ret = @rack.call(request_url('/with/flags'))
213
199
  ret[0].must_equal 200
214
200
 
215
- ret = @rack.call( request_url('/with/flags2'))
201
+ ret = @rack.call(request_url('/with/flags2'))
216
202
  ret[0].must_equal 200
217
203
 
218
204
  @app.verify
219
205
  end
220
206
 
221
- it "must redirect for other entries" do
207
+ it 'must redirect for other entries' do
222
208
  ret = @rack.call request_url('/no/flags')
223
209
  @app.verify
224
210
  ret[0].must_equal 301
225
- ret[1]['Location'].must_equal "http://www.example.org/final/line"
211
+ ret[1]['Location'].must_equal 'http://www.example.org/final/line'
226
212
  end
227
-
228
213
  end
229
-
230
214
  end
231
215
 
232
-
233
-
234
- describe "the env" do
235
-
236
- before {
216
+ describe 'the env' do
217
+ before do
237
218
  @initial_args = call_args.dup
238
- @rack = Rack::Rewritten::Url.new(lambda{|env| [200, {}, [""]]})
239
- }
219
+ @rack = Rack::Rewritten::Url.new(->(_env) { [200, {}, ['']] })
220
+ end
240
221
 
241
- it "must set PATH_INFO to /products/2" do
222
+ it 'must set PATH_INFO to /products/2' do
242
223
  @rack.call(@initial_args)
243
- @initial_args['PATH_INFO'].must_equal "/products/2"
224
+ @initial_args['PATH_INFO'].must_equal '/products/2'
244
225
  end
245
226
 
246
- it "must set QUERY_STRING to w=1" do
227
+ it 'must set QUERY_STRING to w=1' do
247
228
  @rack.call(@initial_args)
248
229
  @initial_args['QUERY_STRING'].must_equal 'w=1'
249
230
  end
250
231
 
251
- it "must merge QUERY parameters" do
232
+ it 'must merge QUERY parameters' do
252
233
  @initial_args.merge!('QUERY_STRING' => 's=1')
253
234
  @rack.call(@initial_args)
254
235
  @initial_args['QUERY_STRING'].split('&').sort.must_equal ['s=1', 'w=1']
255
236
  end
256
237
 
257
- it "must merge nested rails style QUERY parameters" do
238
+ it 'must merge nested rails style QUERY parameters' do
258
239
  @initial_args.merge!('QUERY_STRING' => 'x[id]=1')
259
240
  @rack.call(@initial_args)
260
- @initial_args['QUERY_STRING'].split('&').sort.map{|s| URI.unescape(s)}.must_equal ['w=1', 'x[id]=1']
241
+ @initial_args['QUERY_STRING'].split('&').sort.map { |s| URI.unescape(s) }.must_equal ['w=1', 'x[id]=1']
261
242
  end
262
243
 
263
-
244
+ it 'must discriminate between explicit query string translations' do
245
+ @initial_args.merge!('QUERY_STRING' => 'w=1')
246
+ @rack.call(@initial_args)
247
+ @initial_args['PATH_INFO'].must_equal '/embed/2'
248
+ end
264
249
  end
265
-
266
-
267
-
268
250
  end
@@ -1,84 +1,84 @@
1
1
  require 'test_helper'
2
2
 
3
3
  describe Rewritten::Document do
4
-
5
4
  before do
6
5
  class Product
7
6
  include Rewritten::Document
8
7
  end
9
8
  @instance = Product.new
10
- def @instance.id; 123; end
11
- def @instance.polymorphic_url(object, options={}); "/products/#{self.id}"; end
12
- def @instance.persisted?; true; end
9
+ def @instance.id
10
+ 123
11
+ end
12
+ def @instance.polymorphic_url(_object, _options = {})
13
+ "/products/#{id}"
14
+ end
15
+ def @instance.persisted?
16
+ true
17
+ end
13
18
  end
14
19
 
15
20
  it 'must add the rewritten methods to the class' do
16
21
  @instance.respond_to?(:rewritten_url=).must_equal true
17
22
  @instance.respond_to?(:rewritten_url).must_equal true
18
- @instance.respond_to?(:has_rewritten_url?).must_equal true
23
+ @instance.respond_to?(:rewritten_url?).must_equal true
19
24
  end
20
25
 
21
26
  it 'must return empty string when not persisted' do
27
+ def @instance.persisted?
28
+ false
29
+ end
22
30
 
23
- def @instance.persisted?; false; end
24
-
25
- @instance.rewritten_url.must_equal ""
31
+ @instance.rewritten_url.must_equal ''
26
32
  end
27
33
 
28
34
  describe 'add translation' do
29
-
30
35
  describe 'url_for not overriden' do
31
36
  it 'must return translation when persisted' do
32
- Rewritten.add_translation('/foo/bar', '/products/123')
37
+ Rewritten.add_translation('/foo/bar', '/products/123')
33
38
  @instance.rewritten_url.must_equal '/foo/bar'
34
39
  end
35
40
  end
36
41
 
37
42
  describe 'url_to overriden' do
38
43
  it 'must return translation when persisted' do
39
- Rewritten.add_translation('/foo/bar', '/products/123')
40
- def @instance.polymorphic_url(object, options={}); '/foo/bar'; end
44
+ Rewritten.add_translation('/foo/bar', '/products/123')
45
+ def @instance.polymorphic_url(_object, _options = {})
46
+ '/foo/bar'
47
+ end
41
48
  Rewritten.translate(@instance.rewritten_url).must_equal '/products/123'
42
49
  end
43
50
  end
44
-
45
51
  end
46
52
 
47
53
  it 'must return all translations as array' do
48
-
49
54
  Rewritten.add_translation('/foo/bar', '/products/123')
50
55
  Rewritten.add_translation('/foo/baz', '/products/123')
51
56
 
52
- @instance.rewritten_urls.must_equal ['/foo/bar', '/foo/baz']
57
+ @instance.rewritten_urls.must_equal ['/foo/bar', '/foo/baz']
53
58
  end
54
59
 
55
60
  it 'must add a new translation' do
56
- @instance.has_rewritten_url?.must_equal false
57
- @instance.rewritten_url = '/foo/baz'
61
+ @instance.rewritten_url?.must_equal false
62
+ @instance.rewritten_url = '/foo/baz'
58
63
  Rewritten.get_current_translation('/products/123').must_equal '/foo/baz'
59
- @instance.has_rewritten_url?.must_equal true
64
+ @instance.rewritten_url?.must_equal true
60
65
  end
61
66
 
62
67
  it 'must remove all translation' do
63
- @instance.rewritten_url = '/foo/bar'
64
- @instance.rewritten_url = '/foo/baz'
68
+ @instance.rewritten_url = '/foo/bar'
69
+ @instance.rewritten_url = '/foo/baz'
65
70
  @instance.remove_rewritten_urls
66
71
  @instance.rewritten_urls.must_equal []
67
72
  end
68
73
 
69
74
  it 'must won\'t add blank and similar translations' do
70
- @instance.rewritten_url = '/foo/bar'
75
+ @instance.rewritten_url = '/foo/bar'
71
76
  @instance.rewritten_urls.must_equal ['/foo/bar']
72
77
  @instance.rewritten_url = nil
73
78
  @instance.rewritten_urls.must_equal ['/foo/bar']
74
- @instance.rewritten_url = ""
79
+ @instance.rewritten_url = ''
75
80
  @instance.rewritten_urls.must_equal ['/foo/bar']
76
- @instance.rewritten_url = "/foo/bar"
81
+ @instance.rewritten_url = '/foo/bar'
77
82
  @instance.rewritten_urls.must_equal ['/foo/bar']
78
83
  end
79
-
80
84
  end
81
-
82
-
83
-
84
-