|
| 1 | +import EmptyObject from 'ember-data/-private/system/empty-object'; |
| 2 | +import parseResponseHeaders from 'ember-data/-private/utils/parse-response-headers'; |
| 3 | +import { module, test } from 'qunit'; |
| 4 | + |
| 5 | +const CRLF = '\u000d\u000a'; |
| 6 | + |
| 7 | +module('unit/adapters/parse-response-headers'); |
| 8 | + |
| 9 | +test('returns an EmptyObject when headersString is undefined', function(assert) { |
| 10 | + let headers = parseResponseHeaders(undefined); |
| 11 | + |
| 12 | + assert.deepEqual(headers, new EmptyObject(), 'EmptyObject is returned'); |
| 13 | +}); |
| 14 | + |
| 15 | +test('header parsing', function(assert) { |
| 16 | + let headersString = [ |
| 17 | + 'Content-Encoding: gzip', |
| 18 | + 'content-type: application/json; charset=utf-8', |
| 19 | + 'date: Fri, 05 Feb 2016 21:47:56 GMT' |
| 20 | + ].join(CRLF); |
| 21 | + |
| 22 | + let headers = parseResponseHeaders(headersString); |
| 23 | + |
| 24 | + assert.equal(headers['Content-Encoding'], 'gzip', 'parses basic header pair'); |
| 25 | + assert.equal(headers['content-type'], 'application/json; charset=utf-8', 'parses header with complex value'); |
| 26 | + assert.equal(headers['date'], 'Fri, 05 Feb 2016 21:47:56 GMT', 'parses header with date value'); |
| 27 | +}); |
| 28 | + |
| 29 | +test('field-name parsing', function(assert) { |
| 30 | + let headersString = [ |
| 31 | + ' name-with-leading-whitespace: some value', |
| 32 | + 'name-with-whitespace-before-colon : another value' |
| 33 | + ].join(CRLF); |
| 34 | + |
| 35 | + let headers = parseResponseHeaders(headersString); |
| 36 | + |
| 37 | + assert.equal(headers['name-with-leading-whitespace'], 'some value', 'strips leading whitespace from field-name'); |
| 38 | + assert.equal(headers['name-with-whitespace-before-colon'], 'another value', 'strips whitespace before colon from field-name'); |
| 39 | +}); |
| 40 | + |
| 41 | +test('field-value parsing', function(assert) { |
| 42 | + let headersString = [ |
| 43 | + 'value-with-leading-space: value with leading whitespace', |
| 44 | + 'value-without-leading-space:value without leading whitespace', |
| 45 | + 'value-with-colon: value with: a colon', |
| 46 | + 'value-with-trailing-whitespace: banana ' |
| 47 | + ].join(CRLF); |
| 48 | + |
| 49 | + let headers = parseResponseHeaders(headersString); |
| 50 | + |
| 51 | + assert.equal(headers['value-with-leading-space'], 'value with leading whitespace', 'strips leading whitespace in field-value'); |
| 52 | + assert.equal(headers['value-without-leading-space'], 'value without leading whitespace', 'works without leaading whitespace in field-value'); |
| 53 | + assert.equal(headers['value-with-colon'], 'value with: a colon', 'has correct value when value contains a colon'); |
| 54 | + assert.equal(headers['value-with-trailing-whitespace'], 'banana', 'strips trailing whitespace from field-value'); |
| 55 | +}); |
| 56 | + |
| 57 | +test('ignores headers that do not contain a colon', function(assert) { |
| 58 | + let headersString = [ |
| 59 | + 'Content-Encoding: gzip', |
| 60 | + 'I am ignored because I do not contain a colon' |
| 61 | + ].join(CRLF); |
| 62 | + |
| 63 | + let headers = parseResponseHeaders(headersString); |
| 64 | + |
| 65 | + assert.deepEqual(headers['Content-Encoding'], 'gzip', 'parses basic header pair'); |
| 66 | + assert.equal(Object.keys(headers).length, 1, 'only has the one valid header'); |
| 67 | +}); |
0 commit comments