rack_ssi 0.0.8 → 0.0.9
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/Gemfile +0 -7
- data/README.md +8 -6
- data/lib/rack_ssi.rb +23 -17
- data/lib/ssi_processor.rb +23 -18
- data/rack-ssi.gemspec +4 -1
- data/spec/rack_ssi_spec.rb +29 -32
- data/spec/ssi_processor_spec.rb +95 -75
- metadata +36 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0ea47889df434a7d63e1607e87496b0888166e6
|
4
|
+
data.tar.gz: 241f5b00e9c9be5d5f7ec1d32dbe39521dcb6222
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2da9ee3b8f05f98de6796356c3df0d945c2a7ba0eb6e74099ad4d5bd37ef571156e6c589d094c0af8ab7de002e58190df56da8bd242251f2cf593201b8e56c3
|
7
|
+
data.tar.gz: d7a5ebef9d91814659132cd5a423cf8658df37a04fe848548add1f57e28eaef1a2466cc691e21c98a2483752927e10b83689abdf6268b23d67d28b5c433697cc
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -20,7 +20,7 @@ Or install it yourself as:
|
|
20
20
|
## Usage
|
21
21
|
|
22
22
|
require 'rack_ssi'
|
23
|
-
|
23
|
+
|
24
24
|
### Sinatra
|
25
25
|
```ruby
|
26
26
|
configure do
|
@@ -28,14 +28,16 @@ configure do
|
|
28
28
|
:logging => :on,
|
29
29
|
:when => lambda {|env| not env['SOME_CUSTOM_HEADER'] == 'ON'},
|
30
30
|
:locations => {
|
31
|
-
%r{^/includes} => "http://includes.mydomain.com"
|
32
|
-
|
31
|
+
%r{^/includes} => "http://includes.mydomain.com",
|
32
|
+
%r{\A/path/} => ->(location) { "http://host#{rewrite(location)}" },
|
33
|
+
},
|
34
|
+
headers: ->(env) { hash_of_headers(env) }, # by default bypasses Cookies
|
33
35
|
}
|
34
36
|
end
|
35
|
-
```
|
37
|
+
```
|
36
38
|
### Rails
|
37
39
|
```ruby
|
38
|
-
config.middleware.use Rack::SSI, { ... }
|
40
|
+
config.middleware.use Rack::SSI, { ... }
|
39
41
|
```
|
40
42
|
|
41
43
|
#### Haml
|
@@ -47,5 +49,5 @@ To use includes in your HAML, the following should work ok:
|
|
47
49
|
%html{:xmlns => "http://www.w3.org/1999/xhtml"}
|
48
50
|
%head
|
49
51
|
%title My site
|
50
|
-
/ #include file="
|
52
|
+
/ #include file="/includes/header.html"
|
51
53
|
```
|
data/lib/rack_ssi.rb
CHANGED
@@ -5,37 +5,43 @@ require 'logger'
|
|
5
5
|
|
6
6
|
module Rack
|
7
7
|
class SSI
|
8
|
-
|
8
|
+
|
9
|
+
attr_accessor :app, :logging, :predicate, :processor_options
|
10
|
+
|
9
11
|
def initialize(app, options = {})
|
10
12
|
@app = app
|
11
|
-
@logging = options[:logging]
|
12
|
-
@
|
13
|
-
@
|
13
|
+
@logging = options[:logging]
|
14
|
+
@predicate = options[:when] || ->(*) { true}
|
15
|
+
@processor_options = {}
|
16
|
+
processor_options[:locations] = options.fetch(:locations, {})
|
17
|
+
processor_options[:headers] = options.fetch :headers, ->(env) do
|
18
|
+
env['HTTP_COOKIE'] ? {'Cookie' => env['HTTP_COOKIE']} : {}
|
19
|
+
end
|
14
20
|
end
|
15
21
|
|
16
22
|
def call(env)
|
17
|
-
status, headers, body =
|
23
|
+
status, headers, body = app.call(env)
|
18
24
|
unprocessed = [status, headers, body]
|
19
|
-
|
20
|
-
return unprocessed unless
|
25
|
+
|
26
|
+
return unprocessed unless predicate.call(env)
|
21
27
|
return unprocessed unless headers["Content-Type"] && headers["Content-Type"].include?("text/html")
|
22
|
-
|
23
|
-
ssi = Rack::SSIProcessor.new(env)
|
24
|
-
|
25
|
-
ssi.logger = logger(env) if @logging
|
26
|
-
new_body = ssi.process(body)
|
28
|
+
|
29
|
+
ssi = Rack::SSIProcessor.new(env, logging && logger(env), processor_options)
|
30
|
+
new_body = ssi.process(body)
|
27
31
|
headers["Content-Length"] = (new_body.reduce(0) {|sum, part| sum + part.bytesize}).to_s
|
28
32
|
|
29
33
|
[status, headers, new_body]
|
30
34
|
end
|
31
|
-
|
35
|
+
|
36
|
+
|
32
37
|
private
|
38
|
+
|
33
39
|
def logger(env)
|
34
|
-
if defined?(Rails)
|
35
|
-
|
40
|
+
if defined?(Rails) && defined?(Rails.logger)
|
41
|
+
Rails.logger
|
42
|
+
else
|
43
|
+
env['rack.logger']
|
36
44
|
end
|
37
|
-
env['rack.logger']
|
38
45
|
end
|
39
|
-
|
40
46
|
end
|
41
47
|
end
|
data/lib/ssi_processor.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
module Rack
|
2
2
|
class SSIProcessor
|
3
|
-
|
4
|
-
attr_accessor :logger, :locations
|
5
3
|
|
6
|
-
|
4
|
+
attr_accessor :logger, :locations, :env, :options
|
5
|
+
|
6
|
+
def initialize(env, logger = nil, options = {})
|
7
7
|
@env = env
|
8
|
+
@logger = logger
|
9
|
+
@options = options
|
8
10
|
end
|
9
|
-
|
11
|
+
|
10
12
|
def process(body)
|
11
13
|
# see http://wiki.nginx.org/HttpSsiModule
|
12
14
|
# currently only supporting 'block' and 'include' directives
|
@@ -18,7 +20,7 @@ module Rack
|
|
18
20
|
end
|
19
21
|
output
|
20
22
|
end
|
21
|
-
|
23
|
+
|
22
24
|
def process_block(part)
|
23
25
|
part.gsub(/<!--\s?#\s?block\s+name="(\w+)"\s+-->(.*?)<!--\s?#\s+endblock\s+-->/) do
|
24
26
|
name, content = $1, $2
|
@@ -27,40 +29,43 @@ module Rack
|
|
27
29
|
""
|
28
30
|
end
|
29
31
|
end
|
30
|
-
|
32
|
+
|
31
33
|
def process_include(part, blocks)
|
32
34
|
part.gsub(/<!--\s?#\s?include\s+(?:virtual|file)="([^"]+)"(?:\s+stub="(\w+)")?\s+-->/) do
|
33
35
|
location, stub = $1, $2
|
34
36
|
_info "processing include directive with location=#{location}"
|
35
37
|
status, _, body = fetch location
|
36
38
|
if stub && (status != 200 || body.nil? || body == "")
|
37
|
-
blocks[stub]
|
39
|
+
blocks[stub]
|
38
40
|
else
|
39
41
|
body.force_encoding(part.encoding)
|
40
42
|
end
|
41
43
|
end
|
42
44
|
end
|
43
|
-
|
45
|
+
|
44
46
|
def fetch(location)
|
45
|
-
locations.
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
options[:locations].each do |pattern, host|
|
48
|
+
next unless pattern === location
|
49
|
+
target = if host.is_a?(Proc)
|
50
|
+
host.call(location)
|
51
|
+
else
|
52
|
+
"#{host}#{location}"
|
53
|
+
end
|
54
|
+
return _get(target)
|
50
55
|
end
|
51
56
|
_error "no match found for location=#{location}"
|
52
57
|
end
|
53
|
-
|
58
|
+
|
54
59
|
private
|
55
|
-
|
60
|
+
|
56
61
|
def _get(url)
|
57
62
|
_info "fetching #{url}"
|
58
|
-
headers =
|
63
|
+
headers = options[:headers].call(env)
|
59
64
|
response = HTTParty.get(url, headers: headers, verify: false)
|
60
65
|
_error "error fetching #{url}: #{response.code} response" if response.code != 200
|
61
66
|
[response.code, response.headers, response.body]
|
62
67
|
end
|
63
|
-
|
68
|
+
|
64
69
|
def _info(message)
|
65
70
|
logger.info "Rack::SSI #{message}" if logger
|
66
71
|
end
|
@@ -68,6 +73,6 @@ module Rack
|
|
68
73
|
def _error(message)
|
69
74
|
logger.info "Rack::SSI #{message}" if logger
|
70
75
|
end
|
71
|
-
|
76
|
+
|
72
77
|
end
|
73
78
|
end
|
data/rack-ssi.gemspec
CHANGED
@@ -15,6 +15,9 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
16
16
|
gem.name = "rack_ssi"
|
17
17
|
gem.require_paths = ["lib"]
|
18
|
-
gem.version = "0.0.
|
18
|
+
gem.version = "0.0.9"
|
19
19
|
gem.add_dependency "httparty"
|
20
|
+
|
21
|
+
gem.add_development_dependency "rake", '~> 10.4.2'
|
22
|
+
gem.add_development_dependency "rspec", '~> 3.2'
|
20
23
|
end
|
data/spec/rack_ssi_spec.rb
CHANGED
@@ -2,67 +2,64 @@ require File.expand_path('../spec_helper', __FILE__)
|
|
2
2
|
|
3
3
|
describe Rack::SSI do
|
4
4
|
describe "#call" do
|
5
|
-
|
6
5
|
it "should not process response if the when predicate returns false" do
|
7
6
|
app = double
|
8
|
-
body = ["I am a response"]
|
9
|
-
app.
|
7
|
+
body = ["I am a response"]
|
8
|
+
allow(app).to receive(:call).and_return([200, {"Content-Type" => ["text/html"]}, body])
|
10
9
|
rack_ssi = Rack::SSI.new(app, :when => lambda {|env| false })
|
11
|
-
|
12
|
-
Rack::SSIProcessor.
|
13
|
-
|
10
|
+
|
11
|
+
expect_any_instance_of(Rack::SSIProcessor).to_not receive(:process).and_return([""])
|
12
|
+
|
14
13
|
rack_ssi.call({})
|
15
14
|
end
|
16
15
|
|
17
16
|
it "should process response if the when predicate returns true" do
|
18
17
|
app = double
|
19
|
-
body = ["I am a response"]
|
20
|
-
app.
|
18
|
+
body = ["I am a response"]
|
19
|
+
allow(app).to receive(:call).and_return([200, {"Content-Type" => ["text/html"]}, body])
|
21
20
|
rack_ssi = Rack::SSI.new(app, :when => lambda {|env| true })
|
22
|
-
|
23
|
-
Rack::SSIProcessor.
|
24
|
-
|
21
|
+
|
22
|
+
expect_any_instance_of(Rack::SSIProcessor).to receive(:process).with(body).once.and_return([""])
|
23
|
+
|
25
24
|
rack_ssi.call({})
|
26
25
|
end
|
27
|
-
|
26
|
+
|
28
27
|
it "should process html responses only" do
|
29
28
|
app = double
|
30
|
-
body = ["I am a response"]
|
31
|
-
app.
|
29
|
+
body = ["I am a response"]
|
30
|
+
allow(app).to receive(:call).and_return(
|
32
31
|
[200, {"Content-Type" => ["text/html"]}, body],
|
33
32
|
[200, {"Content-Type" => ["text/css"]}, []])
|
34
33
|
rack_ssi = Rack::SSI.new(app)
|
35
|
-
|
36
|
-
Rack::SSIProcessor.
|
37
|
-
|
34
|
+
|
35
|
+
expect_any_instance_of(Rack::SSIProcessor).to receive(:process).with(body).once.and_return([""])
|
36
|
+
|
38
37
|
rack_ssi.call({})
|
39
38
|
end
|
40
|
-
|
39
|
+
|
41
40
|
it "should process 200 responses only" do
|
42
41
|
app = double
|
43
|
-
body = ["I am a response"]
|
44
|
-
app.
|
42
|
+
body = ["I am a response"]
|
43
|
+
allow(app).to receive(:call).and_return(
|
45
44
|
[200, {"Content-Type" => ["text/html"]}, body],
|
46
45
|
[500, {"Content-Type" => ["text/html"]}, []])
|
47
46
|
rack_ssi = Rack::SSI.new(app)
|
48
|
-
|
49
|
-
Rack::SSIProcessor.
|
50
|
-
|
47
|
+
|
48
|
+
expect_any_instance_of(Rack::SSIProcessor).to receive(:process).with(body).once.and_return([""])
|
49
|
+
|
51
50
|
rack_ssi.call({})
|
52
51
|
end
|
53
|
-
|
52
|
+
|
54
53
|
it "should return the processed response and update the Content-Length header" do
|
55
54
|
body = ["I am a response"]
|
56
55
|
app = double(:call => [200, {"Content-Type" => ["text/html"], "Content-Length" => "15"}, body])
|
57
56
|
rack_ssi = Rack::SSI.new(app)
|
58
|
-
Rack::SSIProcessor.
|
59
|
-
|
57
|
+
allow_any_instance_of(Rack::SSIProcessor).to receive(:process) { ["I am a bigger response"] }
|
58
|
+
|
60
59
|
_, headers, new_body = rack_ssi.call({})
|
61
|
-
|
62
|
-
new_body.
|
63
|
-
headers["Content-Length"].
|
60
|
+
|
61
|
+
expect(new_body).to eq ["I am a bigger response"]
|
62
|
+
expect(headers["Content-Length"]).to eq "22"
|
64
63
|
end
|
65
|
-
|
66
64
|
end
|
67
|
-
|
68
|
-
end
|
65
|
+
end
|
data/spec/ssi_processor_spec.rb
CHANGED
@@ -3,11 +3,11 @@
|
|
3
3
|
require File.expand_path('../spec_helper', __FILE__)
|
4
4
|
|
5
5
|
describe Rack::SSIProcessor do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
let(:env) { double('env', :[] => '') }
|
7
|
+
let(:instance) { described_class.new(env, logger, options) }
|
8
|
+
let(:logger) {}
|
9
|
+
let(:options) { {headers: ->(*) { {} }, locations: locations} }
|
10
|
+
let(:locations) { {} }
|
11
11
|
|
12
12
|
describe "#process_block" do
|
13
13
|
it "should yield block directives and strip them out of the html" do
|
@@ -20,7 +20,7 @@ describe Rack::SSIProcessor do
|
|
20
20
|
</body>
|
21
21
|
</html>
|
22
22
|
eos
|
23
|
-
|
23
|
+
|
24
24
|
expected = <<-eos.gsub /\s+/, ""
|
25
25
|
<html>
|
26
26
|
<body>
|
@@ -28,13 +28,13 @@ describe Rack::SSIProcessor do
|
|
28
28
|
</body>
|
29
29
|
</html>
|
30
30
|
eos
|
31
|
-
|
31
|
+
|
32
32
|
blocks = []
|
33
33
|
|
34
|
-
processed =
|
34
|
+
processed = instance.process_block(html) {|block| blocks << block}
|
35
35
|
|
36
|
-
processed.gsub(/\s+/, "").
|
37
|
-
blocks.
|
36
|
+
expect(processed.gsub(/\s+/, "")).to eq expected
|
37
|
+
expect(blocks).to eq [["shush", ""], ["shouty", "<h1>ERROR!</h1>"]]
|
38
38
|
end
|
39
39
|
it "should yield block directives and strip them out of the html from HAML responses" do
|
40
40
|
html = <<-eos
|
@@ -46,7 +46,7 @@ describe Rack::SSIProcessor do
|
|
46
46
|
</body>
|
47
47
|
</html>
|
48
48
|
eos
|
49
|
-
|
49
|
+
|
50
50
|
expected = <<-eos.gsub /\s+/, ""
|
51
51
|
<html>
|
52
52
|
<body>
|
@@ -54,16 +54,16 @@ describe Rack::SSIProcessor do
|
|
54
54
|
</body>
|
55
55
|
</html>
|
56
56
|
eos
|
57
|
-
|
57
|
+
|
58
58
|
blocks = []
|
59
59
|
|
60
|
-
processed =
|
60
|
+
processed = instance.process_block(html) {|block| blocks << block}
|
61
61
|
|
62
|
-
processed.gsub(/\s+/, "").
|
63
|
-
blocks.
|
62
|
+
expect(processed.gsub(/\s+/, "")).to eq expected
|
63
|
+
expect(blocks).to eq [["shush", ""], ["shouty", "<h1>ERROR!</h1>"]]
|
64
64
|
end
|
65
65
|
end
|
66
|
-
|
66
|
+
|
67
67
|
describe "#process_include" do
|
68
68
|
context "the SSI include request returns a valid response" do
|
69
69
|
it "should replace include directives with appropriate content" do
|
@@ -75,7 +75,7 @@ describe Rack::SSIProcessor do
|
|
75
75
|
</body>
|
76
76
|
</html>
|
77
77
|
eos
|
78
|
-
|
78
|
+
|
79
79
|
expected = <<-eos.gsub /\s+/, ""
|
80
80
|
<html>
|
81
81
|
<body>
|
@@ -84,13 +84,13 @@ describe Rack::SSIProcessor do
|
|
84
84
|
</body>
|
85
85
|
</html>
|
86
86
|
eos
|
87
|
-
|
88
|
-
@processor.stub(:fetch).with("/some/location").and_return([200, {}, "<p>some content</p>"])
|
89
|
-
@processor.stub(:fetch).with("/some/other/location").and_return([200, {}, "<p>some more content</p>"])
|
90
87
|
|
91
|
-
|
88
|
+
allow(instance).to receive(:fetch).with("/some/location").and_return([200, {}, "<p>some content</p>"])
|
89
|
+
allow(instance).to receive(:fetch).with("/some/other/location").and_return([200, {}, "<p>some more content</p>"])
|
90
|
+
|
91
|
+
processed = instance.process_include(html, {})
|
92
92
|
|
93
|
-
processed.gsub(/\s+/, "").
|
93
|
+
expect(processed.gsub(/\s+/, "")).to eq expected
|
94
94
|
end
|
95
95
|
it "should replace include directives from HAML response with appropriate content" do
|
96
96
|
html = <<-eos
|
@@ -101,7 +101,7 @@ describe Rack::SSIProcessor do
|
|
101
101
|
</body>
|
102
102
|
</html>
|
103
103
|
eos
|
104
|
-
|
104
|
+
|
105
105
|
expected = <<-eos.gsub /\s+/, ""
|
106
106
|
<html>
|
107
107
|
<body>
|
@@ -110,15 +110,15 @@ describe Rack::SSIProcessor do
|
|
110
110
|
</body>
|
111
111
|
</html>
|
112
112
|
eos
|
113
|
-
|
114
|
-
@processor.stub(:fetch).with("/some/other/location_from_haml").and_return([200, {}, "<p>some content from haml</p>"])
|
115
113
|
|
116
|
-
|
114
|
+
allow(instance).to receive(:fetch).with("/some/other/location_from_haml").and_return([200, {}, "<p>some content from haml</p>"])
|
115
|
+
|
116
|
+
processed = instance.process_include(html, {})
|
117
117
|
|
118
|
-
processed.gsub(/\s+/, "").
|
118
|
+
expect(processed.gsub(/\s+/, "")).to eq expected
|
119
119
|
end
|
120
120
|
end
|
121
|
-
|
121
|
+
|
122
122
|
context "the SSI include request returns an empty response" do
|
123
123
|
it "should replace include directives with the content of the block specified by the 'stub' parameter" do
|
124
124
|
html = <<-eos
|
@@ -128,7 +128,7 @@ describe Rack::SSIProcessor do
|
|
128
128
|
</body>
|
129
129
|
</html>
|
130
130
|
eos
|
131
|
-
|
131
|
+
|
132
132
|
expected = <<-eos.gsub /\s+/, ""
|
133
133
|
<html>
|
134
134
|
<body>
|
@@ -136,14 +136,14 @@ describe Rack::SSIProcessor do
|
|
136
136
|
</body>
|
137
137
|
</html>
|
138
138
|
eos
|
139
|
-
|
140
|
-
@processor.stub(:fetch).with("/some/broken/location").and_return([200, {}, ""])
|
141
139
|
|
142
|
-
|
140
|
+
allow(instance).to receive(:fetch).with("/some/broken/location").and_return([200, {}, ""])
|
143
141
|
|
144
|
-
processed.
|
142
|
+
processed = instance.process_include(html, {"oops" => "<p>oops, something went wrong!</p>"})
|
143
|
+
|
144
|
+
expect(processed.gsub(/\s+/, "")).to eq expected
|
145
145
|
end
|
146
|
-
|
146
|
+
|
147
147
|
it "should replace include directives with the empty response if no 'stub' parameter" do
|
148
148
|
html = <<-eos
|
149
149
|
<html>
|
@@ -152,15 +152,15 @@ describe Rack::SSIProcessor do
|
|
152
152
|
</body>
|
153
153
|
</html>
|
154
154
|
eos
|
155
|
-
|
156
|
-
@processor.stub(:fetch).with("/some/broken/location").and_return([200, {}, ""])
|
157
155
|
|
158
|
-
|
156
|
+
allow(instance).to receive(:fetch).with("/some/broken/location").and_return([200, {}, ""])
|
157
|
+
|
158
|
+
processed = instance.process_include(html, {})
|
159
159
|
|
160
|
-
processed.gsub(/\s+/, "").
|
160
|
+
expect(processed.gsub(/\s+/, "")).to eq "<html><body></body></html>"
|
161
161
|
end
|
162
162
|
end
|
163
|
-
|
163
|
+
|
164
164
|
context "the SSI include request returns an error response" do
|
165
165
|
it "should replace include directives with the content of the block specified by the 'stub' parameter" do
|
166
166
|
html = <<-eos
|
@@ -170,7 +170,7 @@ describe Rack::SSIProcessor do
|
|
170
170
|
</body>
|
171
171
|
</html>
|
172
172
|
eos
|
173
|
-
|
173
|
+
|
174
174
|
expected = <<-eos.gsub /\s+/, ""
|
175
175
|
<html>
|
176
176
|
<body>
|
@@ -179,13 +179,13 @@ describe Rack::SSIProcessor do
|
|
179
179
|
</html>
|
180
180
|
eos
|
181
181
|
|
182
|
-
|
182
|
+
allow(instance).to receive(:fetch).with("/some/broken/location").and_return([500, {}, "<crap>"])
|
183
183
|
|
184
|
-
processed =
|
184
|
+
processed = instance.process_include(html, {"oops" => "<p>oops, something went wrong!</p>"})
|
185
185
|
|
186
|
-
processed.gsub(/\s+/, "").
|
186
|
+
expect(processed.gsub(/\s+/, "")).to eq expected
|
187
187
|
end
|
188
|
-
|
188
|
+
|
189
189
|
it "should replace include directives with the error response if no 'stub' parameter" do
|
190
190
|
html = <<-eos
|
191
191
|
<html>
|
@@ -195,11 +195,11 @@ describe Rack::SSIProcessor do
|
|
195
195
|
</html>
|
196
196
|
eos
|
197
197
|
|
198
|
-
|
198
|
+
allow(instance).to receive(:fetch).with("/some/broken/location").and_return([500, {}, "<bang>"])
|
199
199
|
|
200
|
-
processed =
|
200
|
+
processed = instance.process_include(html, {})
|
201
201
|
|
202
|
-
processed.gsub(/\s+/, "").
|
202
|
+
expect(processed.gsub(/\s+/, "")).to eq "<html><body><bang></body></html>"
|
203
203
|
end
|
204
204
|
end
|
205
205
|
|
@@ -221,38 +221,57 @@ describe Rack::SSIProcessor do
|
|
221
221
|
</html>
|
222
222
|
eos
|
223
223
|
|
224
|
-
|
224
|
+
allow(instance).to receive(:fetch).with("/some/location").and_return([200, {}, "<p>€254</p>".force_encoding("ASCII-8BIT")])
|
225
225
|
|
226
|
-
processed =
|
227
|
-
processed.gsub(/\s+/, "").
|
226
|
+
processed = instance.process_include(html, {})
|
227
|
+
expect(processed.gsub(/\s+/, "")).to eq expected
|
228
228
|
end
|
229
229
|
end
|
230
230
|
end
|
231
|
-
|
231
|
+
|
232
232
|
describe "#fetch" do
|
233
|
-
|
234
|
-
|
235
|
-
/\/
|
236
|
-
|
237
|
-
}
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
233
|
+
context 'when locations contains mathcing string' do
|
234
|
+
let(:locations) { {
|
235
|
+
/\/shorts/ => 'http://host1',
|
236
|
+
'/pants' => 'http://host2'
|
237
|
+
} }
|
238
|
+
|
239
|
+
it 'resolves locations by exact match first' do
|
240
|
+
allow(HTTParty).to receive(:get).
|
241
|
+
and_return(double('response', code: 200, headers: [], body: ''))
|
242
|
+
expect(HTTParty).to receive(:get).with('http://host2/pants', anything)
|
243
|
+
instance.fetch('/pants')
|
244
|
+
end
|
245
|
+
end
|
242
246
|
|
247
|
+
context 'when locations contains matching regex' do
|
248
|
+
let(:locations) { {
|
249
|
+
/^\/pants\/.*/ => 'http://host1',
|
250
|
+
'/pants' => 'http://host2'
|
251
|
+
} }
|
252
|
+
|
253
|
+
it 'resolves locations by regex if no exact match' do
|
254
|
+
allow(HTTParty).to receive(:get).
|
255
|
+
and_return(double('response', code: 200, headers: [], body: ''))
|
256
|
+
expect(HTTParty).to receive(:get).with('http://host1/pants/on/fire', anything)
|
257
|
+
instance.fetch('/pants/on/fire')
|
258
|
+
end
|
243
259
|
end
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
260
|
+
|
261
|
+
context 'when matching location provides proc' do
|
262
|
+
let(:locations) { {
|
263
|
+
/\/pants/ => ->(location) { "http://host1#{location.sub 'ant', 'ub'}"}
|
264
|
+
} }
|
265
|
+
|
266
|
+
it 'resolves locations by regex if no exact match' do
|
267
|
+
allow(HTTParty).to receive(:get).
|
268
|
+
and_return(double('response', code: 200, headers: [], body: ''))
|
269
|
+
expect(HTTParty).to receive(:get).with('http://host1/pubs/on/fire', anything)
|
270
|
+
instance.fetch('/pants/on/fire')
|
271
|
+
end
|
253
272
|
end
|
254
273
|
end
|
255
|
-
|
274
|
+
|
256
275
|
describe "#process" do
|
257
276
|
it "should do it all!" do
|
258
277
|
body = [
|
@@ -262,11 +281,12 @@ describe Rack::SSIProcessor do
|
|
262
281
|
'<!--# include virtual="/includes/header" -->',
|
263
282
|
'</body></html>'
|
264
283
|
]
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
284
|
+
expect(instance).to receive(:fetch).with("/includes/broken").
|
285
|
+
and_return([500, {}, "<p>pants!</p>"])
|
286
|
+
expect(instance).to receive(:fetch).with("/includes/header").
|
287
|
+
and_return([200, {}, "<h1>Hello</h1>"])
|
288
|
+
|
289
|
+
expect(instance.process(body).join).to eq "<html><body><p>ERROR!</p><h1>Hello</h1></body></html>"
|
269
290
|
end
|
270
291
|
end
|
271
|
-
|
272
292
|
end
|
metadata
CHANGED
@@ -1,29 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack_ssi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thibaut Sacreste
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 10.4.2
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 10.4.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
27
55
|
description: |2
|
28
56
|
Rack middleware for processing SSI based on the nginx HttpSsiModule.
|
29
57
|
Directives currently supported: 'block' and 'include'
|
@@ -33,7 +61,7 @@ executables: []
|
|
33
61
|
extensions: []
|
34
62
|
extra_rdoc_files: []
|
35
63
|
files:
|
36
|
-
- .gitignore
|
64
|
+
- ".gitignore"
|
37
65
|
- Gemfile
|
38
66
|
- LICENSE
|
39
67
|
- README.md
|
@@ -54,17 +82,17 @@ require_paths:
|
|
54
82
|
- lib
|
55
83
|
required_ruby_version: !ruby/object:Gem::Requirement
|
56
84
|
requirements:
|
57
|
-
- -
|
85
|
+
- - ">="
|
58
86
|
- !ruby/object:Gem::Version
|
59
87
|
version: '0'
|
60
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
89
|
requirements:
|
62
|
-
- -
|
90
|
+
- - ">="
|
63
91
|
- !ruby/object:Gem::Version
|
64
92
|
version: '0'
|
65
93
|
requirements: []
|
66
94
|
rubyforge_project:
|
67
|
-
rubygems_version: 2.
|
95
|
+
rubygems_version: 2.2.2
|
68
96
|
signing_key:
|
69
97
|
specification_version: 4
|
70
98
|
summary: Rack middleware for processing SSI based on the nginx HttpSsiModule.
|