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.
- checksums.yaml +4 -4
- data/README.md +10 -10
- data/lib/redirects.rb +1 -11
- data/lib/rewrite_rule.rb +42 -18
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e03d9b04469c37e1360bab69c386edbf104b2773
|
4
|
+
data.tar.gz: 09c41531e7e412eba4b8c0872aeb8a11fd13ee1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
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
|
-
|
31
|
-
$ HTTP_HOST=www.example.com RULES=/path/to//my/rules rake
|
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
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 =
|
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
|
-
|
36
|
-
|
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
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|
-
|
55
|
-
|
56
|
-
|
73
|
+
|
74
|
+
some << {
|
75
|
+
:request_uri => "#{base}",
|
76
|
+
:substituted_data => substituted_data
|
57
77
|
}
|
58
78
|
end
|
59
79
|
|
60
|
-
|
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}"
|
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
|