diff --git a/jest/__mocks__/userLocation.js b/jest/__mocks__/userLocation.js index 199b1c6..d41fe6b 100644 --- a/jest/__mocks__/userLocation.js +++ b/jest/__mocks__/userLocation.js @@ -1,10 +1,18 @@ const UserLocation = jest.fn().mockImplementation(() => { return { + latitude: "42.3581", + longitude: "-71.0647", continent:"NA", - country:"CA", - region:"NS", - city:"HALIFAX", - zipCode:"B3H+B3J+B3K+B3L+B3M+B3N+B3P+B3R+B3S+B3T" + country:"US", + region:"MA", + city:"BOSTON", + zipCode:"02108-02125+02127-02128+02133+02163+02196+02199+02201+02203-02206+02210-02212+02215+02217+02222+02241+02266+02283-02284+02293+02295+02297-02298", + dma: "506", + timezone: "EST", + networkType: "mobile", + bandwidth: "5000", + areaCodes: ["617"], + fips: ["25025", "25017"] }; }); diff --git a/jest/edgeworkers/examples/respond-from-edgeworkers/respondwith/microservice-geolocation/bundle.json b/jest/edgeworkers/examples/respond-from-edgeworkers/respondwith/microservice-geolocation/bundle.json new file mode 100644 index 0000000..de34b1b --- /dev/null +++ b/jest/edgeworkers/examples/respond-from-edgeworkers/respondwith/microservice-geolocation/bundle.json @@ -0,0 +1,4 @@ +{ + "edgeworker-version": "0.1", + "description" : "Reply instantly with a formatted JSON containing location information." +} \ No newline at end of file diff --git a/jest/edgeworkers/examples/respond-from-edgeworkers/respondwith/microservice-geolocation/main.js b/jest/edgeworkers/examples/respond-from-edgeworkers/respondwith/microservice-geolocation/main.js new file mode 100644 index 0000000..50a120a --- /dev/null +++ b/jest/edgeworkers/examples/respond-from-edgeworkers/respondwith/microservice-geolocation/main.js @@ -0,0 +1,29 @@ +/* +(c) Copyright 2019 Akamai Technologies, Inc. Licensed under Apache 2 license. + +Version: 0.1 +Purpose: Respond with JSON formatted geographical location information. +Repo: https://github.com/akamai/edgeworkers-examples/tree/master/microservice-geolocation +*/ + +export function onClientRequest (request) { + var info = {}; + + info.latitude = (request.userLocation.latitude) ? request.userLocation.latitude : 'N/A'; + info.longitude = (request.userLocation.longitude) ? request.userLocation.longitude : 'N/A'; + info.continent = (request.userLocation.continent) ? request.userLocation.continent : 'N/A'; + info.country = (request.userLocation.country) ? request.userLocation.country : 'N/A'; + info.zipCode = (request.userLocation.zipCode) ? request.userLocation.zipCode : 'N/A'; + info.region = (request.userLocation.region) ? request.userLocation.region : 'N/A'; + info.city = (request.userLocation.city) ? request.userLocation.city : 'N/A'; + info.dma = (request.userLocation.dma) ? request.userLocation.dma : 'N/A'; + info.timezone = (request.userLocation.timezone) ? request.userLocation.timezone : 'N/A'; + info.networkType = (request.userLocation.networkType) ? request.userLocation.networkType : 'N/A'; + info.bandwidth = (request.userLocation.bandwidth) ? request.userLocation.bandwidth : 'N/A'; + info.areaCodes = (request.userLocation.areaCodes) ? request.userLocation.areaCodes : 'N/A'; + info.fips = (request.userLocation.fips) ? request.userLocation.fips : 'N/A'; + + info.source = 'Akamai EdgeWorkers'; + + request.respondWith(200, {}, JSON.stringify({ geoInfo: info })); +} \ No newline at end of file diff --git a/jest/package.json b/jest/package.json index d452027..bf42eca 100644 --- a/jest/package.json +++ b/jest/package.json @@ -1,6 +1,6 @@ { "name": "edgeworkers-jest-mocks", - "version": "1.0.19", + "version": "1.0.20", "description": "Akamai EdgeWorkers Jest mocks", "dependencies": { "@babel/core": "^7.17.8", diff --git a/jest/test/examples/cookie-geolocation/main.test.js b/jest/test/examples/cookie-geolocation/main.test.js index 71c2af5..cab37fd 100644 --- a/jest/test/examples/cookie-geolocation/main.test.js +++ b/jest/test/examples/cookie-geolocation/main.test.js @@ -12,6 +12,10 @@ describe('Add a geoloation data to a cookie in the HTTP response.', () => { test("onClientResponse should set location cookie in the response header when salesRegion is empty", () => { let requestMock = new Request(); + requestMock.userLocation.country = 'CA'; + requestMock.userLocation.region = 'NS'; + requestMock.userLocation.city = 'HALIFAX'; + let responseMock = new Response(); mock_SetCookie_toHeader.mockReturnValue("location=CA+NS+HALIFAX; Max-Age=86400; Path=/"); diff --git a/jest/test/examples/microservice-geolocation/main.test.js b/jest/test/examples/microservice-geolocation/main.test.js index a72cdb8..a4f2266 100644 --- a/jest/test/examples/microservice-geolocation/main.test.js +++ b/jest/test/examples/microservice-geolocation/main.test.js @@ -1,4 +1,4 @@ -import {onClientRequest} from "respond-from-edgeworkers/respondwith/microservice-geolocation/main"; +import {onClientRequest} from "examples/respond-from-edgeworkers/respondwith/microservice-geolocation/main"; import Request from "request"; describe('Respond with JSON formatted geographical location information', () => { @@ -9,15 +9,24 @@ describe('Respond with JSON formatted geographical location information', () => test("return geographical location information", () => { let requestMock = new Request(); - requestMock.userLocation.continent = 'EU'; - requestMock.userLocation.country = 'SE'; + requestMock.userLocation.latitude = '42.364948'; + requestMock.userLocation.longitude = '-71.088783'; + requestMock.userLocation.continent = 'NA'; + requestMock.userLocation.country = 'US'; requestMock.userLocation.zipCode = 'N/A'; - requestMock.userLocation.region = 'AB'; - requestMock.userLocation.city = 'STOCKHOLM'; + requestMock.userLocation.region = 'MA'; + requestMock.userLocation.city = 'BOSTON'; + requestMock.userLocation.dma = '506'; + requestMock.userLocation.timezone = 'EST'; + requestMock.userLocation.networkType = 'mobile'; + requestMock.userLocation.bandwidth = '257'; + requestMock.userLocation.areaCodes = ["617"]; + requestMock.userLocation.fips = ["25025", "25017"]; + onClientRequest(requestMock); - expect(requestMock.respondWith).toHaveBeenCalledTimes(1); - expect(requestMock.respondWith).toHaveBeenCalledWith(200, {}, JSON.stringify({ geoInfo: {"continent":"EU","country":"SE","zip":"N/A","region":"AB","city":"STOCKHOLM","source":"Akamai EdgeWorkers"} })); + expect(requestMock.respondWith).toHaveBeenCalledTimes(1); + expect(requestMock.respondWith).toHaveBeenCalledWith(200, {}, JSON.stringify({ geoInfo: {"latitude":"42.364948","longitude":"-71.088783","continent":"NA","country":"US","zipCode":"N/A","region":"MA","city":"BOSTON","dma":"506","timezone":"EST","networkType":"mobile","bandwidth":"257","areaCodes":["617"],"fips":["25025","25017"],"source": "Akamai EdgeWorkers"}})); }); }); diff --git a/jest/test/examples/redirect-geo/main.test.js b/jest/test/examples/redirect-geo/main.test.js index f1e3616..6b3f316 100644 --- a/jest/test/examples/redirect-geo/main.test.js +++ b/jest/test/examples/redirect-geo/main.test.js @@ -3,6 +3,7 @@ import Request from "request"; test("onClientRequest calling respondWith", () => { let requestMock = new Request(); + requestMock.userLocation.country = "CA"; requestMock.host = "www.example.com"; onClientRequest(requestMock); expect(requestMock.respondWith).toHaveBeenCalled(); diff --git a/mocha/__mocks__/userLocation.js b/mocha/__mocks__/userLocation.js index 676d13a..88f3e5e 100644 --- a/mocha/__mocks__/userLocation.js +++ b/mocha/__mocks__/userLocation.js @@ -1,10 +1,19 @@ export default class UserLocation{ // default to dummy data; can be overriden by developers - constructor(continent="NA", country="CA", region="NS", city="HALIFAX", zipCode="B3H+B3J+B3K+B3L+B3M+B3N+B3P+B3R+B3S+B3T") { + constructor(latitude="42.3581", longitude="-71.0647", continent="NA", country="US", region="MA", city="BOSTON", zipCode="02108-02125+02127-02128+02133+02163+02196+02199+02201+02203-02206+02210-02212+02215+02217+02222+02241+02266+02283-02284+02293+02295+02297-02298", + dma="506", timezone="EST", networkType="mobile", bandwidth="5000", areaCodes=["617"], fips=["25025", "25017"]) { + this.latitude = latitude; + this.longitude = longitude; this.continent = continent; this.country = country; this.region = region; this.city = city; this.zipCode = zipCode; + this.dma = dma; + this.timezone = timezone; + this.networkType = networkType; + this.bandwidth = bandwidth; + this.areaCodes = areaCodes; + this.fips = fips; } } \ No newline at end of file diff --git a/mocha/edgeworkers/examples/respond-from-edgeworkers/respondwith/microservice-geolocation/bundle.json b/mocha/edgeworkers/examples/respond-from-edgeworkers/respondwith/microservice-geolocation/bundle.json new file mode 100644 index 0000000..de34b1b --- /dev/null +++ b/mocha/edgeworkers/examples/respond-from-edgeworkers/respondwith/microservice-geolocation/bundle.json @@ -0,0 +1,4 @@ +{ + "edgeworker-version": "0.1", + "description" : "Reply instantly with a formatted JSON containing location information." +} \ No newline at end of file diff --git a/mocha/edgeworkers/examples/respond-from-edgeworkers/respondwith/microservice-geolocation/main.js b/mocha/edgeworkers/examples/respond-from-edgeworkers/respondwith/microservice-geolocation/main.js new file mode 100644 index 0000000..e1257e6 --- /dev/null +++ b/mocha/edgeworkers/examples/respond-from-edgeworkers/respondwith/microservice-geolocation/main.js @@ -0,0 +1,29 @@ +/* +(c) Copyright 2019 Akamai Technologies, Inc. Licensed under Apache 2 license. + +Version: 0.1 +Purpose: Respond with JSON formatted geographical location information. +Repo: https://github.com/akamai/edgeworkers-examples/tree/master/microservice-geolocation +*/ + +export function onClientRequest (request) { + var info = {}; + + info.latitude = (request.userLocation.latitude) ? request.userLocation.latitude : 'N/A'; + info.longitude = (request.userLocation.longitude) ? request.userLocation.longitude : 'N/A'; + info.continent = (request.userLocation.continent) ? request.userLocation.continent : 'N/A'; + info.country = (request.userLocation.country) ? request.userLocation.country : 'N/A'; + info.zipCode = (request.userLocation.zipCode) ? request.userLocation.zipCode : 'N/A'; + info.region = (request.userLocation.region) ? request.userLocation.region : 'N/A'; + info.city = (request.userLocation.city) ? request.userLocation.city : 'N/A'; + info.dma = (request.userLocation.dma) ? request.userLocation.dma : 'N/A'; + info.timezone = (request.userLocation.timezone) ? request.userLocation.timezone : 'N/A'; + info.networkType = (request.userLocation.networkType) ? request.userLocation.networkType : 'N/A'; + info.bandwidth = (request.userLocation.bandwidth) ? request.userLocation.bandwidth : 'N/A'; + info.areaCodes = (request.userLocation.areaCodes) ? request.userLocation.areaCodes : 'N/A'; + info.fips = (request.userLocation.fips) ? request.userLocation.fips : 'N/A'; + + info.source = 'Akamai EdgeWorkers'; + + request.respondWith(200, {}, JSON.stringify({ geoInfo: info })); +} diff --git a/mocha/package.json b/mocha/package.json index 3312d25..079025e 100644 --- a/mocha/package.json +++ b/mocha/package.json @@ -1,6 +1,6 @@ { "name": "edgeworkers-mocha-mocks", - "version": "1.0.19", + "version": "1.0.20", "description": "Akamai EdgeWorkers Mocha mocks", "dependencies": { "@babel/core": "^7.17.8", diff --git a/mocha/test/examples/cookie-geolocation/main.test.js b/mocha/test/examples/cookie-geolocation/main.test.js index 336ccc5..bda3247 100644 --- a/mocha/test/examples/cookie-geolocation/main.test.js +++ b/mocha/test/examples/cookie-geolocation/main.test.js @@ -14,6 +14,9 @@ describe('Add a geoloation data to a cookie in the HTTP response.', () => { it("onClientResponse should set location cookie in the response header when salesRegion is empty", () => { let requestMock = new Request(); + requestMock.userLocation.country = 'CA'; + requestMock.userLocation.region = 'NS'; + requestMock.userLocation.city = 'HALIFAX'; let responseMock = new Response(); mock_SetCookie_toHeader.returns("location=CA+NS+HALIFAX; Max-Age=86400; Path=/"); diff --git a/mocha/test/examples/microservice-geolocation/main.test.js b/mocha/test/examples/microservice-geolocation/main.test.js index a781fac..f5ea7ee 100644 --- a/mocha/test/examples/microservice-geolocation/main.test.js +++ b/mocha/test/examples/microservice-geolocation/main.test.js @@ -1,4 +1,4 @@ -import {onClientRequest} from "../../../src/edgeworkers/examples/respond-from-edgeworkers/respondwith/microservice-geolocation/main"; +import {onClientRequest} from "../../../edgeworkers/examples/respond-from-edgeworkers/respondwith/microservice-geolocation/main"; import Request from "request"; const sinon = require("sinon"); @@ -12,13 +12,23 @@ describe('Respond with JSON formatted geographical location information', () => it("return geographical location information", () => { let requestMock = new Request(); - requestMock.userLocation.continent = 'EU'; - requestMock.userLocation.country = 'SE'; + requestMock.userLocation.latitude = '42.364948'; + requestMock.userLocation.longitude = '-71.088783'; + requestMock.userLocation.continent = 'NA'; + requestMock.userLocation.country = 'US'; requestMock.userLocation.zipCode = 'N/A'; - requestMock.userLocation.region = 'AB'; - requestMock.userLocation.city = 'STOCKHOLM'; + requestMock.userLocation.region = 'MA'; + requestMock.userLocation.city = 'BOSTON'; + requestMock.userLocation.dma = '506'; + requestMock.userLocation.timezone = 'EST'; + requestMock.userLocation.networkType = 'mobile'; + requestMock.userLocation.bandwidth = '257'; + requestMock.userLocation.areaCodes = ["617"]; + requestMock.userLocation.fips = ["25025", "25017"]; + onClientRequest(requestMock); + expect((requestMock.respondWith).callCount).to.be(1); - expect(requestMock.respondWith.calledWith(200, {}, JSON.stringify({ geoInfo: {"continent":"EU","country":"SE","zip":"N/A","region":"AB","city":"STOCKHOLM","source":"Akamai EdgeWorkers"} }))).to.be(true); + expect(requestMock.respondWith.calledWith(200, {}, JSON.stringify({ geoInfo: {"latitude":"42.364948","longitude":"-71.088783","continent":"NA","country":"US","zipCode":"N/A","region":"MA","city":"BOSTON","dma":"506","timezone":"EST","networkType":"mobile","bandwidth":"257","areaCodes":["617"],"fips":["25025","25017"],"source": "Akamai EdgeWorkers"}}))).to.be(true); }); }); diff --git a/mocha/test/examples/redirect-geo/main.test.js b/mocha/test/examples/redirect-geo/main.test.js index 729c9e8..c9f8a4f 100644 --- a/mocha/test/examples/redirect-geo/main.test.js +++ b/mocha/test/examples/redirect-geo/main.test.js @@ -11,6 +11,7 @@ describe('onClientRequest', () => { it("should call respondWith", () => { let requestMock = new Request(); + requestMock.userLocation.country = "CA"; requestMock.host = "www.example.com"; onClientRequest(requestMock); expect(requestMock.respondWith.called).to.be(true)