Skip to content

Commit

Permalink
fix(hydrate): support headers serialization from string
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanebachelier committed Feb 10, 2016
1 parent 0d97a3a commit 6f3085d
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions lib/hydrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,43 @@ value should have the following format
status: '',
statusText: ''
},
headers: {
// map of HTTP response headers
}
headers: ''
}
*/

// borrow from superagent
function trim (s) {
return ''.trim ? s.trim(s) : s.replace(/(^\s*|\s*$)/g, '')
}

// borrow from superagent
function parseHeader (str) {
var lines = str.split(/\r?\n/)
var fields = {}
var index
var line
var field
var val

lines.pop() // trailing CRLF

for (let i = 0, len = lines.length; i < len; i += 1) {
line = lines[i]
index = line.indexOf(':')
field = line.slice(0, index).toLowerCase()
val = trim(line.slice(index + 1))
fields[field] = val
}

return fields
}

export default function hydrate (value) {
const headers = data.headers || {}
const xhr = value.body || {}
const headers = parseHeader(value.headers || {})

xhr.getAllResponseHeaders = function () {
return headers
return value.headers
}

xhr.getResponseHeader = function (header) {
Expand Down

0 comments on commit 6f3085d

Please sign in to comment.