rewrite-tester 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c802a0f6312df1bb99b997bae087306778c85fff
4
- data.tar.gz: 700d3dc001faff00de90d2994cbc9bc749a1d9c9
3
+ metadata.gz: 3ae93c1fc9b599befce895a0c47deda08744fb52
4
+ data.tar.gz: 2f204d38b3584ea62804be58b2e963d5a63f68e2
5
5
  SHA512:
6
- metadata.gz: c07c4f3c966d048fe705b16c221e57ff6c94a4a8df29e6de01937775fafc76464f5a2ae45c432f84ce891bc1970fdf47616f92989462f2a948b8bae9a433690d
7
- data.tar.gz: 0e4d451fce2db291e9b99ef4ff7771aae7f785723f03e4e18e4bdbdd0419844266eb97c09c3130c705e1c981b4a2a9d142a40ee0ca936c2b9c8ea0e32c3b1650
6
+ metadata.gz: 246a47c50679f2e154b5d4522afa3e88e2e0c104c0df0d0158da5909f4a9244c610301e2c0e844170eb04047740efe5bdf5e5ba0cd7f22a536b068244a2045a8
7
+ data.tar.gz: ecf1ca0230049cf452674a8e9f9a3aeb17ebfa7c5ba9763a9b5643040274b63f76983696b7c9fa9c71097448eb621d065946dbcb16aaea0a04b1e59307d3f2ba
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Rewrite Tester
2
2
  ===
3
3
 
4
- [![Gem Version](https://badge.fury.io/rb/rewrite-tester.svg)](http://badge.fury.io/rb/rewrite-tester) [![Build Status](https://travis-ci.org/popox/rewrite-tester.svg?branch=master)](https://travis-ci.org/popox/rewrite-tester) [![Coverage Status](https://coveralls.io/repos/popox/rewrite-tester/badge.png?branch=master)](https://coveralls.io/r/popox/rewrite-tester?branch=master) [![Dependency Status](https://gemnasium.com/popox/rewrite-tester.svg)](https://gemnasium.com/popox/rewrite-tester)
4
+ [![Gem Version](https://badge.fury.io/rb/rewrite-tester.svg)](http://badge.fury.io/rb/rewrite-tester) [![Code Climate](https://codeclimate.com/github/popox/rewrite-tester.png)](https://codeclimate.com/github/popox/rewrite-tester) [![Dependency Status](https://gemnasium.com/popox/rewrite-tester.svg)](https://gemnasium.com/popox/rewrite-tester)
5
5
 
6
6
  This tool provides Rspec integration tests to test your server's *url rewriting* capabilities given a set of Apache `RewriteRules`.
7
7
 
@@ -33,6 +33,10 @@ Usage
33
33
  $ HTTP_HOST=www.example.com RULES=/path/to//my/rules rake try:redirects
34
34
  ```
35
35
 
36
+ Build status
37
+ ===
38
+ [![Build Status](https://travis-ci.org/popox/rewrite-tester.svg?branch=master)](https://travis-ci.org/popox/rewrite-tester) [![Coverage Status](https://coveralls.io/repos/popox/rewrite-tester/badge.png?branch=master)](https://coveralls.io/r/popox/rewrite-tester?branch=master)
39
+
36
40
  LICENSE
37
41
  ===
38
42
 
@@ -0,0 +1,2 @@
1
+ class InvalidRule < Exception
2
+ end
@@ -8,26 +8,45 @@ class Redirects
8
8
 
9
9
  def initialize(io_instance)
10
10
  @rules = []
11
+ @untestable_rules = []
11
12
  unless io_instance.nil?
12
13
 
13
14
  conds = []
14
15
 
15
- io_instance.readlines.each do |line|
16
+ io_instance.readlines.each.with_index do |line,i|
16
17
 
17
- match_data = /RewriteCond/.match(line)
18
+ begin
19
+ match_data = /^[^#]*RewriteCond/.match(line)
18
20
 
19
- unless match_data.nil?
20
- conds << RewriteCond.new(line)
21
- next
22
- end
21
+ unless match_data.nil?
22
+ conds << RewriteCond.new(line)
23
+ next
24
+ end
23
25
 
24
- match_data = /RewriteRule/.match(line)
26
+ match_data = /^[^#]*RewriteRule/.match(line)
25
27
 
26
- unless match_data.nil?
27
- @rules << RewriteRule.new(line, conds)
28
- conds = []
29
- next
28
+ unless match_data.nil?
29
+ rule = RewriteRule.new(line, conds)
30
+ @rules << rule
31
+ conds = []
32
+ next
33
+ end
34
+ rescue InvalidRule => e
35
+ raise "\e[41mSyntax error on Line #{i+1}: #{line}\e[49m"
36
+ rescue Untestable => e
37
+ @untestable_rules << "(Line #{i+1}) #{/.*(Rewrite.*)/.match(line)[1]}"
30
38
  end
39
+
40
+ end
41
+
42
+ unless @untestable_rules.empty?
43
+ text = <<EOS
44
+ \e[91m
45
+ Rewriting rule with conditions are not supported (for now..), please check the following rules by hand:
46
+ #{@untestable_rules.join("\n")}
47
+ \e[49m
48
+ EOS
49
+ puts text
31
50
  end
32
51
  end
33
52
  end
@@ -35,7 +54,9 @@ class Redirects
35
54
  attr_accessor :rules, :http_host, :http_scheme
36
55
 
37
56
  def self.substitute substitute_rule, substituted_data = []
57
+ return nil if substitute_rule == '-'
38
58
  substitution = substitute_rule
59
+ substitution = "/#{substitution}" if /^(\/|https?:\/\/)/.match(substitution).nil?
39
60
  substitution = "#{HTTP_SCHEME}#{HTTP_HOST}#{substitution}" unless /^https?\:\/\//.match(substitution)
40
61
  substitution = substitution.
41
62
  gsub(/\$[0-9]+?/){ |m| !m.nil? && substituted_data[m[1..-1].to_i-1] || '' }.
@@ -1,7 +1,11 @@
1
+ require 'invalid_rule'
2
+
1
3
  class RewriteCond
2
4
  def initialize(line)
3
5
  @line = line
4
6
  self.parse line
7
+
8
+ raise InvalidRule.new unless valid?
5
9
  end
6
10
 
7
11
  def parse(line)
@@ -11,7 +15,11 @@ class RewriteCond
11
15
 
12
16
  @expression = match_data[1]
13
17
  @compare = match_data[2]
14
- @flgas = match_data[3]
18
+ @flags = match_data[4].nil? ? nil : match_data[4].split(',')
19
+ end
20
+
21
+ def valid?
22
+ !@expression.nil? && !@compare.nil?
15
23
  end
16
24
 
17
25
  end
@@ -1,4 +1,6 @@
1
1
  require 'redirects'
2
+ require 'invalid_rule'
3
+ require 'untestable'
2
4
 
3
5
  class RewriteRule
4
6
 
@@ -7,12 +9,16 @@ class RewriteRule
7
9
  def initialize(line, conditions = [])
8
10
  @line = line
9
11
  @conds = conditions
12
+
10
13
  self.parse line
14
+
15
+ raise Untestable.new unless @conds.empty?
16
+ raise InvalidRule.new unless valid?
11
17
  end
12
18
 
13
19
  # Parse an apache RewriteRule and transform it into a Ruby object
14
20
  def parse(line)
15
- match_data = /RewriteRule[ ]+([^ ]+)[ ]+([^ ]+)[ ]+\[([^ ]+)\](\n)?$/.match(line)
21
+ match_data = /RewriteRule[ ]+([^ ]+)[ ]+([^ ]+)([ ]+\[([^ ]+)\]\n?)?$/.match(line)
16
22
 
17
23
  return if match_data.nil?
18
24
 
@@ -22,12 +28,12 @@ class RewriteRule
22
28
  @substitutions = @possibilities.map do |possibility|
23
29
  ::Redirects.substitute @substitution, possibility[:substituted_data]
24
30
  end
25
- @flags = match_data[3].split(',')
31
+ @flags = match_data[4].nil? ? nil : match_data[4].split(',')
26
32
 
27
33
  end
28
34
 
29
35
  def valid?
30
- !@regex.nil? && !@substitution.nil? && !@flags.nil?
36
+ !@regex.nil? && !@substitution.nil?
31
37
  end
32
38
 
33
39
  def redirection?
@@ -36,35 +42,51 @@ class RewriteRule
36
42
 
37
43
  def redirects
38
44
  @substitutions.map.with_index do |substitution, i|
45
+ request = ::Redirects.substitute(@possibilities[i][:request_uri])
46
+ response = substitution.nil? ? request : substitution
39
47
  {
40
- :possibility => ::Redirects.substitute(@possibilities[i][:request_uri]),
41
- :substitution => substitution,
48
+ :possibility => request,
49
+ :substitution => response,
42
50
  :code => redirection_code(@flags)
43
51
  }
44
52
  end
45
53
  end
46
54
 
55
+ def last?
56
+ !@flags.nil? && @flags.include?('L')
57
+ end
58
+
47
59
  private
48
60
 
49
61
  def generate_possibilities regex
50
62
  some = []
51
63
  base = regex.gsub(/\^|\$/,'')
52
64
 
53
- # Maybe will generate two possibilities, with and without
54
- maybe_regex = /\(([^)^(]+?)\)\??/
55
- match_data = maybe_regex.match(base)
65
+ capture_regex = /\(([^)^(]+?)\)\??/
66
+ match_data = capture_regex.match(base)
56
67
  substituted_data = []
57
68
  until match_data.nil?
58
69
  matched = match_data[1]
59
70
  if /\.\*/.match(matched)
71
+ # Match anything -> Replace by a random string
60
72
  matched = matched.gsub('.*', generate_random_string(0, 8))
61
73
  elsif /\?$/.match(match_data[0])
74
+ # Maybe regex -> Replace by match or by empty string
62
75
  matched = rand(2) == 0 ? matched : ''
63
76
  end
64
77
 
65
78
  base = base.gsub(match_data[0], matched)
66
79
  substituted_data << matched
67
- match_data = maybe_regex.match(base)
80
+ match_data = capture_regex.match(base)
81
+ end
82
+
83
+ any_regex = /(\.\*)/
84
+ match_data = any_regex.match(base)
85
+ until match_data.nil?
86
+ matched = match_data[1]
87
+ matched = matched.gsub('.*', generate_random_string(0, 8))
88
+ base = base.gsub(match_data[0], matched)
89
+ match_data = any_regex.match(base)
68
90
  end
69
91
 
70
92
  some << {
@@ -0,0 +1,2 @@
1
+ class Untestable < Exception
2
+ 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.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Bonaud
@@ -88,9 +88,11 @@ extra_rdoc_files: []
88
88
  files:
89
89
  - LICENSE
90
90
  - README.md
91
+ - lib/invalid_rule.rb
91
92
  - lib/redirects.rb
92
93
  - lib/rewrite_cond.rb
93
94
  - lib/rewrite_rule.rb
95
+ - lib/untestable.rb
94
96
  homepage: http://rubygems.org/gems/rewrite-tester
95
97
  licenses:
96
98
  - MIT