Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored cookies ahead of adding new import/export functionality.

  • Loading branch information...
commit ec1a51e737114571cc11d8f82ec017d42735ef71 1 parent c22994a
@jadell jadell authored
Showing with 43 additions and 37 deletions.
  1. +20 −18 spec/browser-spec.coffee
  2. +23 −19 src/zombie/cookies.coffee
View
38 spec/browser-spec.coffee
@@ -314,23 +314,25 @@ vows.describe("Browser").addBatch(
assert.match browser.document.title, /pixelDepth=24/
"fork":
- zombie.wants "http://localhost:3003/living"
- "new browser":
- topic: (browser)->
- forked = browser.fork()
- [forked, browser]
- "should have two browser objects": (browsers)->
- [forked, browser] = browsers
- assert.isNotNull forked
- assert.isNotNull browser
- "should not be the same object": (browsers)->
- [forked, browser] = browsers
- assert.notStrictEqual browser, forked
- "should navigate independently": (browsers)->
- [forked, browser] = browsers
- forked.visit "http://localhost:3003/dead"
- forked.wait()
- assert.equal browser.location.href, "http://localhost:3003/living#/"
- assert.equal forked.location, "http://localhost:3003/dead"
+ topic: ->
+ browser = new zombie.Browser
+ browser.visit("http://localhost:3003/living")
+ browser.wait()
+ browser.cookies("www.example").update("foo=bar; domain=www.example")
+ browser.cookies("www.localhost").update("foo=bar; domain=.localhost")
+ forked = browser.fork()
+ [forked, browser]
+ "should not be the same object": ([forked, browser])-> assert.notStrictEqual browser, forked
+ "should have two browser objects": ([forked, browser])->
+ assert.isNotNull forked
+ assert.isNotNull browser
+ "should navigate independently": ([forked, browser])->
+ forked.visit "http://localhost:3003/dead"
+ forked.wait()
+ assert.equal browser.location.href, "http://localhost:3003/living"
+ assert.equal forked.location, "http://localhost:3003/dead"
+ "should manipulate cookies independently": ([forked, browser])->
+ assert.equal "bar", browser.cookies("localhost").get("foo")
+ assert.equal "bar", forked.cookies("localhost").get("foo")
).export(module)
View
42 src/zombie/cookies.coffee
@@ -11,16 +11,33 @@ serialize = (browser, domain, path, name, cookie)->
str = str + "; secure" if cookie.secure
str
+# Unserialize a cookie
+unserialize = (serialized)->
+ fields = serialized.split(/;+/)
+ first = fields[0].trim()
+ [name, value] = first.split(/\=/, 2)
+
+ cookie = name: name, value: value
+ for field in fields
+ [key, val] = field.trim().split(/\=/, 2)
+ switch key.toLowerCase()
+ when "domain" then cookie.domain = dequote(val)
+ when "path" then cookie.path = dequote(val).replace(/%[^\/]*$/, "")
+ when "expires" then cookie.expires = new Date(dequote(val))
+ when "max-age" then cookie['max-age'] = parseInt(dequote(val), 10)
+ when "secure" then cookie.secure = true
+ return cookie
+
+# Cookie header values are (supposed to be) quoted. This function strips
+# double quotes aroud value, if it finds both quotes.
+dequote = (value)-> value.replace(/^"(.*)"$/, "$1")
+
# Maintains cookies for a Browser instance. This is actually a domain/path
# specific scope around the global cookies collection.
class Cookies
constructor: (browser, cookies, hostname, pathname)->
pathname = "/" if !pathname || pathname == ""
- # Cookie header values are (supposed to be) quoted. This function strips
- # double quotes aroud value, if it finds both quotes.
- dequote = (value)-> value.replace(/^"(.*)"$/, "$1")
-
domainMatch = (domain, hostname)->
return true if domain == hostname
return domain.charAt(0) == "." && domain.substring(1) == hostname.replace(/^[^.]+\./, "")
@@ -112,21 +129,8 @@ class Cookies
# Handle case where we get array of headers.
serialized = serialized.join(",") if serialized.constructor == Array
for cookie in serialized.split(/,(?=[^;,]*=)|,$/)
- fields = cookie.split(/;+/)
- first = fields[0].trim()
- [name, value] = first.split(/\=/, 2)
-
- options = { value: value }
- for field in fields
- [key, val] = field.trim().split(/\=/, 2)
- switch key.toLowerCase()
- when "domain" then options.domain = dequote(val)
- when "path" then options.path = dequote(val).replace(/%[^\/]*$/, "")
- when "expires" then options.expires = new Date(dequote(val))
- when "max-age" then options['max-age'] = parseInt(dequote(val), 10)
- when "secure" then options.secure = true
-
- @set(name, value, options)
+ unserialized = unserialize(cookie)
+ @set(unserialized.name, unserialized.value, unserialized)
#### cookies(host, path).addHeader(headers)
#
Please sign in to comment.
Something went wrong with that request. Please try again.