From b89d0360d4c537370b85a0b50bc6db37ee743914 Mon Sep 17 00:00:00 2001 From: Djordje Date: Mon, 25 Nov 2024 15:02:26 +0100 Subject: [PATCH] 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(); })