From 06b0c9aea332a45de7306c25d86e19cf1f4cd9a3 Mon Sep 17 00:00:00 2001 From: Djordje Date: Tue, 12 Nov 2024 13:58:38 +0100 Subject: [PATCH 01/11] Stop sending api calls after unsuccessful request --- src/script/acquisition-sdk.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/script/acquisition-sdk.ts b/src/script/acquisition-sdk.ts index 1feb5b3f..28ed2af2 100644 --- a/src/script/acquisition-sdk.ts +++ b/src/script/acquisition-sdk.ts @@ -65,7 +65,7 @@ export class AcquisitionManager { private _ignoreAppVersion: boolean; private _serverUrl: string; private _publicPrefixUrl: string = "v0.1/public/codepush/"; - + private _apiAttempts: number = 0; constructor(httpRequester: Http.Requester, configuration: Configuration) { this._httpRequester = httpRequester; @@ -81,6 +81,7 @@ export class AcquisitionManager { } public queryUpdateWithCurrentPackage(currentPackage: Package, callback?: Callback): void { + if(this._apiAttempts>=1) return if (!currentPackage || !currentPackage.appVersion) { throw new CodePushPackageError("Calling common acquisition SDK with incorrect package"); // Unexpected; indicates error in our implementation } @@ -104,6 +105,7 @@ export class AcquisitionManager { if (response.statusCode !== 200) { let errorMessage: any; + this._apiAttempts++ if (response.statusCode === 0) { errorMessage = `Couldn't send request to ${requestUrl}, xhr.statusCode = 0 was returned. One of the possible reasons for that might be connection problems. Please, check your internet connection.`; } else { @@ -147,6 +149,7 @@ export class AcquisitionManager { } public reportStatusDeploy(deployedPackage?: Package, status?: string, previousLabelOrAppVersion?: string, previousDeploymentKey?: string, callback?: Callback): void { + if(this._apiAttempts>=1) return var url: string = this._serverUrl + this._publicPrefixUrl + "report_status/deploy"; var body: DeploymentStatusReport = { app_version: this._appVersion, @@ -197,6 +200,7 @@ export class AcquisitionManager { } if (response.statusCode !== 200) { + this._apiAttempts++ callback(new CodePushHttpError(response.statusCode + ": " + response.body), /*not used*/ null); return; } @@ -207,6 +211,7 @@ export class AcquisitionManager { } public reportStatusDownload(downloadedPackage: Package, callback?: Callback): void { + if(this._apiAttempts>=1) return var url: string = this._serverUrl + this._publicPrefixUrl + "report_status/download"; var body: DownloadReport = { client_unique_id: this._clientUniqueId, @@ -222,10 +227,11 @@ export class AcquisitionManager { } if (response.statusCode !== 200) { + this._apiAttempts++ callback(new CodePushHttpError(response.statusCode + ": " + response.body), /*not used*/ null); return; } - + callback(/*error*/ null, /*not used*/ null); } }); From ac62663ecc42e475d265806e133da8907c3d4f9c Mon Sep 17 00:00:00 2001 From: Djordje Date: Thu, 14 Nov 2024 10:47:27 +0100 Subject: [PATCH 02/11] Update disable api calls logic --- src/script/acquisition-sdk.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/script/acquisition-sdk.ts b/src/script/acquisition-sdk.ts index 28ed2af2..b9fcaed4 100644 --- a/src/script/acquisition-sdk.ts +++ b/src/script/acquisition-sdk.ts @@ -58,6 +58,7 @@ export class AcquisitionStatus { } export class AcquisitionManager { + readonly BASER_URL = "https://codepush.appcenter.ms" private _appVersion: string; private _clientUniqueId: string; private _deploymentKey: string; @@ -65,7 +66,7 @@ export class AcquisitionManager { private _ignoreAppVersion: boolean; private _serverUrl: string; private _publicPrefixUrl: string = "v0.1/public/codepush/"; - private _apiAttempts: number = 0; + private static _apiCallsDisabled: Boolean = false; constructor(httpRequester: Http.Requester, configuration: Configuration) { this._httpRequester = httpRequester; @@ -80,8 +81,14 @@ export class AcquisitionManager { this._ignoreAppVersion = configuration.ignoreAppVersion; } + private disableApiCalls(statusCode: number) { + if (this._serverUrl.includes(this.BASER_URL) && !(statusCode >= 500 || statusCode == 408 || statusCode == 429)) { + AcquisitionManager._apiCallsDisabled = true + } + } + public queryUpdateWithCurrentPackage(currentPackage: Package, callback?: Callback): void { - if(this._apiAttempts>=1) return + if (AcquisitionManager._apiCallsDisabled) return if (!currentPackage || !currentPackage.appVersion) { throw new CodePushPackageError("Calling common acquisition SDK with incorrect package"); // Unexpected; indicates error in our implementation } @@ -105,7 +112,7 @@ export class AcquisitionManager { if (response.statusCode !== 200) { let errorMessage: any; - this._apiAttempts++ + this.disableApiCalls(response.statusCode) if (response.statusCode === 0) { errorMessage = `Couldn't send request to ${requestUrl}, xhr.statusCode = 0 was returned. One of the possible reasons for that might be connection problems. Please, check your internet connection.`; } else { @@ -149,7 +156,7 @@ export class AcquisitionManager { } public reportStatusDeploy(deployedPackage?: Package, status?: string, previousLabelOrAppVersion?: string, previousDeploymentKey?: string, callback?: Callback): void { - if(this._apiAttempts>=1) return + if (AcquisitionManager._apiCallsDisabled) return var url: string = this._serverUrl + this._publicPrefixUrl + "report_status/deploy"; var body: DeploymentStatusReport = { app_version: this._appVersion, @@ -200,7 +207,7 @@ export class AcquisitionManager { } if (response.statusCode !== 200) { - this._apiAttempts++ + this.disableApiCalls(response.statusCode) callback(new CodePushHttpError(response.statusCode + ": " + response.body), /*not used*/ null); return; } @@ -211,7 +218,7 @@ export class AcquisitionManager { } public reportStatusDownload(downloadedPackage: Package, callback?: Callback): void { - if(this._apiAttempts>=1) return + if (AcquisitionManager._apiCallsDisabled) return var url: string = this._serverUrl + this._publicPrefixUrl + "report_status/download"; var body: DownloadReport = { client_unique_id: this._clientUniqueId, @@ -227,7 +234,7 @@ export class AcquisitionManager { } if (response.statusCode !== 200) { - this._apiAttempts++ + this.disableApiCalls(response.statusCode) callback(new CodePushHttpError(response.statusCode + ": " + response.body), /*not used*/ null); return; } From 8edb766c48c900de81f68c006952dfdf4a846848 Mon Sep 17 00:00:00 2001 From: Djordje Date: Thu, 14 Nov 2024 10:49:18 +0100 Subject: [PATCH 03/11] Update access modifer --- src/script/acquisition-sdk.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/script/acquisition-sdk.ts b/src/script/acquisition-sdk.ts index b9fcaed4..dfeb2e5b 100644 --- a/src/script/acquisition-sdk.ts +++ b/src/script/acquisition-sdk.ts @@ -58,7 +58,7 @@ export class AcquisitionStatus { } export class AcquisitionManager { - readonly BASER_URL = "https://codepush.appcenter.ms" + private readonly BASER_URL = "https://codepush.appcenter.ms" private _appVersion: string; private _clientUniqueId: string; private _deploymentKey: string; From deffe547d0ec87a92b5f0a9e348ea43769260117 Mon Sep 17 00:00:00 2001 From: Djordje Date: Thu, 14 Nov 2024 11:32:12 +0100 Subject: [PATCH 04/11] Add test case for disabling api calls --- src/script/acquisition-sdk.ts | 5 ++++- src/test/acquisition-sdk.ts | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/script/acquisition-sdk.ts b/src/script/acquisition-sdk.ts index dfeb2e5b..56bbc410 100644 --- a/src/script/acquisition-sdk.ts +++ b/src/script/acquisition-sdk.ts @@ -66,7 +66,7 @@ export class AcquisitionManager { private _ignoreAppVersion: boolean; private _serverUrl: string; private _publicPrefixUrl: string = "v0.1/public/codepush/"; - private static _apiCallsDisabled: Boolean = false; + private static _apiCallsDisabled: boolean = false; constructor(httpRequester: Http.Requester, configuration: Configuration) { this._httpRequester = httpRequester; @@ -81,6 +81,9 @@ export class AcquisitionManager { this._ignoreAppVersion = configuration.ignoreAppVersion; } + public static get apiCallsDisabled(): boolean { + return this._apiCallsDisabled; + } private disableApiCalls(statusCode: number) { if (this._serverUrl.includes(this.BASER_URL) && !(statusCode >= 500 || statusCode == 408 || statusCode == 429)) { AcquisitionManager._apiCallsDisabled = true diff --git a/src/test/acquisition-sdk.ts b/src/test/acquisition-sdk.ts index 2bafcd48..288b7a73 100644 --- a/src/test/acquisition-sdk.ts +++ b/src/test/acquisition-sdk.ts @@ -226,8 +226,23 @@ describe("Acquisition SDK", () => { done(); })); }); + + it("disables api calls on unsuccessful response", (done: Mocha.Done): void => { + var invalidJsonResponse: acquisitionSdk.Http.Response = { + statusCode: 404, + body: "Not found" + }; + configuration={...configuration, serverUrl:"https://codepush.appcenter.ms"} + var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.CustomResponseHttpRequester(invalidJsonResponse), configuration); + acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { + assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); + done(); + }); + }) }); + + function clone(initialObject: T): T { return JSON.parse(JSON.stringify(initialObject)); } From c3e1c7837635daf4f0c23332c799ee2123efec8b Mon Sep 17 00:00:00 2001 From: Djordje Date: Thu, 14 Nov 2024 11:33:05 +0100 Subject: [PATCH 05/11] Reformat code --- src/test/acquisition-sdk.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/acquisition-sdk.ts b/src/test/acquisition-sdk.ts index 288b7a73..614c18ce 100644 --- a/src/test/acquisition-sdk.ts +++ b/src/test/acquisition-sdk.ts @@ -232,7 +232,7 @@ describe("Acquisition SDK", () => { statusCode: 404, body: "Not found" }; - configuration={...configuration, serverUrl:"https://codepush.appcenter.ms"} + configuration = { ...configuration, serverUrl: "https://codepush.appcenter.ms" } var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.CustomResponseHttpRequester(invalidJsonResponse), configuration); acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); From 9baedaefeb338f7cf8820fdee142bbfda2889695 Mon Sep 17 00:00:00 2001 From: Djordje Date: Tue, 19 Nov 2024 12:36:28 +0100 Subject: [PATCH 06/11] Refactor and reformat code --- src/script/acquisition-sdk.ts | 38 +++++++++++++++++++++++------------ src/test/acquisition-sdk.ts | 19 +++++++++++++++++- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/script/acquisition-sdk.ts b/src/script/acquisition-sdk.ts index 56bbc410..99724b4a 100644 --- a/src/script/acquisition-sdk.ts +++ b/src/script/acquisition-sdk.ts @@ -58,7 +58,7 @@ export class AcquisitionStatus { } export class AcquisitionManager { - private readonly BASER_URL = "https://codepush.appcenter.ms" + private readonly BASER_URL_PART = "appcenter.ms" private _appVersion: string; private _clientUniqueId: string; private _deploymentKey: string; @@ -66,7 +66,7 @@ export class AcquisitionManager { private _ignoreAppVersion: boolean; private _serverUrl: string; private _publicPrefixUrl: string = "v0.1/public/codepush/"; - private static _apiCallsDisabled: boolean = false; + private static _apiCallsDisabled: boolean = false; constructor(httpRequester: Http.Requester, configuration: Configuration) { this._httpRequester = httpRequester; @@ -80,18 +80,23 @@ export class AcquisitionManager { this._deploymentKey = configuration.deploymentKey; this._ignoreAppVersion = configuration.ignoreAppVersion; } - + // Used for Tests public static get apiCallsDisabled(): boolean { return this._apiCallsDisabled; } - private disableApiCalls(statusCode: number) { - if (this._serverUrl.includes(this.BASER_URL) && !(statusCode >= 500 || statusCode == 408 || statusCode == 429)) { - AcquisitionManager._apiCallsDisabled = true + + private handleRequestFailure(statusCode: number) { + if (this._serverUrl.includes(this.BASER_URL_PART) && !(statusCode >= 500 || statusCode == 408 || statusCode == 429)) { + AcquisitionManager._apiCallsDisabled = true; } } public queryUpdateWithCurrentPackage(currentPackage: Package, callback?: Callback): void { - if (AcquisitionManager._apiCallsDisabled) return + if (AcquisitionManager._apiCallsDisabled) { + console.log(`[CodePush] Api calls are disabled, skipping queryUpdateWithCurrentPackage`); + return; + } + if (!currentPackage || !currentPackage.appVersion) { throw new CodePushPackageError("Calling common acquisition SDK with incorrect package"); // Unexpected; indicates error in our implementation } @@ -115,7 +120,7 @@ export class AcquisitionManager { if (response.statusCode !== 200) { let errorMessage: any; - this.disableApiCalls(response.statusCode) + this.handleRequestFailure(response.statusCode) if (response.statusCode === 0) { errorMessage = `Couldn't send request to ${requestUrl}, xhr.statusCode = 0 was returned. One of the possible reasons for that might be connection problems. Please, check your internet connection.`; } else { @@ -159,7 +164,11 @@ export class AcquisitionManager { } public reportStatusDeploy(deployedPackage?: Package, status?: string, previousLabelOrAppVersion?: string, previousDeploymentKey?: string, callback?: Callback): void { - if (AcquisitionManager._apiCallsDisabled) return + if (AcquisitionManager._apiCallsDisabled) { + console.log(`[CodePush] Api calls are disabled, skipping queryUpdateWithCurrentPackage`); + return; + } + var url: string = this._serverUrl + this._publicPrefixUrl + "report_status/deploy"; var body: DeploymentStatusReport = { app_version: this._appVersion, @@ -210,7 +219,7 @@ export class AcquisitionManager { } if (response.statusCode !== 200) { - this.disableApiCalls(response.statusCode) + this.handleRequestFailure(response.statusCode) callback(new CodePushHttpError(response.statusCode + ": " + response.body), /*not used*/ null); return; } @@ -221,7 +230,11 @@ export class AcquisitionManager { } public reportStatusDownload(downloadedPackage: Package, callback?: Callback): void { - if (AcquisitionManager._apiCallsDisabled) return + if (AcquisitionManager._apiCallsDisabled) { + console.log(`[CodePush] Api calls are disabled, skipping queryUpdateWithCurrentPackage`); + return; + } + var url: string = this._serverUrl + this._publicPrefixUrl + "report_status/download"; var body: DownloadReport = { client_unique_id: this._clientUniqueId, @@ -237,11 +250,10 @@ export class AcquisitionManager { } if (response.statusCode !== 200) { - this.disableApiCalls(response.statusCode) + this.handleRequestFailure(response.statusCode) callback(new CodePushHttpError(response.statusCode + ": " + response.body), /*not used*/ null); return; } - callback(/*error*/ null, /*not used*/ null); } }); diff --git a/src/test/acquisition-sdk.ts b/src/test/acquisition-sdk.ts index 614c18ce..6e16e0fa 100644 --- a/src/test/acquisition-sdk.ts +++ b/src/test/acquisition-sdk.ts @@ -234,14 +234,31 @@ describe("Acquisition SDK", () => { }; configuration = { ...configuration, serverUrl: "https://codepush.appcenter.ms" } var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.CustomResponseHttpRequester(invalidJsonResponse), configuration); + acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); done(); }); + + acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { + assert.strictEqual(returnPackage, undefined); + done(); + }) }) -}); + it("doesn't disable api calls on successful response", (done: Mocha.Done): void => { + var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.HttpRequester(), configuration); + + acquisition.reportStatusDeploy(templateCurrentPackage, acquisitionSdk.AcquisitionStatus.DeploymentSucceeded, "1.5.0", mockApi.validDeploymentKey, ((error: Error, parameter: void): void => { + assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); + })) + acquisition.reportStatusDownload(templateCurrentPackage, ((error: Error, parameter: void): void => { + assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); + done(); + })); + }) +}); function clone(initialObject: T): T { return JSON.parse(JSON.stringify(initialObject)); From 315ba253101e6bd71048167661b20ca49374cb28 Mon Sep 17 00:00:00 2001 From: Djordje Date: Tue, 19 Nov 2024 13:14:34 +0100 Subject: [PATCH 07/11] Improve tests --- src/script/acquisition-sdk.ts | 5 +++-- src/test/acquisition-sdk.ts | 30 +++++++++++++++--------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/script/acquisition-sdk.ts b/src/script/acquisition-sdk.ts index 99724b4a..162948bd 100644 --- a/src/script/acquisition-sdk.ts +++ b/src/script/acquisition-sdk.ts @@ -94,6 +94,7 @@ export class AcquisitionManager { public queryUpdateWithCurrentPackage(currentPackage: Package, callback?: Callback): void { if (AcquisitionManager._apiCallsDisabled) { console.log(`[CodePush] Api calls are disabled, skipping queryUpdateWithCurrentPackage`); + callback(/*error=*/ null, /*remotePackage=*/ null); return; } @@ -165,7 +166,7 @@ export class AcquisitionManager { public reportStatusDeploy(deployedPackage?: Package, status?: string, previousLabelOrAppVersion?: string, previousDeploymentKey?: string, callback?: Callback): void { if (AcquisitionManager._apiCallsDisabled) { - console.log(`[CodePush] Api calls are disabled, skipping queryUpdateWithCurrentPackage`); + console.log(`[CodePush] Api calls are disabled, skipping reportStatusDeploy`); return; } @@ -231,7 +232,7 @@ export class AcquisitionManager { public reportStatusDownload(downloadedPackage: Package, callback?: Callback): void { if (AcquisitionManager._apiCallsDisabled) { - console.log(`[CodePush] Api calls are disabled, skipping queryUpdateWithCurrentPackage`); + console.log(`[CodePush] Api calls are disabled, skipping reportStatusDownload`); return; } diff --git a/src/test/acquisition-sdk.ts b/src/test/acquisition-sdk.ts index 6e16e0fa..2c0f18d5 100644 --- a/src/test/acquisition-sdk.ts +++ b/src/test/acquisition-sdk.ts @@ -227,6 +227,19 @@ describe("Acquisition SDK", () => { })); }); + it("doesnt disable api calls on successful response", (done: Mocha.Done): void => { + var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.HttpRequester(), configuration); + + acquisition.reportStatusDeploy(templateCurrentPackage, acquisitionSdk.AcquisitionStatus.DeploymentSucceeded, "1.5.0", mockApi.validDeploymentKey, ((error: Error, parameter: void): void => { + assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); + })) + + acquisition.reportStatusDownload(templateCurrentPackage, ((error: Error, parameter: void): void => { + assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); + })); + done(); + }) + it("disables api calls on unsuccessful response", (done: Mocha.Done): void => { var invalidJsonResponse: acquisitionSdk.Http.Response = { statusCode: 404, @@ -237,27 +250,14 @@ describe("Acquisition SDK", () => { acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); - done(); }); acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { - assert.strictEqual(returnPackage, undefined); - done(); + assert.strictEqual(returnPackage, null); }) + done(); }) - it("doesn't disable api calls on successful response", (done: Mocha.Done): void => { - var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.HttpRequester(), configuration); - - acquisition.reportStatusDeploy(templateCurrentPackage, acquisitionSdk.AcquisitionStatus.DeploymentSucceeded, "1.5.0", mockApi.validDeploymentKey, ((error: Error, parameter: void): void => { - assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); - })) - - acquisition.reportStatusDownload(templateCurrentPackage, ((error: Error, parameter: void): void => { - assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); - done(); - })); - }) }); function clone(initialObject: T): T { From b9a7352cf0532dc7c4f45dd32adbbc1649200c7e Mon Sep 17 00:00:00 2001 From: Djordje Date: Wed, 20 Nov 2024 12:39:52 +0100 Subject: [PATCH 08/11] Update unit tests --- src/script/acquisition-sdk.ts | 13 +++++++++---- src/test/acquisition-sdk.ts | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/script/acquisition-sdk.ts b/src/script/acquisition-sdk.ts index 162948bd..79ff8fbd 100644 --- a/src/script/acquisition-sdk.ts +++ b/src/script/acquisition-sdk.ts @@ -58,7 +58,7 @@ export class AcquisitionStatus { } export class AcquisitionManager { - private readonly BASER_URL_PART = "appcenter.ms" + private readonly BASER_URL_PART = "appcenter.ms"; private _appVersion: string; private _clientUniqueId: string; private _deploymentKey: string; @@ -66,6 +66,7 @@ export class AcquisitionManager { private _ignoreAppVersion: boolean; private _serverUrl: string; private _publicPrefixUrl: string = "v0.1/public/codepush/"; + private static _apiCallsDisabled: boolean = false; constructor(httpRequester: Http.Requester, configuration: Configuration) { this._httpRequester = httpRequester; @@ -80,6 +81,7 @@ export class AcquisitionManager { this._deploymentKey = configuration.deploymentKey; this._ignoreAppVersion = configuration.ignoreAppVersion; } + // Used for Tests public static get apiCallsDisabled(): boolean { return this._apiCallsDisabled; @@ -93,7 +95,7 @@ export class AcquisitionManager { public queryUpdateWithCurrentPackage(currentPackage: Package, callback?: Callback): void { if (AcquisitionManager._apiCallsDisabled) { - console.log(`[CodePush] Api calls are disabled, skipping queryUpdateWithCurrentPackage`); + console.log(`[CodePush] Api calls are disabled, skipping API call`); callback(/*error=*/ null, /*remotePackage=*/ null); return; } @@ -166,7 +168,8 @@ export class AcquisitionManager { public reportStatusDeploy(deployedPackage?: Package, status?: string, previousLabelOrAppVersion?: string, previousDeploymentKey?: string, callback?: Callback): void { if (AcquisitionManager._apiCallsDisabled) { - console.log(`[CodePush] Api calls are disabled, skipping reportStatusDeploy`); + console.log(`[CodePush] Api calls are disabled, skipping API call`); + callback(null, null) return; } @@ -232,7 +235,8 @@ export class AcquisitionManager { public reportStatusDownload(downloadedPackage: Package, callback?: Callback): void { if (AcquisitionManager._apiCallsDisabled) { - console.log(`[CodePush] Api calls are disabled, skipping reportStatusDownload`); + console.log(`[CodePush] Api calls are disabled, skipping API call`); + callback(null, null) return; } @@ -255,6 +259,7 @@ export class AcquisitionManager { callback(new CodePushHttpError(response.statusCode + ": " + response.body), /*not used*/ null); return; } + callback(/*error*/ null, /*not used*/ null); } }); diff --git a/src/test/acquisition-sdk.ts b/src/test/acquisition-sdk.ts index 2c0f18d5..8e73e652 100644 --- a/src/test/acquisition-sdk.ts +++ b/src/test/acquisition-sdk.ts @@ -229,7 +229,15 @@ describe("Acquisition SDK", () => { it("doesnt disable api calls on successful response", (done: Mocha.Done): void => { var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.HttpRequester(), configuration); - + + acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { + assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); + }); + + acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { + assert.notStrictEqual(returnPackage, null) + }); + acquisition.reportStatusDeploy(templateCurrentPackage, acquisitionSdk.AcquisitionStatus.DeploymentSucceeded, "1.5.0", mockApi.validDeploymentKey, ((error: Error, parameter: void): void => { assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); })) @@ -252,6 +260,13 @@ describe("Acquisition SDK", () => { assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); }); + acquisition.reportStatusDeploy(templateCurrentPackage, acquisitionSdk.AcquisitionStatus.DeploymentSucceeded, "1.5.0", mockApi.validDeploymentKey, ((error: Error, parameter: void): void => { + assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); + })) + acquisition.reportStatusDownload(templateCurrentPackage, ((error: Error, parameter: void): void => { + assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); + })); + acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { assert.strictEqual(returnPackage, null); }) From b89d0360d4c537370b85a0b50bc6db37ee743914 Mon Sep 17 00:00:00 2001 From: Djordje Date: Mon, 25 Nov 2024 15:02:26 +0100 Subject: [PATCH 09/11] Update test cases --- src/script/acquisition-sdk.ts | 6 ++-- src/test/acquisition-rest-mock.ts | 26 ++++++++++---- src/test/acquisition-sdk.ts | 59 ++++++++++++++++++++----------- 3 files changed, 60 insertions(+), 31 deletions(-) diff --git a/src/script/acquisition-sdk.ts b/src/script/acquisition-sdk.ts index 79ff8fbd..cda40123 100644 --- a/src/script/acquisition-sdk.ts +++ b/src/script/acquisition-sdk.ts @@ -84,7 +84,7 @@ export class AcquisitionManager { // Used for Tests public static get apiCallsDisabled(): boolean { - return this._apiCallsDisabled; + return AcquisitionManager._apiCallsDisabled; } private handleRequestFailure(statusCode: number) { @@ -169,7 +169,7 @@ export class AcquisitionManager { public reportStatusDeploy(deployedPackage?: Package, status?: string, previousLabelOrAppVersion?: string, previousDeploymentKey?: string, callback?: Callback): void { if (AcquisitionManager._apiCallsDisabled) { console.log(`[CodePush] Api calls are disabled, skipping API call`); - callback(null, null) + callback(/*error*/ null, /*not used*/ null); return; } @@ -236,7 +236,7 @@ export class AcquisitionManager { public reportStatusDownload(downloadedPackage: Package, callback?: Callback): void { if (AcquisitionManager._apiCallsDisabled) { console.log(`[CodePush] Api calls are disabled, skipping API call`); - callback(null, null) + callback(/*error*/ null, /*not used*/ null); return; } diff --git a/src/test/acquisition-rest-mock.ts b/src/test/acquisition-rest-mock.ts index 5b179a91..b162e003 100644 --- a/src/test/acquisition-rest-mock.ts +++ b/src/test/acquisition-rest-mock.ts @@ -22,7 +22,19 @@ var reportStatusDeployUrl = serverUrl + publicPrefixUrl + "/report_status/deploy var reportStatusDownloadUrl = serverUrl + publicPrefixUrl + "/report_status/download"; var updateCheckUrl = serverUrl + publicPrefixUrl + "/update_check?"; +export function updateMockUrl() { + reportStatusDeployUrl = serverUrl + publicPrefixUrl + "/report_status/deploy"; + reportStatusDownloadUrl = serverUrl + publicPrefixUrl + "/report_status/download"; + updateCheckUrl = serverUrl + publicPrefixUrl + "/update_check?"; +} + + export class HttpRequester implements acquisitionSdk.Http.Requester { + private expectedStatusCode: number; + + constructor(expectedStatusCode?: number) { + this.expectedStatusCode = expectedStatusCode; + } public request(verb: acquisitionSdk.Http.Verb, url: string, requestBodyOrCallback: string | acquisitionSdk.Callback, callback?: acquisitionSdk.Callback): void { if (!callback && typeof requestBodyOrCallback === "function") { callback = >requestBodyOrCallback; @@ -30,11 +42,11 @@ export class HttpRequester implements acquisitionSdk.Http.Requester { if (verb === acquisitionSdk.Http.Verb.GET && url.indexOf(updateCheckUrl) === 0) { var params = querystring.parse(url.substring(updateCheckUrl.length)); - Server.onUpdateCheck(params, callback); + Server.onUpdateCheck(params, callback,this.expectedStatusCode); } else if (verb === acquisitionSdk.Http.Verb.POST && url === reportStatusDeployUrl) { - Server.onReportStatus(callback); + Server.onReportStatus(callback,this.expectedStatusCode); } else if (verb === acquisitionSdk.Http.Verb.POST && url === reportStatusDownloadUrl) { - Server.onReportStatus(callback); + Server.onReportStatus(callback,this.expectedStatusCode); } else { throw new Error("Unexpected call"); } @@ -73,7 +85,7 @@ class Server { } } - public static onUpdateCheck(params: any, callback: acquisitionSdk.Callback): void { + public static onUpdateCheck(params: any, callback: acquisitionSdk.Callback, expectedStatusCode?: number): void { var updateRequest: types.UpdateCheckRequest = { deployment_key: params.deployment_key, app_version: params.app_version, @@ -97,13 +109,13 @@ class Server { } callback(/*error=*/ null, { - statusCode: 200, + statusCode: expectedStatusCode ? expectedStatusCode : 200, body: JSON.stringify({ update_info: updateInfo }) }); } } - public static onReportStatus(callback: acquisitionSdk.Callback): void { - callback(/*error*/ null, /*response*/ { statusCode: 200 }); + public static onReportStatus(callback: acquisitionSdk.Callback, expectedStatusCode: number): void { + callback(/*error*/ null, /*response*/ { statusCode: expectedStatusCode ? expectedStatusCode : 200 }); } } diff --git a/src/test/acquisition-sdk.ts b/src/test/acquisition-sdk.ts index 8e73e652..929e407d 100644 --- a/src/test/acquisition-sdk.ts +++ b/src/test/acquisition-sdk.ts @@ -4,6 +4,7 @@ import * as acquisitionSdk from "../script/acquisition-sdk"; import * as acquisitionRestMock from "./acquisition-rest-mock"; import * as types from "../script/types"; import { CodePushPackageError } from "../script/code-push-error" +import { updateMockUrl } from "./acquisition-rest-mock"; const mockApi = acquisitionRestMock; var latestPackage: types.UpdateCheckResponse = clone(mockApi.latestPackage); @@ -44,6 +45,8 @@ var nativeUpdateResult: acquisitionSdk.NativeUpdateNotification = { describe("Acquisition SDK", () => { beforeEach(() => { mockApi.latestPackage = clone(latestPackage); + mockApi.serverUrl = "http://myurl.com"; + updateMockUrl(); }); it("Package with lower label and different package hash gives update", (done: Mocha.Done) => { @@ -226,50 +229,64 @@ describe("Acquisition SDK", () => { done(); })); }); + it("disables api calls on unsuccessful response", (done: Mocha.Done): void => { + var invalidJsonResponse: acquisitionSdk.Http.Response = { + statusCode: 404, + body: "Not found" + }; - it("doesnt disable api calls on successful response", (done: Mocha.Done): void => { - var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.HttpRequester(), configuration); + mockApi.serverUrl = "https://codepush.appcenter.ms"; + updateMockUrl(); + configuration = { ...configuration, serverUrl: "https://codepush.appcenter.ms" }; + var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.CustomResponseHttpRequester(invalidJsonResponse), configuration); acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { - assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); + assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); + (acquisitionSdk.AcquisitionManager as any)._apiCallsDisabled = false; }); acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { - assert.notStrictEqual(returnPackage, null) + assert.strictEqual(returnPackage, null); + acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.HttpRequester(404), configuration); + (acquisitionSdk.AcquisitionManager as any)._apiCallsDisabled = false; + }); acquisition.reportStatusDeploy(templateCurrentPackage, acquisitionSdk.AcquisitionStatus.DeploymentSucceeded, "1.5.0", mockApi.validDeploymentKey, ((error: Error, parameter: void): void => { - assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); - })) + assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); + acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.HttpRequester(404), configuration); + (acquisitionSdk.AcquisitionManager as any)._apiCallsDisabled = false; + })); acquisition.reportStatusDownload(templateCurrentPackage, ((error: Error, parameter: void): void => { - assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); + assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); + acquisition = acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.CustomResponseHttpRequester(invalidJsonResponse), configuration); + (acquisitionSdk.AcquisitionManager as any)._apiCallsDisabled = false; })); + done(); }) + it("doesn't disable api calls on successful response", (done: Mocha.Done): void => { + var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.HttpRequester(), configuration); + mockApi.serverUrl = "https://codepush.appcenter.ms"; + updateMockUrl(); - it("disables api calls on unsuccessful response", (done: Mocha.Done): void => { - var invalidJsonResponse: acquisitionSdk.Http.Response = { - statusCode: 404, - body: "Not found" - }; - configuration = { ...configuration, serverUrl: "https://codepush.appcenter.ms" } - var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.CustomResponseHttpRequester(invalidJsonResponse), configuration); + acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { + assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); + }); acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { - assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); + assert.notStrictEqual(returnPackage, null); }); acquisition.reportStatusDeploy(templateCurrentPackage, acquisitionSdk.AcquisitionStatus.DeploymentSucceeded, "1.5.0", mockApi.validDeploymentKey, ((error: Error, parameter: void): void => { - assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); - })) + assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); + })); + acquisition.reportStatusDownload(templateCurrentPackage, ((error: Error, parameter: void): void => { - assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); + assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); })); - acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { - assert.strictEqual(returnPackage, null); - }) done(); }) From d0cfdd29044813ff67342d3d884065d397c7e0be Mon Sep 17 00:00:00 2001 From: Djordje Date: Tue, 26 Nov 2024 11:00:13 +0100 Subject: [PATCH 10/11] Remove static getter --- src/script/acquisition-sdk.ts | 4 ---- src/test/acquisition-sdk.ts | 12 ++++++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/script/acquisition-sdk.ts b/src/script/acquisition-sdk.ts index cda40123..19ef62d0 100644 --- a/src/script/acquisition-sdk.ts +++ b/src/script/acquisition-sdk.ts @@ -82,10 +82,6 @@ export class AcquisitionManager { this._ignoreAppVersion = configuration.ignoreAppVersion; } - // Used for Tests - public static get apiCallsDisabled(): boolean { - return AcquisitionManager._apiCallsDisabled; - } private handleRequestFailure(statusCode: number) { if (this._serverUrl.includes(this.BASER_URL_PART) && !(statusCode >= 500 || statusCode == 408 || statusCode == 429)) { diff --git a/src/test/acquisition-sdk.ts b/src/test/acquisition-sdk.ts index 929e407d..fab6e52a 100644 --- a/src/test/acquisition-sdk.ts +++ b/src/test/acquisition-sdk.ts @@ -241,7 +241,7 @@ describe("Acquisition SDK", () => { var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.CustomResponseHttpRequester(invalidJsonResponse), configuration); acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { - assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); + assert.strictEqual((acquisitionSdk.AcquisitionManager as any)._apiCallsDisabled, true); (acquisitionSdk.AcquisitionManager as any)._apiCallsDisabled = false; }); @@ -253,13 +253,13 @@ describe("Acquisition SDK", () => { }); acquisition.reportStatusDeploy(templateCurrentPackage, acquisitionSdk.AcquisitionStatus.DeploymentSucceeded, "1.5.0", mockApi.validDeploymentKey, ((error: Error, parameter: void): void => { - assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); + assert.strictEqual((acquisitionSdk.AcquisitionManager as any)._apiCallsDisabled, true); acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.HttpRequester(404), configuration); (acquisitionSdk.AcquisitionManager as any)._apiCallsDisabled = false; })); acquisition.reportStatusDownload(templateCurrentPackage, ((error: Error, parameter: void): void => { - assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, true); + assert.strictEqual((acquisitionSdk.AcquisitionManager as any)._apiCallsDisabled, true); acquisition = acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.CustomResponseHttpRequester(invalidJsonResponse), configuration); (acquisitionSdk.AcquisitionManager as any)._apiCallsDisabled = false; })); @@ -272,7 +272,7 @@ describe("Acquisition SDK", () => { updateMockUrl(); acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { - assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); + assert.strictEqual((acquisitionSdk.AcquisitionManager as any)._apiCallsDisabled, false); }); acquisition.queryUpdateWithCurrentPackage(templateCurrentPackage, (error: Error, returnPackage: acquisitionSdk.RemotePackage | acquisitionSdk.NativeUpdateNotification) => { @@ -280,11 +280,11 @@ describe("Acquisition SDK", () => { }); acquisition.reportStatusDeploy(templateCurrentPackage, acquisitionSdk.AcquisitionStatus.DeploymentSucceeded, "1.5.0", mockApi.validDeploymentKey, ((error: Error, parameter: void): void => { - assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); + assert.strictEqual((acquisitionSdk.AcquisitionManager as any)._apiCallsDisabled, false); })); acquisition.reportStatusDownload(templateCurrentPackage, ((error: Error, parameter: void): void => { - assert.strictEqual(acquisitionSdk.AcquisitionManager.apiCallsDisabled, false); + assert.strictEqual((acquisitionSdk.AcquisitionManager as any)._apiCallsDisabled, false); })); done(); From 05fb0d4ad96a40c186b32ffc2082ece25937c123 Mon Sep 17 00:00:00 2001 From: Djordje Date: Mon, 2 Dec 2024 10:21:30 +0100 Subject: [PATCH 11/11] Reformat code --- src/test/acquisition-rest-mock.ts | 8 ++++---- src/test/acquisition-sdk.ts | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/test/acquisition-rest-mock.ts b/src/test/acquisition-rest-mock.ts index b162e003..b3620d83 100644 --- a/src/test/acquisition-rest-mock.ts +++ b/src/test/acquisition-rest-mock.ts @@ -28,13 +28,13 @@ export function updateMockUrl() { updateCheckUrl = serverUrl + publicPrefixUrl + "/update_check?"; } - export class HttpRequester implements acquisitionSdk.Http.Requester { private expectedStatusCode: number; constructor(expectedStatusCode?: number) { this.expectedStatusCode = expectedStatusCode; } + public request(verb: acquisitionSdk.Http.Verb, url: string, requestBodyOrCallback: string | acquisitionSdk.Callback, callback?: acquisitionSdk.Callback): void { if (!callback && typeof requestBodyOrCallback === "function") { callback = >requestBodyOrCallback; @@ -42,11 +42,11 @@ export class HttpRequester implements acquisitionSdk.Http.Requester { if (verb === acquisitionSdk.Http.Verb.GET && url.indexOf(updateCheckUrl) === 0) { var params = querystring.parse(url.substring(updateCheckUrl.length)); - Server.onUpdateCheck(params, callback,this.expectedStatusCode); + Server.onUpdateCheck(params, callback, this.expectedStatusCode); } else if (verb === acquisitionSdk.Http.Verb.POST && url === reportStatusDeployUrl) { - Server.onReportStatus(callback,this.expectedStatusCode); + Server.onReportStatus(callback, this.expectedStatusCode); } else if (verb === acquisitionSdk.Http.Verb.POST && url === reportStatusDownloadUrl) { - Server.onReportStatus(callback,this.expectedStatusCode); + Server.onReportStatus(callback, this.expectedStatusCode); } else { throw new Error("Unexpected call"); } diff --git a/src/test/acquisition-sdk.ts b/src/test/acquisition-sdk.ts index fab6e52a..18aa86f8 100644 --- a/src/test/acquisition-sdk.ts +++ b/src/test/acquisition-sdk.ts @@ -229,6 +229,7 @@ describe("Acquisition SDK", () => { done(); })); }); + it("disables api calls on unsuccessful response", (done: Mocha.Done): void => { var invalidJsonResponse: acquisitionSdk.Http.Response = { statusCode: 404, @@ -249,7 +250,6 @@ describe("Acquisition SDK", () => { assert.strictEqual(returnPackage, null); acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.HttpRequester(404), configuration); (acquisitionSdk.AcquisitionManager as any)._apiCallsDisabled = false; - }); acquisition.reportStatusDeploy(templateCurrentPackage, acquisitionSdk.AcquisitionStatus.DeploymentSucceeded, "1.5.0", mockApi.validDeploymentKey, ((error: Error, parameter: void): void => { @@ -266,6 +266,7 @@ describe("Acquisition SDK", () => { done(); }) + it("doesn't disable api calls on successful response", (done: Mocha.Done): void => { var acquisition = new acquisitionSdk.AcquisitionManager(new mockApi.HttpRequester(), configuration); mockApi.serverUrl = "https://codepush.appcenter.ms";