Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
[#202] Authorization method changed to scheme (see RFC2617)
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed Nov 22, 2011
1 parent 3fbf323 commit ee58c31
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,8 @@ zombie.js-changelog(7) -- Changelog

Send Content-Length in URL-encoded form requests (Sven Bange).

Added support for HTTP Basic and OAuth 2.0 authorization (Paul Dixon).


## Version 0.11.1 2011-11-21

Expand Down
20 changes: 18 additions & 2 deletions lib/zombie/browser.coffee
Expand Up @@ -18,6 +18,7 @@ Xhr = require("./xhr")


MOUSE_EVENT_NAMES = ["mousedown", "mousemove", "mouseup"]
BROWSER_OPTIONS = ["credentials", "debug", "htmlParser", "loadCSS", "runScripts", "site", "userAgent"]


# Use the browser to open up new windows and load documents.
Expand All @@ -43,7 +44,22 @@ class Browser extends EventEmitter
# Options
# -------

OPTIONS = ["site", "debug", "htmlParser", "loadCSS", "runScripts", "userAgent"]

# ### credentials
#
# Object containing authorization credentials. Supported schemes include
# `basic` (HTTP Basic), `oauth` (OAuth 2.0 draft 10) and `bearer` (OAuth
# 2.0 draft 20). Scheme name is case insensitive.
#
# Example
# creadentials = { scheme: "basic", username: "bloody", password: "hungry" }
# browser.visit("/basic/auth", { creadentials: creadentials }, function(error, browser) {
# })
#
# creadentials = { scheme: "bearer", token: "b1004a8" }
# browser.visit("/oauth/2", { creadentials: creadentials }, function(error, browser) {
# })
@credentials = false

# ### debug
#
Expand Down Expand Up @@ -79,7 +95,7 @@ class Browser extends EventEmitter
# Sets the browser options.
if options
for k,v of options
if OPTIONS.indexOf(k) >= 0
if BROWSER_OPTIONS.indexOf(k) >= 0
@[k] = v
else
throw "I don't recognize the option #{k}"
Expand Down
2 changes: 1 addition & 1 deletion lib/zombie/history.coffee
Expand Up @@ -87,7 +87,7 @@ class History
referer = stack[index-1]?.url

if credentials = browser.credentials
switch credentials.method.toLowerCase()
switch credentials.scheme.toLowerCase()
when "basic"
base64 = new Buffer(credentials.user + ":" + credentials.password).toString("base64")
headers["authorization"] = "Basic #{base64}"
Expand Down
8 changes: 4 additions & 4 deletions spec/browser-spec.coffee
Expand Up @@ -276,14 +276,14 @@ vows.describe("Browser").addBatch(
"with invalid credentials":
topic: ->
browser = new Browser
credentials = { method: "basic", user: "username", password: "wrong" }
credentials = { scheme: "basic", user: "username", password: "wrong" }
browser.visit "http://localhost:3003/auth/basic", credentials: credentials, @callback
"should return status code 401": (browser)->
assert.equal browser.statusCode, 401
"with valid credentials":
topic: ->
browser = new Browser
credentials = { method: "basic", user: "username", password: "pass123" }
credentials = { scheme: "basic", user: "username", password: "pass123" }
browser.visit "http://localhost:3003/auth/basic", credentials: credentials, @callback
"should have the authentication header": (browser)->
assert.equal browser.text("body"), "Basic dXNlcm5hbWU6cGFzczEyMw=="
Expand All @@ -307,14 +307,14 @@ vows.describe("Browser").addBatch(
"with invalid credentials":
topic: ->
browser = new Browser
credentials = { method: "bearer", token: "wrong" }
credentials = { scheme: "bearer", token: "wrong" }
browser.visit "http://localhost:3003/auth/oauth2", credentials: credentials, @callback
"should return status code 401": (browser)->
assert.equal browser.statusCode, 401
"with valid credentials":
topic: ->
browser = new Browser
credentials = { method: "bearer", token: "12345" }
credentials = { scheme: "bearer", token: "12345" }
browser.visit "http://localhost:3003/auth/oauth2", credentials: credentials, @callback
"should have the authentication header": (browser)->
assert.equal browser.text("body"), "Bearer 12345"
Expand Down

0 comments on commit ee58c31

Please sign in to comment.