Merge "Use Accept JSON Header when fetching detail, account"

This commit is contained in:
Ben Rohlfs
2019-08-22 12:20:06 +00:00
committed by Gerrit Code Review
2 changed files with 37 additions and 7 deletions

View File

@@ -349,11 +349,7 @@
* @param {Defs.FetchJSONRequest} req
*/
_fetchJSON(req) {
if (!req.fetchOptions) req.fetchOptions = {};
if (!req.fetchOptions.headers) req.fetchOptions.headers = new Headers();
if (!req.fetchOptions.headers.has('Accept')) {
req.fetchOptions.headers.append('Accept', 'application/json');
}
req = this._addAcceptJsonHeader(req);
return this._fetchRawJSON(req).then(response => {
if (!response) {
return;
@@ -425,6 +421,19 @@
return JSON.parse(source.substring(JSON_PREFIX.length));
},
/**
* @param {Defs.FetchJSONRequest} req
* @return {Defs.FetchJSONRequest}
*/
_addAcceptJsonHeader(req) {
if (!req.fetchOptions) req.fetchOptions = {};
if (!req.fetchOptions.headers) req.fetchOptions.headers = new Headers();
if (!req.fetchOptions.headers.has('Accept')) {
req.fetchOptions.headers.append('Accept', 'application/json');
}
return req;
},
getConfig(noCache) {
if (!noCache) {
return this._fetchSharedCacheURL({
@@ -1126,7 +1135,8 @@
return;
}
this._credentialCheck.checking = true;
const req = {url: '/accounts/self/detail', reportUrlAsIs: true};
let req = {url: '/accounts/self/detail', reportUrlAsIs: true};
req = this._addAcceptJsonHeader(req);
// Skip the REST response cache.
return this._fetchRawJSON(req).then(res => {
if (!res) { return; }
@@ -1399,7 +1409,7 @@
return this.getChangeActionURL(changeNum, null, '/detail').then(url => {
const urlWithParams = this._urlWithParams(url, optionsHex);
const params = {O: optionsHex};
const req = {
let req = {
url,
errFn: opt_errFn,
cancelCondition: opt_cancelCondition,
@@ -1407,6 +1417,7 @@
fetchOptions: this._etags.getOptions(urlWithParams),
anonymizedUrl: '/changes/*~*/detail?O=' + optionsHex,
};
req = this._addAcceptJsonHeader(req);
return this._fetchRawJSON(req).then(response => {
if (response && response.status === 304) {
return Promise.resolve(this._parsePrefixedJSON(

View File

@@ -575,6 +575,15 @@ limitations under the License.
});
});
test('checkCredentials accepts only json', () => {
const authFetchStub = sandbox.stub(element._auth, 'fetch')
.returns(Promise.resolve());
element.checkCredentials();
assert.isTrue(authFetchStub.called);
assert.equal(authFetchStub.lastCall.args[1].headers.get('Accept'),
'application/json');
});
test('legacy n,z key in change url is replaced', () => {
const stub = sandbox.stub(element, '_fetchJSON')
.returns(Promise.resolve([]));
@@ -1227,6 +1236,16 @@ limitations under the License.
});
});
test('_getChangeDetail accepts only json', () => {
const authFetchStub = sandbox.stub(element._auth, 'fetch')
.returns(Promise.resolve());
const errFn = sinon.stub();
element._getChangeDetail(123, '516714', errFn);
assert.isTrue(authFetchStub.called);
assert.equal(authFetchStub.lastCall.args[1].headers.get('Accept'),
'application/json');
});
test('_getChangeDetail populates _projectLookup', () => {
sandbox.stub(element, 'getChangeActionURL')
.returns(Promise.resolve(''));