Skip to content

Commit

Permalink
Added User-Agent string. You can change it by setting the browser
Browse files Browse the repository at this point in the history
option `userAgent`.

There was an error with `browser.location`: documentation said it
returns a `Location` object but also just a URL.  Since `Location`
object is more consistent with `window.location`, accepted that
interpretation.

`Location.assign` did not load a page if the page was already loaded
in the browser.  Changed it to load the page (add caching later on).
  • Loading branch information
assaf committed Dec 30, 2010
1 parent 2decad4 commit 981d8aa
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 14 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,23 @@ zombie.js-changelog(7) -- Changelog
===================================


## Version 0.8.1 2010-12-29

Added User-Agent string. You can change it by setting the browser
option `userAgent`.

There was an error with `browser.location`: documentation said it
returns a `Location` object but also just a URL. Since `Location`
object is more consistent with `window.location`, accepted that
interpretation.

`Location.assign` did not load a page if the page was already loaded
in the browser. Changed it to load the page (add caching later on).

196 Tests
2.6 sec to complete


## Version 0.8.0 2010-12-29

Fixed issue 8, wrong location of package.json.
Expand Down
3 changes: 0 additions & 3 deletions TODO.md
Expand Up @@ -9,7 +9,4 @@ zombie.js-todo(7) -- Wishlist
* Time and timezone: within window context, new Date() should use browser clock
and timezone; allow changing browser timezone and default to system's.

* User agent: allow setting of user agent; brower sends user agent in all
requests (pages, forms and XHR).

* Prompts: handle window.confirm and window.alert.
3 changes: 2 additions & 1 deletion doc/API.md
Expand Up @@ -24,6 +24,7 @@ You can use the following options:
false.
- `runScripts` -- Run scripts included in or loaded from the page.
Defaults to true.
- `userAgent` -- The User-Agent string to send to the server.

### Browser.visit(url, callback)
### Browser.visit(url, options, callback)
Expand Down Expand Up @@ -144,7 +145,7 @@ For example:

### browser.location : Location

Return the location of the current document (same as `window.location.href`).
Return the location of the current document (same as `window.location`).

### browser.location = url

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "zombie",
"version": "0.8.0",
"version": "0.8.1",
"description": "Insanely fast, full-stack, headless testing using node.js",
"homepage": "http://zombie.labnotes.org/",
"author": "Assaf Arkin <assaf@labnotes.org> (http://labnotes.org/)",
Expand Down
12 changes: 12 additions & 0 deletions spec/browser-spec.coffee
Expand Up @@ -102,6 +102,8 @@ brains.get "/script/append", (req, res)-> res.send """
</html>
"""

brains.get "/useragent", (req, res)-> res.send "<body>#{req.headers["user-agent"]}</body>"


vows.describe("Browser").addBatch(
"open page":
Expand Down Expand Up @@ -207,4 +209,14 @@ vows.describe("Browser").addBatch(
"should set options for the duration of the request": (browser)-> assert.equal browser.document.title, "Whatever"
"should reset options following the request": (browser)-> assert.isTrue browser.runScripts

"user agent":
topic: ->
browser = new zombie.Browser
browser.wants "http://localhost:3003/useragent", @callback
"should send own version": (browser)-> assert.match browser.text("body"), /Zombie.js\/\d\.\d/
"specified":
topic: (browser)->
browser.visit "http://localhost:3003/useragent", { userAgent: "imposter" }, @callback
"should send user agent to browser": (browser)-> assert.equal browser.text("body"), "imposter"

).export(module)
17 changes: 10 additions & 7 deletions src/zombie/browser.coffee
Expand Up @@ -38,16 +38,20 @@ class Browser extends require("events").EventEmitter
# Options
# -------

@OPTIONS = ["debug", "runScripts"]
@OPTIONS = ["debug", "runScripts", "userAgent"]

# ### debug
#
# True to have Zombie report what it's doing.
@debug = false
# ### runScripts
#
# Run scripts included in or loaded from the page. Defaults to true.
@runScripts = true
# ### debug
# ### userAgent
#
# True to have Zombie report what it's doing.
@debug = false
# User agent string sent to server.
@userAgent = "Mozilla/5.0 Chrome/10.0.613.0 Safari/534.15 Zombie.js/#{exports.version}"

# ### withOptions(options, fn)
#
Expand Down Expand Up @@ -227,8 +231,8 @@ class Browser extends require("events").EventEmitter

# ### browser.location => Location
#
# Return the location of the current document (same as `window.location.href`).
@__defineGetter__ "location", -> window.location.href
# Return the location of the current document (same as `window.location`).
@__defineGetter__ "location", -> window.location
# ### browser.location = url
#
# Changes document location, loads new document if necessary (same as
Expand Down Expand Up @@ -495,7 +499,6 @@ class Browser extends require("events").EventEmitter

exports.Browser = Browser


# ### zombie.version : String
try
exports.package = JSON.parse(require("fs").readFileSync(__dirname + "/../../package.json"))
Expand Down
5 changes: 4 additions & 1 deletion src/zombie/history.coffee
Expand Up @@ -43,6 +43,9 @@ class History
evt = browser.document.createEvent("HTMLEvents")
evt.initEvent "hashchange", true, false
browser.window.dispatchEvent evt
else
# Load new page for now (but later on use caching).
resource url

# Make a request to external resource. We use this to fetch pages and
# submit forms, see _loadPage and _submit.
Expand Down Expand Up @@ -72,7 +75,7 @@ class History

# Make the actual request: called again when dealing with a redirect.
makeRequest = (url, method, data)=>
headers = {}
headers = { "User-Agent": browser.userAgent }
browser.cookies(url.hostname, url.pathname).addHeader headers
if method == "GET" || method == "HEAD"
url.search = "?" + qs.stringify(data) if data
Expand Down
2 changes: 1 addition & 1 deletion src/zombie/xhr.coffee
Expand Up @@ -45,7 +45,7 @@ XMLHttpRequest = (browser, window)->
reset()

# Allow setting headers in this state.
headers = {}
headers = { "User-Agent": browser.userAgent }
@setRequestHeader = (header, value)-> headers[header.toString().toLowerCase()] = value.toString()
# Allow calling send method.
@send = (data)->
Expand Down

0 comments on commit 981d8aa

Please sign in to comment.