diff options
Diffstat (limited to 'toolkit/components/shopping/test/mockapis')
7 files changed, 261 insertions, 0 deletions
diff --git a/toolkit/components/shopping/test/mockapis/analysis.sjs b/toolkit/components/shopping/test/mockapis/analysis.sjs new file mode 100644 index 0000000000..2cc154803e --- /dev/null +++ b/toolkit/components/shopping/test/mockapis/analysis.sjs @@ -0,0 +1,47 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +function loadHelperScript(path) { + let scriptFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); + scriptFile.initWithPath(getState("__LOCATION__")); + scriptFile = scriptFile.parent; + scriptFile.append(path); + let scriptSpec = Services.io.newFileURI(scriptFile).spec; + Services.scriptloader.loadSubScript(scriptSpec, this); +} +/* import-globals-from ./server_helper.js */ +loadHelperScript("server_helper.js"); + +let gResponses = new Map( + Object.entries({ + ABCDEFG123: { needs_analysis: false, grade: "B", adjusted_rating: 4.1 }, + HIJKLMN456: { needs_analysis: false, grade: "F", adjusted_rating: 1.0 }, + OPQRSTU789: { needs_analysis: true }, + INVALID123: { needs_analysis: false, grade: 0.85, adjusted_rating: 1.0 }, + HTTPERR503: { status: 503, error: "Service Unavailable" }, + HTTPERR429: { status: 429, error: "Too Many Requests" }, + }) +); + +function handleRequest(request, response) { + var body = getPostBody(request.bodyInputStream); + let requestData = JSON.parse(body); + let productDetails = gResponses.get(requestData.product_id); + if (!productDetails) { + response.setStatusLine(request.httpVersion, 400, "Bad Request"); + productDetails = { + status: 400, + error: "Bad Request", + }; + } + if (productDetails?.status) { + response.setStatusLine( + request.httpVersion, + productDetails.status, + productDetails.error + ); + } + response.write(JSON.stringify(productDetails)); +} diff --git a/toolkit/components/shopping/test/mockapis/analysis_status.sjs b/toolkit/components/shopping/test/mockapis/analysis_status.sjs new file mode 100644 index 0000000000..4d943a9476 --- /dev/null +++ b/toolkit/components/shopping/test/mockapis/analysis_status.sjs @@ -0,0 +1,37 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +function loadHelperScript(path) { + let scriptFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); + scriptFile.initWithPath(getState("__LOCATION__")); + scriptFile = scriptFile.parent; + scriptFile.append(path); + let scriptSpec = Services.io.newFileURI(scriptFile).spec; + Services.scriptloader.loadSubScript(scriptSpec, this); +} +/* import-globals-from ./server_helper.js */ +loadHelperScript("server_helper.js"); + +let gResponses = new Map( + Object.entries({ + N0T3NOUGHR: { status: "not_enough_reviews", progress: 100.0 }, + PAG3N0TSUP: { status: "page_not_supported", progress: 100.0 }, + UNPR0C3SSA: { status: "unprocessable", progress: 100.0 }, + }) +); + +function handleRequest(request, response) { + let body = getPostBody(request.bodyInputStream); + let requestData = JSON.parse(body); + let responseData = gResponses.get(requestData.product_id); + if (!responseData) { + // We want the status to be completed for most tests. + responseData = { + status: "completed", + progress: 100.0, + }; + } + response.write(JSON.stringify(responseData)); +} diff --git a/toolkit/components/shopping/test/mockapis/analyze.sjs b/toolkit/components/shopping/test/mockapis/analyze.sjs new file mode 100644 index 0000000000..6f9a8cbcee --- /dev/null +++ b/toolkit/components/shopping/test/mockapis/analyze.sjs @@ -0,0 +1,23 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +function loadHelperScript(path) { + let scriptFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); + scriptFile.initWithPath(getState("__LOCATION__")); + scriptFile = scriptFile.parent; + scriptFile.append(path); + let scriptSpec = Services.io.newFileURI(scriptFile).spec; + Services.scriptloader.loadSubScript(scriptSpec, this); +} +/* import-globals-from ./server_helper.js */ +loadHelperScript("server_helper.js"); + +function handleRequest(_request, response) { + // We always want the status to be pending for the current tests. + let status = { + status: "pending", + }; + response.write(JSON.stringify(status)); +} diff --git a/toolkit/components/shopping/test/mockapis/attribution.sjs b/toolkit/components/shopping/test/mockapis/attribution.sjs new file mode 100644 index 0000000000..b25cf78b3b --- /dev/null +++ b/toolkit/components/shopping/test/mockapis/attribution.sjs @@ -0,0 +1,41 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +function loadHelperScript(path) { + let scriptFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); + scriptFile.initWithPath(getState("__LOCATION__")); + scriptFile = scriptFile.parent; + scriptFile.append(path); + let scriptSpec = Services.io.newFileURI(scriptFile).spec; + Services.scriptloader.loadSubScript(scriptSpec, this); +} +/* import-globals-from ./server_helper.js */ +loadHelperScript("server_helper.js"); + +function getPostBody(stream) { + let binaryStream = new BinaryInputStream(stream); + let count = binaryStream.available(); + let arrayBuffer = new ArrayBuffer(count); + while (count > 0) { + let actuallyRead = binaryStream.readArrayBuffer(count, arrayBuffer); + if (!actuallyRead) { + throw new Error("Nothing read from input stream!"); + } + count -= actuallyRead; + } + return new TextDecoder().decode(arrayBuffer); +} + +function handleRequest(request, response) { + var body = getPostBody(request.bodyInputStream); + let requestData = JSON.parse(body); + + let key = requestData.aid || (requestData.aidvs && requestData.aidvs[0]); + let responseObj = { + [key]: null, + }; + + response.write(JSON.stringify(responseObj)); +} diff --git a/toolkit/components/shopping/test/mockapis/recommendations.sjs b/toolkit/components/shopping/test/mockapis/recommendations.sjs new file mode 100644 index 0000000000..2c4abc23d2 --- /dev/null +++ b/toolkit/components/shopping/test/mockapis/recommendations.sjs @@ -0,0 +1,45 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +function loadHelperScript(path) { + let scriptFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); + scriptFile.initWithPath(getState("__LOCATION__")); + scriptFile = scriptFile.parent; + scriptFile.append(path); + let scriptSpec = Services.io.newFileURI(scriptFile).spec; + Services.scriptloader.loadSubScript(scriptSpec, this); +} +/* import-globals-from ./server_helper.js */ +loadHelperScript("server_helper.js"); + +let gResponses = new Map( + Object.entries({ + ABCDEFG123: [ + { + name: "VIVO Electric 60 x 24 inch Stand Up Desk | Black Table Top, Black Frame, Height Adjustable Standing Workstation with Memory Preset Controller (DESK-KIT-1B6B)", + url: "https://example.com/Some-Product/dp/ABCDEFG123", + image_url: "https://example.com/api/image.jpg", + price: "249.99", + currency: "USD", + grade: "A", + adjusted_rating: 4.6, + analysis_url: + "https://www.fakespot.com/product/vivo-electric-60-x-24-inch-stand-up-desk-black-table-top-black-frame-height-adjustable-standing-workstation-with-memory-preset-controller-desk-kit-1b6b", + sponsored: true, + aid: "ELcC6OziGKu2jjQsCf5xMYHTpyPKFtjl/4mKPeygbSuQMyIqF/gkY3bTTznoMmNv0OsPV5uql0/NdzNsoguccIS0BujM3DwBADvkGIKLLF2WX0u3G+B2tvRpZmbTmC1NQW0ivS/KX7dTrRjae3Z84fs0i0PySM68buCo5JY848wvzdlyTfCrcT0B3/Ov0tJjZdy9FupF9skLuFtx0/lgElSRsnoGow/H--uLo2Tq7E++RNxgsl--YqlLhv5n8iGFYQwBD61VBg==", + }, + ], + HIJKLMN456: [], + OPQRSTU789: [], + }) +); + +function handleRequest(request, response) { + var body = getPostBody(request.bodyInputStream); + let requestData = JSON.parse(body); + let recommendation = gResponses.get(requestData.product_id); + + response.write(JSON.stringify(recommendation)); +} diff --git a/toolkit/components/shopping/test/mockapis/reporting.sjs b/toolkit/components/shopping/test/mockapis/reporting.sjs new file mode 100644 index 0000000000..1f4a4115e5 --- /dev/null +++ b/toolkit/components/shopping/test/mockapis/reporting.sjs @@ -0,0 +1,40 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const BinaryInputStream = Components.Constructor( + "@mozilla.org/binaryinputstream;1", + "nsIBinaryInputStream", + "setInputStream" +); + +function getPostBody(stream) { + let binaryStream = new BinaryInputStream(stream); + let count = binaryStream.available(); + let arrayBuffer = new ArrayBuffer(count); + while (count > 0) { + let actuallyRead = binaryStream.readArrayBuffer(count, arrayBuffer); + if (!actuallyRead) { + throw new Error("Nothing read from input stream!"); + } + count -= actuallyRead; + } + return new TextDecoder().decode(arrayBuffer); +} + +let gResponses = new Map( + Object.entries({ + ABCDEFG123: { message: "report created" }, + HIJKLMN456: { message: "already reported" }, + OPQRSTU789: { message: "not deleted" }, + }) +); + +function handleRequest(request, response) { + var body = getPostBody(request.bodyInputStream); + let requestData = JSON.parse(body); + let report = gResponses.get(requestData.product_id); + + response.write(JSON.stringify(report)); +} diff --git a/toolkit/components/shopping/test/mockapis/server_helper.js b/toolkit/components/shopping/test/mockapis/server_helper.js new file mode 100644 index 0000000000..e5d3a1d591 --- /dev/null +++ b/toolkit/components/shopping/test/mockapis/server_helper.js @@ -0,0 +1,28 @@ +/* Any copyright is dedicated to the Public Domain. +http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; +/* exported getPostBody */ + +const BinaryInputStream = Components.Constructor( + "@mozilla.org/binaryinputstream;1", + "nsIBinaryInputStream", + "setInputStream" +); + +// eslint-disable-next-line mozilla/reject-importGlobalProperties +Cu.importGlobalProperties(["TextDecoder"]); + +function getPostBody(stream) { + let binaryStream = new BinaryInputStream(stream); + let count = binaryStream.available(); + let arrayBuffer = new ArrayBuffer(count); + while (count > 0) { + let actuallyRead = binaryStream.readArrayBuffer(count, arrayBuffer); + if (!actuallyRead) { + throw new Error("Nothing read from input stream!"); + } + count -= actuallyRead; + } + return new TextDecoder().decode(arrayBuffer); +} |