Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Zombie should support gzip and deflate

  • Loading branch information...
commit 7f0929409d28ece58ef7d4a9ff81811de7479673 1 parent ede11c9
@assaf authored
View
12 CHANGELOG.md
@@ -51,8 +51,16 @@ Introducing assertsions to make your life all the more easier, for example:
browser.elements("li", { atLeast: 5 })
browser.assert.url("http://example.com")
- 560 tests
- 10.9 sec to complete
+Added support for deflat and gzip responses.
+
+New API for accessing resources, use `browser.resources`. Lots of goodies like
+allowing you to retrieve resources directory (`resources.get`,
+`resources.post`), define how resources are handled, emulate server failures
+(`resources.fail`), delay responses (`resources.delay`), even mock responses
+(`resources.mock`).
+
+ 566 tests
+ 11.1 sec to complete
## Version 1.4.1 2012-08-22
View
36 lib/zombie/resources.coffee
@@ -94,12 +94,13 @@
# to the browser object.
-File = require("fs")
-HTML = require("jsdom").dom.level3.html
-Path = require("path")
-QS = require("querystring")
-Request = require("request")
-URL = require("url")
+File = require("fs")
+HTML = require("jsdom").dom.level3.html
+Path = require("path")
+QS = require("querystring")
+Request = require("request")
+URL = require("url")
+Zlib = require("zlib")
# Each browser has a resources object that provides the means for retrieving
@@ -434,6 +435,27 @@ Resources.specialURLHandlers = (request, next)->
next()
+
+Resources.decompressBody = (request, response, next)->
+ if response.body && response.headers
+ transferEncoding = response.headers["transfer-encoding"]
+ switch transferEncoding
+ when "deflate"
+ Zlib.inflate response.body, (error, buffer)->
+ unless error
+ response.body = buffer
+ next(error)
+ when "gzip"
+ Zlib.gunzip response.body, (error, buffer)->
+ unless error
+ response.body = buffer
+ next(error)
+ else
+ next()
+ return
+
+
+
# This filter decodes the response body based on the response content type.
Resources.decodeBody = (request, response, next)->
# Use content type to determine how to decode response
@@ -457,6 +479,7 @@ Resources.filters = [
Resources.mergeHeaders
Resources.createBody
Resources.specialURLHandlers
+ Resources.decompressBody
Resources.decodeBody
]
@@ -503,6 +526,7 @@ Resources.httpRequest = (request, callback)->
proxy: @browser.proxy
jar: false
followRedirect: false
+ encoding: null
Request httpRequest, (error, response)=>
if error
View
65 test/resources_test.coffee
@@ -1,10 +1,12 @@
{ assert, brains, Browser } = require("./helpers")
+File = require("fs")
+Zlib = require("zlib")
describe "Resources", ->
before (done)->
- brains.get "/browser/resource", (req, res)->
+ brains.get "/resources/resource", (req, res)->
res.send """
<html>
<head>
@@ -27,12 +29,12 @@ describe "Resources", ->
describe "as array", ->
before (done)->
@browser = new Browser()
- @browser.visit "http://localhost:3003/browser/resource", done
+ @browser.visit "/resources/resource", done
it "should have a length", ->
assert.equal @browser.resources.length, 2
it "should include loaded page", ->
- assert.equal @browser.resources[0].response.url, "http://localhost:3003/browser/resource"
+ assert.equal @browser.resources[0].response.url, "http://localhost:3003/resources/resource"
it "should include loaded JavaScript", ->
assert.equal @browser.resources[1].response.url, "http://localhost:3003/jquery-1.7.1.js"
@@ -43,8 +45,8 @@ describe "Resources", ->
describe "fail URL", ->
before (done)->
@browser = new Browser()
- @browser.resources.fail("http://localhost:3003/browser/resource", "Fail!")
- @browser.visit "http://localhost:3003/browser/resource", (@error)=>
+ @browser.resources.fail("http://localhost:3003/resource/resource", "Fail!")
+ @browser.visit "/resource/resource", (@error)=>
done()
it "should fail the request", ->
@@ -57,8 +59,8 @@ describe "Resources", ->
describe "delay URL with timeout", ->
before (done)->
@browser = new Browser()
- @browser.resources.delay("http://localhost:3003/browser/resource", 100)
- @browser.visit "http://localhost:3003/browser/resource"
+ @browser.resources.delay("http://localhost:3003/resources/resource", 100)
+ @browser.visit "/resources/resource"
@browser.wait duration: 90, done
it "should not load page", ->
@@ -78,8 +80,8 @@ describe "Resources", ->
describe "mock URL", ->
before (done)->
@browser = new Browser()
- @browser.resources.mock("http://localhost:3003/browser/resource", statusCode: 204, body: "empty")
- @browser.visit "http://localhost:3003/browser/resource", done
+ @browser.resources.mock("http://localhost:3003/resources/resource", statusCode: 204, body: "empty")
+ @browser.visit "/resources/resource", done
it "should return mock result", ->
@browser.assert.status 204
@@ -87,8 +89,8 @@ describe "Resources", ->
describe "restore", ->
before (done)->
- @browser.resources.restore("http://localhost:3003/browser/resource")
- @browser.visit "http://localhost:3003/browser/resource", done
+ @browser.resources.restore("http://localhost:3003/resources/resource")
+ @browser.visit "/resources/resource", done
it "should return actual page", ->
@browser.assert.text "title", "Awesome"
@@ -97,3 +99,44 @@ describe "Resources", ->
@browser.destroy()
+ describe "deflate", ->
+ before ->
+ brains.get "/resources/deflate", (req, res)->
+ res.setHeader "Transfer-Encoding", "deflate"
+ image = File.readFileSync("#{__dirname}/data/zombie.jpg")
+ Zlib.deflate image, (error, buffer)->
+ res.send(buffer)
+
+ before (done)->
+ @browser = new Browser()
+ @browser.resources.get "http://localhost:3003/resources/deflate", (error, @response)=>
+ done()
+
+ it "should uncompress deflated response", ->
+ image = File.readFileSync("#{__dirname}/data/zombie.jpg")
+ assert.deepEqual image, @response.body
+
+ after ->
+ @browser.destroy()
+
+
+ describe "gzip", ->
+ before ->
+ brains.get "/resources/gzip", (req, res)->
+ res.setHeader "Transfer-Encoding", "gzip"
+ image = File.readFileSync("#{__dirname}/data/zombie.jpg")
+ Zlib.gzip image, (error, buffer)->
+ res.send(buffer)
+
+ before (done)->
+ @browser = new Browser()
+ @browser.resources.get "http://localhost:3003/resources/gzip", (error, @response)=>
+ done()
+
+ it "should uncompress gzipped response", ->
+ image = File.readFileSync("#{__dirname}/data/zombie.jpg")
+ assert.deepEqual image, @response.body
+
+ after ->
+ @browser.destroy()
+
Please sign in to comment.
Something went wrong with that request. Please try again.