|
| 1 | +// Ported from https://github.com/microsoft/code-push/blob/master/src/test/acquisition-rest-mock.ts (archived, MIT licensed) |
| 2 | +// Not currently wired into any test runner — this repo has no unit test infra yet. |
| 3 | + |
| 4 | +import * as querystring from "querystring"; |
| 5 | + |
| 6 | +import * as acquisitionSdk from "../acquisition-sdk"; |
| 7 | +import * as types from "../types"; |
| 8 | + |
| 9 | +export var validDeploymentKey = "Valid Deployment Key"; |
| 10 | +export var latestPackage = <types.UpdateCheckResponse>{ |
| 11 | + download_url: "http://www.windowsazure.com/blobs/awperoiuqpweru", |
| 12 | + description: "Angry flappy birds", |
| 13 | + target_binary_range: "1.5.0", |
| 14 | + label: "2.4.0", |
| 15 | + is_mandatory: false, |
| 16 | + is_available: true, |
| 17 | + update_app_version: false, |
| 18 | + package_hash: "hash240", |
| 19 | + package_size: 1024 |
| 20 | +}; |
| 21 | + |
| 22 | +export var serverUrl = "http://myurl.com"; |
| 23 | +var publicPrefixUrl = "/v0.1/public/codepush"; |
| 24 | +var reportStatusDeployUrl = serverUrl + publicPrefixUrl + "/report_status/deploy"; |
| 25 | +var reportStatusDownloadUrl = serverUrl + publicPrefixUrl + "/report_status/download"; |
| 26 | +var updateCheckUrl = serverUrl + publicPrefixUrl + "/update_check?"; |
| 27 | + |
| 28 | +export function updateMockUrl() { |
| 29 | + reportStatusDeployUrl = serverUrl + publicPrefixUrl + "/report_status/deploy"; |
| 30 | + reportStatusDownloadUrl = serverUrl + publicPrefixUrl + "/report_status/download"; |
| 31 | + updateCheckUrl = serverUrl + publicPrefixUrl + "/update_check?"; |
| 32 | +} |
| 33 | + |
| 34 | +export class HttpRequester implements acquisitionSdk.Http.Requester { |
| 35 | + private expectedStatusCode: number; |
| 36 | + |
| 37 | + constructor(expectedStatusCode?: number) { |
| 38 | + this.expectedStatusCode = expectedStatusCode; |
| 39 | + } |
| 40 | + |
| 41 | + public request(verb: acquisitionSdk.Http.Verb, url: string, requestBodyOrCallback: string | acquisitionSdk.Callback<acquisitionSdk.Http.Response>, callback?: acquisitionSdk.Callback<acquisitionSdk.Http.Response>): void { |
| 42 | + if (!callback && typeof requestBodyOrCallback === "function") { |
| 43 | + callback = <acquisitionSdk.Callback<acquisitionSdk.Http.Response>>requestBodyOrCallback; |
| 44 | + } |
| 45 | + |
| 46 | + if (verb === acquisitionSdk.Http.Verb.GET && url.indexOf(updateCheckUrl) === 0) { |
| 47 | + var params = querystring.parse(url.substring(updateCheckUrl.length)); |
| 48 | + Server.onUpdateCheck(params, callback, this.expectedStatusCode); |
| 49 | + } else if (verb === acquisitionSdk.Http.Verb.POST && url === reportStatusDeployUrl) { |
| 50 | + Server.onReportStatus(callback, this.expectedStatusCode); |
| 51 | + } else if (verb === acquisitionSdk.Http.Verb.POST && url === reportStatusDownloadUrl) { |
| 52 | + Server.onReportStatus(callback, this.expectedStatusCode); |
| 53 | + } else { |
| 54 | + throw new Error("Unexpected call"); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +export class CustomResponseHttpRequester implements acquisitionSdk.Http.Requester { |
| 60 | + response: acquisitionSdk.Http.Response; |
| 61 | + |
| 62 | + constructor(response: acquisitionSdk.Http.Response) { |
| 63 | + this.response = response; |
| 64 | + } |
| 65 | + |
| 66 | + public request(verb: acquisitionSdk.Http.Verb, url: string, requestBodyOrCallback: string | acquisitionSdk.Callback<acquisitionSdk.Http.Response>, callback?: acquisitionSdk.Callback<acquisitionSdk.Http.Response>): void { |
| 67 | + if (typeof requestBodyOrCallback !== "function") { |
| 68 | + throw new Error("Unexpected request body"); |
| 69 | + } |
| 70 | + |
| 71 | + callback = <acquisitionSdk.Callback<acquisitionSdk.Http.Response>>requestBodyOrCallback; |
| 72 | + callback(null, this.response); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +class Server { |
| 77 | + public static onAcquire(params: any, callback: acquisitionSdk.Callback<acquisitionSdk.Http.Response>): void { |
| 78 | + if (params.deploymentKey !== validDeploymentKey) { |
| 79 | + callback(/*error=*/ null, { |
| 80 | + statusCode: 200, |
| 81 | + body: JSON.stringify({ update_info: { isAvailable: false } }) |
| 82 | + }); |
| 83 | + } else { |
| 84 | + callback(/*error=*/ null, { |
| 85 | + statusCode: 200, |
| 86 | + body: JSON.stringify({ update_info: latestPackage }) |
| 87 | + }); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public static onUpdateCheck(params: any, callback: acquisitionSdk.Callback<acquisitionSdk.Http.Response>, expectedStatusCode?: number): void { |
| 92 | + var updateRequest: types.UpdateCheckRequest = { |
| 93 | + deployment_key: params.deployment_key, |
| 94 | + app_version: params.app_version, |
| 95 | + package_hash: params.package_hash, |
| 96 | + is_companion: !!(params.is_companion), |
| 97 | + label: params.label |
| 98 | + }; |
| 99 | + |
| 100 | + if (!updateRequest.deployment_key || !updateRequest.app_version) { |
| 101 | + callback(/*error=*/ null, { statusCode: 400 }); |
| 102 | + } else { |
| 103 | + var updateInfo = <types.UpdateCheckResponse>{ is_available: false }; |
| 104 | + if (updateRequest.deployment_key === validDeploymentKey) { |
| 105 | + if (updateRequest.is_companion || updateRequest.app_version === latestPackage.target_binary_range) { |
| 106 | + if (updateRequest.package_hash !== latestPackage.package_hash) { |
| 107 | + updateInfo = latestPackage; |
| 108 | + } |
| 109 | + } else if (updateRequest.app_version < latestPackage.target_binary_range) { |
| 110 | + updateInfo = <types.UpdateCheckResponse><any>{ update_app_version: true, target_binary_range: latestPackage.target_binary_range }; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + callback(/*error=*/ null, { |
| 115 | + statusCode: expectedStatusCode ? expectedStatusCode : 200, |
| 116 | + body: JSON.stringify({ update_info: updateInfo }) |
| 117 | + }); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public static onReportStatus(callback: acquisitionSdk.Callback<acquisitionSdk.Http.Response>, expectedStatusCode: number): void { |
| 122 | + callback(/*error*/ null, /*response*/ { statusCode: expectedStatusCode ? expectedStatusCode : 200 }); |
| 123 | + } |
| 124 | +} |
0 commit comments