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

Commit

Permalink
Properly handle repeating headers (e.g. set-cookie) by storing and re…
Browse files Browse the repository at this point in the history
…ading

multiple entries.
  • Loading branch information
assaf committed May 8, 2012
1 parent c7a75c7 commit 45181fa
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Version 1.5.0 2012-05-08

Properly handle repeating headers (e.g. set-cookie) by storing and reading
multiple entries.


## Version 1.4.4 2012-05-02

Filter out request headers *not* response headers.
Expand Down
51 changes: 38 additions & 13 deletions lib/replay/catalog.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,11 @@ class Catalog
try
file = File.createWriteStream(tmpfile, encoding: "utf-8")
file.write "#{request.method.toUpperCase()} #{request.url.path || "/"}\n"
for name, value of request.headers
if ~REQUEST_HEADERS.indexOf(name)
file.write "#{name}: #{value}\n"
writeHeaders file, request.headers, REQUEST_HEADERS
file.write "\n"
# Response part
file.write "#{response.status || 200} HTTP/#{response.version || "1.1"}\n"
for name, value of response.headers
file.write "#{name}: #{value}\n"
writeHeaders file, response.headers
file.write "\n"
for part in response.body
file.write part
Expand All @@ -88,7 +85,6 @@ class Catalog
@_basedir ?= Path.resolve(@settings.fixtures || "fixtures")
return @_basedir


_read: (filename)->
parse_request = (request)->
assert request, "#{filename} missing request section"
Expand Down Expand Up @@ -116,18 +112,47 @@ class Catalog
return { request: parse_request(request), response: parse_response(response, body) }


parseHeaders = (filename, header_lines, restrict = null)->
headers = {}
# Parse headers from header_lines. Optional argument `only` is an array of
# regular expressions; only headers matching one of these expressions are
# parsed. Returns a object with name/value pairs.
parseHeaders = (filename, header_lines, only = null)->
headers = Object.create(null)
for line in header_lines
continue if line == ""
[_, name, value] = line.match(/^(.*?)\:\s+(.*)$/)
assert name && value, "#{filename}: can't make sense of header line #{line}"
if restrict
for regexp in restrict
if regexp.test(name)
continue
headers[name.toLowerCase()] = value.trim().replace(/^"(.*)"$/, "$1")
continue if only && !match(name, only)

key = name.toLowerCase()
value = value.trim().replace(/^"(.*)"$/, "$1")
if Array.isArray(headers[key])
headers[key].push value
else if headers[key]
headers[key] = [headers[key], value]
else
headers[key] = value
return headers


# Write headers to the File object. Optional argument `only` is an array of
# regular expressions; only headers matching one of these expressions are
# written.
writeHeaders = (file, headers, only = null)->
for name, value of headers
continue if only && !match(name, only)
if Array.isArray(value)
for item in value
file.write "#{name}: #{item}\n"
else
file.write "#{name}: #{value}\n"


# Returns true if header name matches one of the regular expressions.
match = (name, regexps)->
for regexp in regexps
if regexp.test(name)
return true
return false


module.exports = Catalog

0 comments on commit 45181fa

Please sign in to comment.