-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4137 from HeroicEric/bugfix-4122
[BUGFIX beta] Allow optional spaces when parsing response headers
- Loading branch information
Showing
3 changed files
with
94 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import EmptyObject from 'ember-data/-private/system/empty-object'; | ||
|
||
const CLRF = '\u000d\u000a'; | ||
|
||
export default function parseResponseHeaders(headersString) { | ||
let headers = new EmptyObject(); | ||
|
||
if (!headersString) { | ||
return headers; | ||
} | ||
|
||
let headerPairs = headersString.split(CLRF); | ||
|
||
headerPairs.forEach((header) => { | ||
let [field, ...value] = header.split(':'); | ||
|
||
field = field.trim(); | ||
value = value.join(':').trim(); | ||
|
||
if (value) { | ||
headers[field] = value; | ||
} | ||
}); | ||
|
||
return headers; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import EmptyObject from 'ember-data/-private/system/empty-object'; | ||
import parseResponseHeaders from 'ember-data/-private/utils/parse-response-headers'; | ||
import { module, test } from 'qunit'; | ||
|
||
const CRLF = '\u000d\u000a'; | ||
|
||
module('unit/adapters/parse-response-headers'); | ||
|
||
test('returns an EmptyObject when headersString is undefined', function(assert) { | ||
let headers = parseResponseHeaders(undefined); | ||
|
||
assert.deepEqual(headers, new EmptyObject(), 'EmptyObject is returned'); | ||
}); | ||
|
||
test('header parsing', function(assert) { | ||
let headersString = [ | ||
'Content-Encoding: gzip', | ||
'content-type: application/json; charset=utf-8', | ||
'date: Fri, 05 Feb 2016 21:47:56 GMT' | ||
].join(CRLF); | ||
|
||
let headers = parseResponseHeaders(headersString); | ||
|
||
assert.equal(headers['Content-Encoding'], 'gzip', 'parses basic header pair'); | ||
assert.equal(headers['content-type'], 'application/json; charset=utf-8', 'parses header with complex value'); | ||
assert.equal(headers['date'], 'Fri, 05 Feb 2016 21:47:56 GMT', 'parses header with date value'); | ||
}); | ||
|
||
test('field-name parsing', function(assert) { | ||
let headersString = [ | ||
' name-with-leading-whitespace: some value', | ||
'name-with-whitespace-before-colon : another value' | ||
].join(CRLF); | ||
|
||
let headers = parseResponseHeaders(headersString); | ||
|
||
assert.equal(headers['name-with-leading-whitespace'], 'some value', 'strips leading whitespace from field-name'); | ||
assert.equal(headers['name-with-whitespace-before-colon'], 'another value', 'strips whitespace before colon from field-name'); | ||
}); | ||
|
||
test('field-value parsing', function(assert) { | ||
let headersString = [ | ||
'value-with-leading-space: value with leading whitespace', | ||
'value-without-leading-space:value without leading whitespace', | ||
'value-with-colon: value with: a colon', | ||
'value-with-trailing-whitespace: banana ' | ||
].join(CRLF); | ||
|
||
let headers = parseResponseHeaders(headersString); | ||
|
||
assert.equal(headers['value-with-leading-space'], 'value with leading whitespace', 'strips leading whitespace in field-value'); | ||
assert.equal(headers['value-without-leading-space'], 'value without leading whitespace', 'works without leaading whitespace in field-value'); | ||
assert.equal(headers['value-with-colon'], 'value with: a colon', 'has correct value when value contains a colon'); | ||
assert.equal(headers['value-with-trailing-whitespace'], 'banana', 'strips trailing whitespace from field-value'); | ||
}); | ||
|
||
test('ignores headers that do not contain a colon', function(assert) { | ||
let headersString = [ | ||
'Content-Encoding: gzip', | ||
'I am ignored because I do not contain a colon' | ||
].join(CRLF); | ||
|
||
let headers = parseResponseHeaders(headersString); | ||
|
||
assert.deepEqual(headers['Content-Encoding'], 'gzip', 'parses basic header pair'); | ||
assert.equal(Object.keys(headers).length, 1, 'only has the one valid header'); | ||
}); |