Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added including certain paths from deflating if they match :include r…

…egexp option.
  • Loading branch information...
commit dc5927fc31c86679553202f79db72f9c001eea0c 1 parent 4b4d159
@jakubpawlowicz authored
Showing with 37 additions and 0 deletions.
  1. +8 −0 lib/rack/deflater.rb
  2. +29 −0 test/spec_deflater.rb
View
8 lib/rack/deflater.rb
@@ -10,6 +10,7 @@ def initialize(app, options = {})
@min_content_length = options[:min_content_length] || options['min_content_length']
@skip_if = options[:skip_if] || options['skip_if']
+ @include = options[:include] || options['include']
@exclude = options[:exclude] || options['exclude']
end
@@ -118,6 +119,13 @@ def should_deflate?(env, status, headers, body)
return false
end
+ # Skip if :include is provided and evaluates to false
+ if @include &&
+ @include.kind_of?(Regexp) &&
+ !@include.match(env['PATH_INFO'])
+ return false
+ end
+
# Skip if :exclude is provided and evaluates to true
if @exclude &&
@exclude.kind_of?(Regexp) &&
View
29 test/spec_deflater.rb
@@ -246,6 +246,35 @@ class << body; def each; yield("foo"); yield("bar"); end; end
gz.close
end
+ should "process if path matches :include" do
+ body = 'Hello World!'
+ app = lambda do |env|
+ [200, {'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }, [body]]
+ end
+ request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
+ response = deflater(app, { "include" => /^\/$/ }).call(request)
+
+ response[0].should.equal(200)
+ buf = ''
+ response[2].each { |part| buf << part }
+ io = StringIO.new(buf)
+ gz = Zlib::GzipReader.new(io)
+ gz.read.should.equal(body)
+ gz.close
+ end
+
+ should "skip processing if path do not match :include" do
+ body = 'Hello World!'
+ app = lambda do |env|
+ [200, {'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }, [body]]
+ end
+ request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
+ response = deflater(app, { "include" => /^\/something$/ }).call(request)
+
+ response[0].should.equal(200)
+ Enumerator.new(response[2]).to_a.join.should.equal("Hello World!")
+ end
+
should "skip processing if path matches :exclude" do
body = 'Hello World!'
app = lambda do |env|
Please sign in to comment.
Something went wrong with that request. Please try again.