rewrite-tester 0.0.3 → 0.0.4

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -10
  3. data/lib/redirects.rb +1 -11
  4. data/lib/rewrite_rule.rb +42 -18
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 763e235adb195c5ca273470564ebd7b0d9c484b9
4
- data.tar.gz: e94fe3e995481687c8a7d229d4857c8a2d18a2b3
3
+ metadata.gz: e03d9b04469c37e1360bab69c386edbf104b2773
4
+ data.tar.gz: 09c41531e7e412eba4b8c0872aeb8a11fd13ee1c
5
5
  SHA512:
6
- metadata.gz: 860246b0af05c765a004a38950c2795d5d62f72b0b8c2526469ec99cbfb759a2fefcf0daf443bbbf17047e5db9854b0552a34bef7a9e8e77f2e89f1aa75b75f4
7
- data.tar.gz: d973871a71c7d4f1190fda33926b2eecce77d7c45a33d7d4d3241bd4101eb1938bb86801053258394e269d3d41e84c8bf437a95334280efdbddfc88e0752feff
6
+ metadata.gz: 4b8f3e9bf1135e3e9de1b8f2ad804f67d5bc16e0cfb1ab5c33fa0af33faa0442dddc4d8275e3cf57a45a3ee3d876e45983026af983e6f75b7d3d2e89e1457819
7
+ data.tar.gz: ab4d281465f251f24148baa1c810e6ea83eff770a5b19d02fafb8a22ec7254ef6a76944ae1cf19215d8399121e94969b2cd8a0f6c2b16157c6cf75bf6b0e4516
data/README.md CHANGED
@@ -13,24 +13,24 @@ Usage
13
13
 
14
14
  - Clone the repository and use the default rake task to run the tests. You will need to give the absolute path of a file containing a list of `RewriteRules` through a `RULES` env variable. Additionally you will need to provide a `HTTP_HOST` env variable, containing the host that you are testing against.
15
15
 
16
- `bash
17
- $ HTTP_HOST=www.example.com RULES=/path/to//my/rules rake
18
- `
19
-
16
+ ```bash
17
+ $ HTTP_HOST=www.example.com RULES=/path/to//my/rules rake try:redirects
18
+ ```
19
+
20
20
  - From your project
21
21
 
22
22
  - Include the gem in your `Gemfile`
23
23
 
24
- `ruby
24
+ ```ruby
25
25
  gem 'rewrite-tester'
26
- `
26
+ ```
27
27
 
28
28
  - Use the `test:redirects` rake task by giving a absolute file path to the `RULES` env variable.
29
29
 
30
- `bash
31
- $ HTTP_HOST=www.example.com RULES=/path/to//my/rules rake test:redirects
32
- `
33
-
30
+ ```bash
31
+ $ HTTP_HOST=www.example.com RULES=/path/to//my/rules rake try:redirects
32
+ ```
33
+
34
34
  LICENSE
35
35
  ===
36
36
 
data/lib/redirects.rb CHANGED
@@ -11,17 +11,7 @@ class Redirects
11
11
  end
12
12
  end
13
13
  end
14
-
15
- # Increments the rul reading
16
- def has_next?
17
- !@rules.empty?
18
- end
19
-
20
- # Returns a rule
21
- def next
22
- @rules.pop
23
- end
24
-
14
+
25
15
  attr_accessor :rules
26
16
 
27
17
  end
data/lib/rewrite_rule.rb CHANGED
@@ -16,9 +16,14 @@ class RewriteRule
16
16
 
17
17
  return if match_data.nil?
18
18
 
19
+ me = self
20
+
19
21
  @regex = match_data[1]
20
22
  @possibilities = generate_possibilities @regex
21
- @substitution = substitute match_data[2]
23
+ @substitution = match_data[2]
24
+ @substitutions = @possibilities.map do |possibility|
25
+ me.send(:substitute, @substitution, possibility[:substituted_data])
26
+ end
22
27
  @flags = match_data[3].split(',')
23
28
 
24
29
  end
@@ -32,43 +37,62 @@ class RewriteRule
32
37
  end
33
38
 
34
39
  def redirects
35
- @possibilities.map { |possibility|
36
- { possibility => { :substitution => @substitution, :code => redirection_code(@flags) } }
37
- }
40
+ me = self
41
+
42
+ @substitutions.map.with_index do |substitution, i|
43
+ {
44
+ :possibility => me.send(:substitute, @possibilities[i][:request_uri]),
45
+ :substitution => substitution,
46
+ :code => redirection_code(@flags)
47
+ }
48
+ end
38
49
  end
39
50
 
51
+ private
52
+
40
53
  def generate_possibilities regex
41
54
  some = []
42
55
  base = regex.gsub(/\^|\$/,'')
43
56
 
44
57
  # Maybe will generate two possibilities, with and without
45
- maybe_regex = /\(([^*+]+)\)\?/
58
+ maybe_regex = /\(([^)^(]+?)\)\??/
46
59
  match_data = maybe_regex.match(base)
47
- if match_data.nil?
48
- some << base
49
- else
50
- some << base.gsub(match_data[0], match_data[1])
51
- some << base.gsub(match_data[0], '')
60
+ substituted_data = []
61
+ until match_data.nil?
62
+ matched = match_data[1]
63
+ if /\.\*/.match(matched)
64
+ matched = match_data[0].gsub('.*', generate_random_string(0, 8))
65
+ elsif /\?$/.match(match_data[0])
66
+ matched = rand(2) == 0 ? match_data[1] : ''
67
+ end
68
+
69
+ base = base.gsub(match_data[0], matched)
70
+ substituted_data << matched
71
+ match_data = maybe_regex.match(base)
52
72
  end
53
-
54
- # Anything will be replaced by nothing
55
- some.map { |possibility|
56
- "#{@http_scheme}#{@http_host}#{possibility.gsub('(.*)', '')}"
73
+
74
+ some << {
75
+ :request_uri => "#{base}",
76
+ :substituted_data => substituted_data
57
77
  }
58
78
  end
59
79
 
60
- private
80
+ # Generate a string containing downcase letters, with a random length included
81
+ # between min and max
82
+ def generate_random_string min, max
83
+ (min...rand(max)).map { (97 + rand(26)).chr }.join
84
+ end
61
85
 
62
86
  def redirection_code flags
63
87
  redirect_regex = /^R=?([0-9]{3})?$/
64
88
  redirect_regex.match(flags.detect { |flag| redirect_regex.match(flag) })[1] || '302'
65
89
  end
66
90
 
67
- def substitute substitute_rule
91
+ def substitute substitute_rule, substituted_data = []
68
92
  substitution = substitute_rule
69
- substitution = "#{@http_scheme}#{@http_host}#{substitution}" if /^\//.match(substitution)
93
+ substitution = "#{@http_scheme}#{@http_host}#{substitution}" unless /^https?\:\/\//.match(substitution)
70
94
  substitution = substitution.
71
- gsub(/\$[0-9]+/, '').
95
+ gsub(/\$[0-9]+?/){ |m| !m.nil? && substituted_data[m[1..-1].to_i] || '' }.
72
96
  gsub('%{HTTP_HOST}', @http_host).
73
97
  gsub('%{HTTP_SCHEME}', @http_scheme)
74
98
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rewrite-tester
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Bonaud