Skip to content

Commit

Permalink
Merge pull request #1280 from AnalyticalGraphicsInc/fixLoadJson
Browse files Browse the repository at this point in the history
loadJson did not properly add the Accept header when no headers were provided.
  • Loading branch information
mramato committed Oct 31, 2013
2 parents 3ef174c + 5dc56de commit 40b58c2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
10 changes: 8 additions & 2 deletions Source/Core/loadJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ define([
DeveloperError) {
"use strict";

var defaultHeaders = {
Accept : 'application/json,*/*;q=0.01'
};

// note: */* below is */* but that ends the comment block early
/**
* Asynchronously loads the given URL as JSON. Returns a promise that will resolve to
Expand Down Expand Up @@ -46,10 +50,12 @@ define([
throw new DeveloperError('url is required.');
}

if (defined(headers) && !defined(headers.Accept)) {
if (!defined(headers)) {
headers = defaultHeaders;
} else if (!defined(headers.Accept)) {
// clone before adding the Accept header
headers = clone(headers);
headers.Accept = 'application/json,*/*;q=0.01';
headers.Accept = defaultHeaders.Accept;
}

return loadText(url, headers).then(function(value) {
Expand Down
21 changes: 17 additions & 4 deletions Specs/Core/loadJsonSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,29 @@ defineSuite([
}).toThrow();
});

it('creates and sends request, adding Accept header', function() {
loadJson("test", {
it('creates and sends request, adding Accept header when headers are provided', function() {
var headers = {
'Cache-Control' : 'no-cache'
});
};
loadJson('test', headers);

expect(fakeXHR.open).toHaveBeenCalledWith('GET', "test", true);
expect(fakeXHR.open).toHaveBeenCalledWith('GET', 'test', true);
expect(fakeXHR.setRequestHeader.callCount).toEqual(2);
expect(fakeXHR.setRequestHeader).toHaveBeenCalledWith('Accept', 'application/json,*/*;q=0.01');
expect(fakeXHR.setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
expect(fakeXHR.send).toHaveBeenCalled();

// the headers object we passed in should not have been modified
expect(Object.keys(headers)).toEqual(['Cache-Control']);
});

it('creates and sends request, adding Accept header when headers are not provided', function() {
loadJson('test');

expect(fakeXHR.open).toHaveBeenCalledWith('GET', 'test', true);
expect(fakeXHR.setRequestHeader.callCount).toEqual(1);
expect(fakeXHR.setRequestHeader).toHaveBeenCalledWith('Accept', 'application/json,*/*;q=0.01');
expect(fakeXHR.send).toHaveBeenCalled();
});

it('returns a promise that resolves when the request loads', function() {
Expand Down

0 comments on commit 40b58c2

Please sign in to comment.