forked from emberjs/data
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test
parseResponseHeaders
function and extract to module
This pulls the `parseResponseHeaders` function out of `DS.RestAdapter` and moves it to its own module. The main reason for this was to make it easier to unit test in order to document current behavior before working on fixing the emberjs#4122
- Loading branch information
1 parent
567dfc5
commit f029211
Showing
3 changed files
with
71 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,27 @@ | ||
import EmptyObject from 'ember-data/-private/system/empty-object'; | ||
|
||
export default function parseResponseHeaders(headersString) { | ||
let headers = new EmptyObject(); | ||
|
||
if (!headersString) { | ||
return headers; | ||
} | ||
|
||
let headerPairs = headersString.split('\u000d\u000a'); | ||
|
||
for (let i = 0; i < headerPairs.length; i++) { | ||
let headerPair = headerPairs[i]; | ||
// Can't use split() here because it does the wrong thing | ||
// if the header value has the string ": " in it. | ||
let index = headerPair.indexOf('\u003a\u0020'); | ||
|
||
if (index > 0) { | ||
let key = headerPair.substring(0, index); | ||
let val = headerPair.substring(index + 2); | ||
|
||
headers[key] = val; | ||
} | ||
} | ||
|
||
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,43 @@ | ||
import EmptyObject from 'ember-data/-private/system/empty-object'; | ||
import parseResponseHeaders from 'ember-data/-private/adapters/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', | ||
'Apple-Field: value with leading whitespace', | ||
'Bear-Field: value that contains: a colon' | ||
].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'); | ||
assert.equal(headers['Apple-Field'], 'value with leading whitespace', 'strips leading whitespace from field-value'); | ||
assert.equal(headers['Bear-Field'], 'value that contains: a colon', 'has correct value when value contins a colon'); | ||
}); | ||
|
||
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'); | ||
}); |