From 981d8aa0b006310b85a9bf297fe493ac403abf3f Mon Sep 17 00:00:00 2001 From: Assaf Arkin Date: Wed, 29 Dec 2010 20:47:03 -0800 Subject: [PATCH] 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). --- CHANGELOG.md | 17 +++++++++++++++++ TODO.md | 3 --- doc/API.md | 3 ++- package.json | 2 +- spec/browser-spec.coffee | 12 ++++++++++++ src/zombie/browser.coffee | 17 ++++++++++------- src/zombie/history.coffee | 5 ++++- src/zombie/xhr.coffee | 2 +- 8 files changed, 47 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ecf60a39b..047ca63fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/TODO.md b/TODO.md index b01aae6bb..a83c38f95 100644 --- a/TODO.md +++ b/TODO.md @@ -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. diff --git a/doc/API.md b/doc/API.md index 2dcfa7600..b51b70383 100644 --- a/doc/API.md +++ b/doc/API.md @@ -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) @@ -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 diff --git a/package.json b/package.json index dbda153a0..570fef856 100644 --- a/package.json +++ b/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 (http://labnotes.org/)", diff --git a/spec/browser-spec.coffee b/spec/browser-spec.coffee index aced094ac..e726903f1 100644 --- a/spec/browser-spec.coffee +++ b/spec/browser-spec.coffee @@ -102,6 +102,8 @@ brains.get "/script/append", (req, res)-> res.send """ """ +brains.get "/useragent", (req, res)-> res.send "#{req.headers["user-agent"]}" + vows.describe("Browser").addBatch( "open page": @@ -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) diff --git a/src/zombie/browser.coffee b/src/zombie/browser.coffee index 7fd60634e..b8e96339a 100644 --- a/src/zombie/browser.coffee +++ b/src/zombie/browser.coffee @@ -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) # @@ -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 @@ -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")) diff --git a/src/zombie/history.coffee b/src/zombie/history.coffee index 2f554dc5c..dc687989c 100644 --- a/src/zombie/history.coffee +++ b/src/zombie/history.coffee @@ -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. @@ -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 diff --git a/src/zombie/xhr.coffee b/src/zombie/xhr.coffee index 4493c8674..7264fa0c8 100644 --- a/src/zombie/xhr.coffee +++ b/src/zombie/xhr.coffee @@ -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)->