Permalink
Browse files

Added accessors for status code from last respone

(`browser.statusCode`) and whether last response followed a redirect
(`browser.redirected`).
  • Loading branch information...
1 parent 8750a5e commit 9bbd3a043513f443a309e71fd9004a39e0ce7d6e @assaf committed Jan 14, 2011
Showing with 30 additions and 7 deletions.
  1. +5 −2 CHANGELOG.md
  2. +0 −2 TODO.md
  3. +8 −0 doc/API.md
  4. +1 −0 spec/browser-spec.coffee
  5. +1 −0 spec/history-spec.coffee
  6. +12 −0 src/zombie/browser.coffee
  7. +3 −3 src/zombie/history.coffee
View
@@ -15,8 +15,11 @@ as sent to server) (Assaf Arkin).
Added support for `alert`, `confirm` and `prompt` (Assaf Arkin).
- 254 Tests
- 3.6 sec to complete
+Added accessors for status code from last respone (`browser.statusCode`)
+and whether last response followed a redirect (`browser.redirected`).
+
+ 256 Tests
+ 3.5 sec to complete
View
@@ -28,6 +28,4 @@ zombie.js-todo(7) -- Wishlist
* Accessor for HTTP status from last request, also pass back to visit method callback
-* Accessor to determine if last request was a redirect.
-
* Support focus and blur events.
View
@@ -182,6 +182,10 @@ hash (Zombie.js will fire a `hashchange` event), for example:
...
});
+### browser.statusCode : Number
+
+Returns the status code from the last response (200, 304, etc).
+
### browser.visit(url, callback)
### browser.visit(url, options, callback)
@@ -198,6 +202,10 @@ and resets before passing control to the callback. For example:
console.log("The page:", browser.html());
}
);
+
+### browser.redirected : Boolean
+
+Returns true if the last response followed a redirect.
## Forms
View
@@ -116,6 +116,7 @@ vows.describe("Browser").addBatch(
assert.ok jQuery = browser.window.jQuery, "window.jQuery not available"
assert.typeOf jQuery.ajax, "function"
"should run jQuery.onready": (browser)-> assert.equal browser.document.title, "Awesome"
+ "should return status code of last request": (browser)-> assert.equal browser.statusCode, 200
"event emitter":
"successful":
View
@@ -160,4 +160,5 @@ vows.describe("History").addBatch(
"should redirect to final destination": (browser)-> assert.equal browser.location, "http://localhost:3003/boo?redirected=true"
"should pass query parameter": (browser)-> assert.equal browser.text("title"), "Redirected"
"should not add location in history": (browser)-> assert.length browser.window.history, 1
+ "should indicate last request followed a redirect": (browser)-> assert.ok browser.redirected
).export(module)
View
@@ -255,6 +255,18 @@ class Browser extends require("events").EventEmitter
@__defineGetter__ "body", -> window.document?.querySelector("body")
+ # ### browser.statusCode => Number
+ #
+ # Returns the status code of the last response.
+ @__defineGetter__ "statusCode", ->
+ response.status if response = @lastResponse
+ # ### browser.redirected => Boolean
+ #
+ # Returns true if the last response followed a redirect.
+ @__defineGetter__ "redirected", ->
+ response.redirected if response = @lastResponse
+
+
# Navigation
# ----------
@@ -77,7 +77,7 @@ class History
window.document = document
# Make the actual request: called again when dealing with a redirect.
- makeRequest = (url, method, data)=>
+ makeRequest = (url, method, data, redirected)=>
headers = { "user-agent": browser.userAgent }
browser.cookies(url.hostname, url.pathname).addHeader headers
@@ -134,7 +134,7 @@ class History
response.on "data", (chunk)-> body += chunk
response.on "end", =>
browser.response = [response.statusCode, response.headers, body]
- done null, { status: response.statusCode, headers: response.headers, body: body }
+ done null, { status: response.statusCode, headers: response.headers, body: body, redirected: !!redirected }
switch response.statusCode
when 200
browser.cookies(url.hostname, url.pathname).update response.headers["set-cookie"]
@@ -150,7 +150,7 @@ class History
redirect = URL.parse(URL.resolve(url, response.headers["location"]))
stack[index] = new Entry(this, redirect)
browser.emit "redirected", redirect
- process.nextTick -> makeRequest redirect, "GET"
+ process.nextTick -> makeRequest redirect, "GET", null, true
else
error = "Could not load document at #{URL.format(url)}, got #{response.statusCode}"
document.open()

0 comments on commit 9bbd3a0

Please sign in to comment.