rewrite-tester 0.0.4 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e03d9b04469c37e1360bab69c386edbf104b2773
4
- data.tar.gz: 09c41531e7e412eba4b8c0872aeb8a11fd13ee1c
3
+ metadata.gz: c802a0f6312df1bb99b997bae087306778c85fff
4
+ data.tar.gz: 700d3dc001faff00de90d2994cbc9bc749a1d9c9
5
5
  SHA512:
6
- metadata.gz: 4b8f3e9bf1135e3e9de1b8f2ad804f67d5bc16e0cfb1ab5c33fa0af33faa0442dddc4d8275e3cf57a45a3ee3d876e45983026af983e6f75b7d3d2e89e1457819
7
- data.tar.gz: ab4d281465f251f24148baa1c810e6ea83eff770a5b19d02fafb8a22ec7254ef6a76944ae1cf19215d8399121e94969b2cd8a0f6c2b16157c6cf75bf6b0e4516
6
+ metadata.gz: c07c4f3c966d048fe705b16c221e57ff6c94a4a8df29e6de01937775fafc76464f5a2ae45c432f84ce891bc1970fdf47616f92989462f2a948b8bae9a433690d
7
+ data.tar.gz: 0e4d451fce2db291e9b99ef4ff7771aae7f785723f03e4e18e4bdbdd0419844266eb97c09c3130c705e1c981b4a2a9d142a40ee0ca936c2b9c8ea0e32c3b1650
data/README.md CHANGED
@@ -1,6 +1,8 @@
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)
5
+
4
6
  This tool provides Rspec integration tests to test your server's *url rewriting* capabilities given a set of Apache `RewriteRules`.
5
7
 
6
8
 
data/lib/redirects.rb CHANGED
@@ -1,17 +1,46 @@
1
1
  require 'rewrite_rule'
2
+ require 'rewrite_cond'
2
3
 
3
4
  class Redirects
4
5
 
5
- def initialize(io_instance, http_host = 'example.com')
6
- @rules = []
6
+ HTTP_HOST = ENV['HTTP_HOST'] || 'example.com'
7
+ HTTP_SCHEME = ENV['HTTP_SCHEME'] || 'http://'
7
8
 
9
+ def initialize(io_instance)
10
+ @rules = []
8
11
  unless io_instance.nil?
12
+
13
+ conds = []
14
+
9
15
  io_instance.readlines.each do |line|
10
- @rules << RewriteRule.new(line, http_host)
16
+
17
+ match_data = /RewriteCond/.match(line)
18
+
19
+ unless match_data.nil?
20
+ conds << RewriteCond.new(line)
21
+ next
22
+ end
23
+
24
+ match_data = /RewriteRule/.match(line)
25
+
26
+ unless match_data.nil?
27
+ @rules << RewriteRule.new(line, conds)
28
+ conds = []
29
+ next
30
+ end
11
31
  end
12
32
  end
13
33
  end
14
34
 
15
- attr_accessor :rules
35
+ attr_accessor :rules, :http_host, :http_scheme
36
+
37
+ def self.substitute substitute_rule, substituted_data = []
38
+ substitution = substitute_rule
39
+ substitution = "#{HTTP_SCHEME}#{HTTP_HOST}#{substitution}" unless /^https?\:\/\//.match(substitution)
40
+ substitution = substitution.
41
+ gsub(/\$[0-9]+?/){ |m| !m.nil? && substituted_data[m[1..-1].to_i-1] || '' }.
42
+ gsub('%{HTTP_HOST}', HTTP_HOST).
43
+ gsub('%{HTTP_SCHEME}', HTTP_SCHEME)
44
+ end
16
45
 
17
46
  end
@@ -0,0 +1,17 @@
1
+ class RewriteCond
2
+ def initialize(line)
3
+ @line = line
4
+ self.parse line
5
+ end
6
+
7
+ def parse(line)
8
+ match_data = /RewriteCond[ ]+([^ ]+)[ ]+([^ ]+)([ ]+\[([^ ]+)\]\n?)?$/.match(line)
9
+
10
+ return if match_data.nil?
11
+
12
+ @expression = match_data[1]
13
+ @compare = match_data[2]
14
+ @flgas = match_data[3]
15
+ end
16
+
17
+ end
data/lib/rewrite_rule.rb CHANGED
@@ -1,28 +1,26 @@
1
+ require 'redirects'
2
+
1
3
  class RewriteRule
2
4
 
3
5
  attr_accessor :possibilities, :substitution, :line
4
6
 
5
- def initialize(line, http_host = 'example.com', http_scheme = 'http://')
7
+ def initialize(line, conditions = [])
6
8
  @line = line
7
- @http_host = http_host
8
- @http_scheme = http_scheme
9
+ @conds = conditions
9
10
  self.parse line
10
11
  end
11
12
 
12
13
  # Parse an apache RewriteRule and transform it into a Ruby object
13
14
  def parse(line)
14
-
15
- match_data = /RewriteRule ([^ ]+) ([^ ]+) \[([^ ]+)\](\n)?$/.match(line)
15
+ match_data = /RewriteRule[ ]+([^ ]+)[ ]+([^ ]+)[ ]+\[([^ ]+)\](\n)?$/.match(line)
16
16
 
17
17
  return if match_data.nil?
18
18
 
19
- me = self
20
-
21
19
  @regex = match_data[1]
22
20
  @possibilities = generate_possibilities @regex
23
21
  @substitution = match_data[2]
24
22
  @substitutions = @possibilities.map do |possibility|
25
- me.send(:substitute, @substitution, possibility[:substituted_data])
23
+ ::Redirects.substitute @substitution, possibility[:substituted_data]
26
24
  end
27
25
  @flags = match_data[3].split(',')
28
26
 
@@ -37,11 +35,9 @@ class RewriteRule
37
35
  end
38
36
 
39
37
  def redirects
40
- me = self
41
-
42
38
  @substitutions.map.with_index do |substitution, i|
43
39
  {
44
- :possibility => me.send(:substitute, @possibilities[i][:request_uri]),
40
+ :possibility => ::Redirects.substitute(@possibilities[i][:request_uri]),
45
41
  :substitution => substitution,
46
42
  :code => redirection_code(@flags)
47
43
  }
@@ -61,9 +57,9 @@ class RewriteRule
61
57
  until match_data.nil?
62
58
  matched = match_data[1]
63
59
  if /\.\*/.match(matched)
64
- matched = match_data[0].gsub('.*', generate_random_string(0, 8))
60
+ matched = matched.gsub('.*', generate_random_string(0, 8))
65
61
  elsif /\?$/.match(match_data[0])
66
- matched = rand(2) == 0 ? match_data[1] : ''
62
+ matched = rand(2) == 0 ? matched : ''
67
63
  end
68
64
 
69
65
  base = base.gsub(match_data[0], matched)
@@ -87,13 +83,4 @@ class RewriteRule
87
83
  redirect_regex = /^R=?([0-9]{3})?$/
88
84
  redirect_regex.match(flags.detect { |flag| redirect_regex.match(flag) })[1] || '302'
89
85
  end
90
-
91
- def substitute substitute_rule, substituted_data = []
92
- substitution = substitute_rule
93
- substitution = "#{@http_scheme}#{@http_host}#{substitution}" unless /^https?\:\/\//.match(substitution)
94
- substitution = substitution.
95
- gsub(/\$[0-9]+?/){ |m| !m.nil? && substituted_data[m[1..-1].to_i] || '' }.
96
- gsub('%{HTTP_HOST}', @http_host).
97
- gsub('%{HTTP_SCHEME}', @http_scheme)
98
- end
99
86
  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.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Bonaud
@@ -38,6 +38,48 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
41
83
  description: This tool was built to do full integration testing of HTTP requests
42
84
  email: paul.bonaud@clicrdv.com
43
85
  executables: []
@@ -47,6 +89,7 @@ files:
47
89
  - LICENSE
48
90
  - README.md
49
91
  - lib/redirects.rb
92
+ - lib/rewrite_cond.rb
50
93
  - lib/rewrite_rule.rb
51
94
  homepage: http://rubygems.org/gems/rewrite-tester
52
95
  licenses: