Permalink
Browse files

Refactored deflater's verification & updated comments.

  • Loading branch information...
1 parent 8ac0558 commit f84b96202ace9bb05f9aacd0aa7a8df6cf16dfbb @jakubpawlowicz committed Jan 22, 2013
Showing with 36 additions and 36 deletions.
  1. +36 −36 test/spec_deflater.rb
View
@@ -54,37 +54,37 @@ def verify(expected_status, expected_body, accept_encoding, options = {}, &block
# verify status
status.should.equal(expected_status)
- # verify headers
- yield(status, headers, body) if block_given?
-
# verify body
- return if options['skip_body_verify']
-
- body_text = ''
- body.each { |part| body_text << part }
-
- deflated_body = case expected_encoding
- when 'deflate'
- inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
- inflater.inflate(body_text) << inflater.finish
- when 'gzip'
- io = StringIO.new(body_text)
- gz = Zlib::GzipReader.new(io)
- tmp = gz.read
- gz.close
- tmp
- else
- body_text
+ unless options['skip_body_verify']
+ body_text = ''
+ body.each { |part| body_text << part }
+
+ deflated_body = case expected_encoding
+ when 'deflate'
+ inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
+ inflater.inflate(body_text) << inflater.finish
+ when 'gzip'
+ io = StringIO.new(body_text)
+ gz = Zlib::GzipReader.new(io)
+ tmp = gz.read
+ gz.close
+ tmp
+ else
+ body_text
+ end
+
+ deflated_body.should.equal(expected_body)
end
- deflated_body.should.equal(expected_body)
+ # yield full response verification
+ yield(status, headers, body) if block_given?
end
should "be able to deflate bodies that respond to each" do
- body = Object.new
- class << body; def each; yield("foo"); yield("bar"); end; end
+ app_body = Object.new
+ class << app_body; def each; yield("foo"); yield("bar"); end; end
- verify(200, 'foobar', 'deflate', { 'app_body' => body }) do |status, headers, body|
+ verify(200, 'foobar', 'deflate', { 'app_body' => app_body }) do |status, headers, body|
headers.should.equal({
"Content-Encoding" => "deflate",
"Vary" => "Accept-Encoding",
@@ -94,10 +94,10 @@ class << body; def each; yield("foo"); yield("bar"); end; end
end
should "flush deflated chunks to the client as they become ready" do
- body = Object.new
- class << body; def each; yield("foo"); yield("bar"); end; end
+ app_body = Object.new
+ class << app_body; def each; yield("foo"); yield("bar"); end; end
- verify(200, body, 'deflate', { 'skip_body_verify' => true }) do |status, headers, body|
+ verify(200, app_body, 'deflate', { 'skip_body_verify' => true }) do |status, headers, body|
headers.should.equal({
"Content-Encoding" => "deflate",
"Vary" => "Accept-Encoding",
@@ -125,10 +125,10 @@ class << body; def each; yield("foo"); yield("bar"); end; end
end
should "be able to gzip bodies that respond to each" do
- body = Object.new
- class << body; def each; yield("foo"); yield("bar"); end; end
+ app_body = Object.new
+ class << app_body; def each; yield("foo"); yield("bar"); end; end
- verify(200, "foobar", 'gzip', { 'app_body' => body }) do |status, headers, body|
+ verify(200, "foobar", 'gzip', { 'app_body' => app_body }) do |status, headers, body|
headers.should.equal({
"Content-Encoding" => "gzip",
"Vary" => "Accept-Encoding",
@@ -138,10 +138,10 @@ class << body; def each; yield("foo"); yield("bar"); end; end
end
should "flush gzipped chunks to the client as they become ready" do
- body = Object.new
- class << body; def each; yield("foo"); yield("bar"); end; end
+ app_body = Object.new
+ class << app_body; def each; yield("foo"); yield("bar"); end; end
- verify(200, body, 'gzip', { 'skip_body_verify' => true }) do |status, headers, body|
+ verify(200, app_body, 'gzip', { 'skip_body_verify' => true }) do |status, headers, body|
headers.should.equal({
"Content-Encoding" => "gzip",
"Vary" => "Accept-Encoding",
@@ -173,19 +173,19 @@ class << body; def each; yield("foo"); yield("bar"); end; end
end
should "handle the lack of an acceptable encoding" do
- body = "Hello world!"
+ app_body = "Hello world!"
not_found_body1 = "An acceptable encoding for the requested resource / could not be found."
not_found_body2 = "An acceptable encoding for the requested resource /foo/bar could not be found."
options1 = {
'app_status' => 200,
- 'app_body' => body,
+ 'app_body' => app_body,
'request_headers' => {
"PATH_INFO" => "/"
}
}
options2 = {
'app_status' => 200,
- 'app_body' => body,
+ 'app_body' => app_body,
'request_headers' => {
"PATH_INFO" => "/foo/bar"
}

0 comments on commit f84b962

Please sign in to comment.