Bug 1333431 - Fixed ERROR_PARSE thrown on 304 response when fetching profile. r=eoger

MozReview-Commit-ID: KJPvZeU10xP

--HG--
extra : rebase_source : faaa2f7bc49d0aefd85e0f0846076290f71998c8
This commit is contained in:
Mayank Srivastav 2017-02-07 23:25:01 +05:30
parent abd6d9a1ce
commit ccf5a4ca8d
4 changed files with 51 additions and 6 deletions

View File

@ -99,16 +99,16 @@ this.FxAccountsProfile.prototype = {
return this.client.fetchProfile(etag);
})
.then(response => {
return this._cacheProfile(response);
// response may be null if the profile was not modified (same ETag).
return response ? this._cacheProfile(response) : null;
})
.then(body => { // finally block
onFinally();
// body may be null if the profile was not modified
return body;
}, err => {
onFinally();
if (err.code != 304) { // fetchProfile() throws when the profile wasn't modified
throw err;
}
throw err;
});
},

View File

@ -127,7 +127,8 @@ this.FxAccountsProfileClient.prototype = {
* @param {String} token
* @param {String} etag
* @return Promise
* Resolves: {body: Object, etag: Object} Successful response from the Profile server.
* Resolves: {body: Object, etag: Object} Successful response from the Profile server
or null if 304 is hit (same ETag).
* Rejects: {FxAccountsProfileClientError} Profile client error.
* @private
*/
@ -154,6 +155,9 @@ this.FxAccountsProfileClient.prototype = {
let body = null;
try {
if (request.response.status == 304) {
return resolve(null);
}
body = JSON.parse(request.response.body);
} catch (e) {
return reject(new FxAccountsProfileClientError({

View File

@ -291,6 +291,29 @@ add_task(function* fetchAndCacheProfileOnce() {
do_check_eq(got.avatar, "myimg");
});
add_test(function fetchAndCacheProfile_alreadyCached() {
let cachedUrl = "cachedurl";
let fxa = mockFxa();
fxa.profileCache = { profile: { avatar: cachedUrl }, etag: "bogusETag" };
let client = mockClient(fxa);
client.fetchProfile = function(etag) {
do_check_eq(etag, "bogusETag");
return Promise.resolve(null);
};
let profile = CreateFxAccountsProfile(fxa, client);
profile._cacheProfile = function(toCache) {
do_throw("This method should not be called.");
};
return profile._fetchAndCacheProfile()
.then(result => {
do_check_eq(result, null);
do_check_eq(fxa.profileCache.profile.avatar, cachedUrl);
run_next_test();
});
});
// Check that a new profile request within PROFILE_FRESHNESS_THRESHOLD of the
// last one doesn't kick off a new request to check the cached copy is fresh.
add_task(function* fetchAndCacheProfileAfterThreshold() {

View File

@ -72,7 +72,7 @@ add_test(function successfulResponse() {
let response = {
success: true,
status: STATUS_SUCCESS,
headers: { etag:"bogusETag" },
headers: { etag: "bogusETag" },
body: "{\"email\":\"someone@restmail.net\",\"uid\":\"0d5c1a89b8c54580b8e3e8adadae864a\"}",
};
@ -112,6 +112,24 @@ add_test(function setsIfNoneMatchETagHeader() {
);
});
add_test(function successful304Response() {
let client = new FxAccountsProfileClient(PROFILE_OPTIONS);
let response = {
success: true,
headers: { etag: "bogusETag" },
status: 304,
};
client._Request = new mockResponse(response);
client.fetchProfile()
.then(
function(result) {
do_check_eq(result, null);
run_next_test();
}
);
});
add_test(function parseErrorResponse() {
let client = new FxAccountsProfileClient(PROFILE_OPTIONS);
let response = {