Skip to content

Commit

Permalink
Zombie should support gzip and deflate
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed Nov 14, 2012
1 parent ede11c9 commit 7f09294
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 19 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 30 additions & 6 deletions lib/zombie/resources.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -457,6 +479,7 @@ Resources.filters = [
Resources.mergeHeaders
Resources.createBody
Resources.specialURLHandlers
Resources.decompressBody
Resources.decodeBody
]

Expand Down Expand Up @@ -503,6 +526,7 @@ Resources.httpRequest = (request, callback)->
proxy: @browser.proxy
jar: false
followRedirect: false
encoding: null

Request httpRequest, (error, response)=>
if error
Expand Down
65 changes: 54 additions & 11 deletions test/resources_test.coffee
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -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"

Expand All @@ -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", ->
Expand All @@ -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", ->
Expand All @@ -78,17 +80,17 @@ 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
@browser.assert.text "body", "empty"

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"
Expand All @@ -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()

0 comments on commit 7f09294

Please sign in to comment.