|
| 1 | +import { Yok } from "../../lib/common/yok"; |
| 2 | +import { LoggerStub } from "../stubs"; |
| 3 | +import { IPService } from "../../lib/services/ip-service"; |
| 4 | +import { assert } from "chai"; |
| 5 | + |
| 6 | +describe("ipService", () => { |
| 7 | + const ip = "8.8.8.8"; |
| 8 | + const whoamiDefaultEndpoint = "https://who.am.i/api/whoami"; |
| 9 | + const errorMsgForDefaultEndpoint = `Unable to get data from ${whoamiDefaultEndpoint}`; |
| 10 | + const errMsgForMyipCom = "Unable to get data from myip.com"; |
| 11 | + const errMsgForIpifyOrg = "Unable to get data from ipify.org"; |
| 12 | + |
| 13 | + const createTestInjector = (): IInjector => { |
| 14 | + const testInjector = new Yok(); |
| 15 | + testInjector.register("httpClient", { |
| 16 | + httpRequest: async (options: any, proxySettings?: IProxySettings): Promise<Server.IResponse> => (<any>{}) |
| 17 | + }); |
| 18 | + |
| 19 | + testInjector.register("logger", LoggerStub); |
| 20 | + testInjector.register("ipService", IPService); |
| 21 | + testInjector.register("config", { |
| 22 | + WHOAMI_URL_ENDPOINT: whoamiDefaultEndpoint |
| 23 | + }); |
| 24 | + return testInjector; |
| 25 | + }; |
| 26 | + |
| 27 | + describe("getCurrentIPv4Address", () => { |
| 28 | + it("returns result from default service (play.nativescript.org) when it succeeds", async () => { |
| 29 | + const testInjector = createTestInjector(); |
| 30 | + const httpClient = testInjector.resolve<Server.IHttpClient>("httpClient"); |
| 31 | + const httpRequestPassedOptions: any[] = []; |
| 32 | + httpClient.httpRequest = async (options: any, proxySettings?: IProxySettings): Promise<Server.IResponse> => { |
| 33 | + httpRequestPassedOptions.push(options); |
| 34 | + return <any>{ body: JSON.stringify({ ip }) }; |
| 35 | + }; |
| 36 | + |
| 37 | + const ipService = testInjector.resolve<IIPService>("ipService"); |
| 38 | + const ipAddress = await ipService.getCurrentIPv4Address(); |
| 39 | + |
| 40 | + assert.equal(ipAddress, ip); |
| 41 | + assert.deepEqual(httpRequestPassedOptions, [{ method: "GET", url: whoamiDefaultEndpoint, timeout: 1000 }]); |
| 42 | + }); |
| 43 | + |
| 44 | + it("returns result from myip.com when the default endpoint fails", async () => { |
| 45 | + const testInjector = createTestInjector(); |
| 46 | + const httpClient = testInjector.resolve<Server.IHttpClient>("httpClient"); |
| 47 | + const httpRequestPassedOptions: any[] = []; |
| 48 | + httpClient.httpRequest = async (options: any, proxySettings?: IProxySettings): Promise<Server.IResponse> => { |
| 49 | + httpRequestPassedOptions.push(options); |
| 50 | + if (options.url === whoamiDefaultEndpoint) { |
| 51 | + throw new Error(errorMsgForDefaultEndpoint); |
| 52 | + } |
| 53 | + return <any>{ body: JSON.stringify({ ip }) }; |
| 54 | + }; |
| 55 | + |
| 56 | + const ipService = testInjector.resolve<IIPService>("ipService"); |
| 57 | + const ipAddress = await ipService.getCurrentIPv4Address(); |
| 58 | + |
| 59 | + assert.equal(ipAddress, ip); |
| 60 | + assert.deepEqual(httpRequestPassedOptions, [ |
| 61 | + { method: "GET", url: whoamiDefaultEndpoint, timeout: 1000 }, |
| 62 | + { method: "GET", url: "https://api.myip.com", timeout: 1000 } |
| 63 | + ]); |
| 64 | + |
| 65 | + const logger = testInjector.resolve<LoggerStub>("logger"); |
| 66 | + assert.isTrue(logger.traceOutput.indexOf(errorMsgForDefaultEndpoint) !== -1, `Trace output\n'${logger.traceOutput}'\ndoes not contain expected message:\n${errorMsgForDefaultEndpoint}`); |
| 67 | + |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns result from ipify.com when it default endpoint and myip.comm both fail", async () => { |
| 71 | + const testInjector = createTestInjector(); |
| 72 | + const httpClient = testInjector.resolve<Server.IHttpClient>("httpClient"); |
| 73 | + const httpRequestPassedOptions: any[] = []; |
| 74 | + httpClient.httpRequest = async (options: any, proxySettings?: IProxySettings): Promise<Server.IResponse> => { |
| 75 | + httpRequestPassedOptions.push(options); |
| 76 | + if (options.url === whoamiDefaultEndpoint) { |
| 77 | + throw new Error(errorMsgForDefaultEndpoint); |
| 78 | + } |
| 79 | + |
| 80 | + if (options.url === "https://api.myip.com") { |
| 81 | + throw new Error(errMsgForMyipCom); |
| 82 | + } |
| 83 | + |
| 84 | + return <any>{ body: ip }; |
| 85 | + }; |
| 86 | + |
| 87 | + const ipService = testInjector.resolve<IIPService>("ipService"); |
| 88 | + const ipAddress = await ipService.getCurrentIPv4Address(); |
| 89 | + |
| 90 | + assert.equal(ipAddress, ip); |
| 91 | + assert.deepEqual(httpRequestPassedOptions, [ |
| 92 | + { method: "GET", url: whoamiDefaultEndpoint, timeout: 1000 }, |
| 93 | + { method: "GET", url: "https://api.myip.com", timeout: 1000 }, |
| 94 | + { method: "GET", url: "https://api.ipify.org", timeout: 1000 } |
| 95 | + ]); |
| 96 | + |
| 97 | + const logger = testInjector.resolve<LoggerStub>("logger"); |
| 98 | + assert.isTrue(logger.traceOutput.indexOf(errorMsgForDefaultEndpoint) !== -1, `Trace output\n'${logger.traceOutput}'\ndoes not contain expected message:\n${errorMsgForDefaultEndpoint}`); |
| 99 | + assert.isTrue(logger.traceOutput.indexOf(errMsgForMyipCom) !== -1, `Trace output\n'${logger.traceOutput}'\ndoes not contain expected message:\n${errMsgForMyipCom}`); |
| 100 | + }); |
| 101 | + |
| 102 | + it("returns null when all endpoints fail", async () => { |
| 103 | + const testInjector = createTestInjector(); |
| 104 | + const httpClient = testInjector.resolve<Server.IHttpClient>("httpClient"); |
| 105 | + const httpRequestPassedOptions: any[] = []; |
| 106 | + httpClient.httpRequest = async (options: any, proxySettings?: IProxySettings): Promise<Server.IResponse> => { |
| 107 | + httpRequestPassedOptions.push(options); |
| 108 | + if (options.url === whoamiDefaultEndpoint) { |
| 109 | + throw new Error(errorMsgForDefaultEndpoint); |
| 110 | + } |
| 111 | + |
| 112 | + if (options.url === "https://api.myip.com") { |
| 113 | + throw new Error(errMsgForMyipCom); |
| 114 | + } |
| 115 | + |
| 116 | + if (options.url === "https://api.ipify.org") { |
| 117 | + throw new Error(errMsgForIpifyOrg); |
| 118 | + } |
| 119 | + |
| 120 | + return <any>{ body: ip }; |
| 121 | + }; |
| 122 | + |
| 123 | + const ipService = testInjector.resolve<IIPService>("ipService"); |
| 124 | + const ipAddress = await ipService.getCurrentIPv4Address(); |
| 125 | + |
| 126 | + assert.isNull(ipAddress); |
| 127 | + assert.deepEqual(httpRequestPassedOptions, [ |
| 128 | + { method: "GET", url: whoamiDefaultEndpoint, timeout: 1000 }, |
| 129 | + { method: "GET", url: "https://api.myip.com", timeout: 1000 }, |
| 130 | + { method: "GET", url: "https://api.ipify.org", timeout: 1000 } |
| 131 | + ]); |
| 132 | + |
| 133 | + const logger = testInjector.resolve<LoggerStub>("logger"); |
| 134 | + assert.isTrue(logger.traceOutput.indexOf(errorMsgForDefaultEndpoint) !== -1, `Trace output\n'${logger.traceOutput}'\ndoes not contain expected message:\n${errorMsgForDefaultEndpoint}`); |
| 135 | + assert.isTrue(logger.traceOutput.indexOf(errMsgForMyipCom) !== -1, `Trace output\n'${logger.traceOutput}'\ndoes not contain expected message:\n${errMsgForMyipCom}`); |
| 136 | + assert.isTrue(logger.traceOutput.indexOf(errMsgForIpifyOrg) !== -1, `Trace output\n'${logger.traceOutput}'\ndoes not contain expected message:\n${errMsgForMyipCom}`); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments