diff --git a/src/capture.js b/src/capture.js index cf70e4e..d808c5c 100644 --- a/src/capture.js +++ b/src/capture.js @@ -25,12 +25,14 @@ mode: "no-cors", headers: { "Content-Type": "application/json", - "X-Has-Changes": hasChanges ? "1" : "0", }, keepalive: true, credentials: "omit", cache: "no-cache", - body: JSON.stringify(Object.fromEntries(formData.entries())), + body: JSON.stringify({ + "hasChanges": hasChanges, + "form": Object.fromEntries(formData.entries()) + }), }) } diff --git a/tests/capture.test.js b/tests/capture.test.js index 3b38482..533344c 100644 --- a/tests/capture.test.js +++ b/tests/capture.test.js @@ -21,7 +21,7 @@ const GetMutationObserverMock = function () { /* end mock of MutationObserver */ test('Edit scores', async () => { - let expectedPost = { + let expectedForm = { "hlf":"0", "prt":"188619", "prti":"999999", @@ -95,6 +95,11 @@ test('Edit scores', async () => { "AddEstim":"0", }; + let expectedPost = { + hasChanges: false, + form: expectedForm, + } + // base HTML of page document.body.innerHTML = await readFile(__dirname + "/html/edit-scores.html", {encoding:'utf8'}); document.body.querySelector('form').submit = () => {} @@ -121,7 +126,6 @@ test('Edit scores', async () => { body: JSON.stringify(expectedPost), headers: { "Content-Type": "application/json", - "X-Has-Changes": "0", }, cache: "no-cache", credentials: "omit", @@ -130,11 +134,13 @@ test('Edit scores', async () => { }); // emulate change of input and check that Lib send event to endpoint - fetch.mockReset(); + fetch.mockClear(); - expectedPost["st110030_2-999999"] = "3"; + expectedPost.hasChanges = true; + expectedForm["st110030_2-999999"] = "3"; document.querySelector('[name="st110030_2-999999"]').value = "3"; + // emulate click on button and check that Lib send event to endpoint document.querySelector('[type=submit]').dispatchEvent(new MouseEvent('click')); @@ -144,7 +150,6 @@ test('Edit scores', async () => { body: JSON.stringify(expectedPost), headers: { "Content-Type": "application/json", - "X-Has-Changes": "1", }, cache: "no-cache", credentials: "omit", @@ -156,7 +161,7 @@ test('Edit scores', async () => { /* end mock of MutationObserver */ test('Create lesson', async () => { - let expectedPost = { + let expectedForm = { "hlf":"0", "prt":"193000", "prti":"0", @@ -171,6 +176,11 @@ test('Create lesson', async () => { "grade":"" }; + let expectedPost = { + hasChanges: false, + form: expectedForm, + } + // base HTML of page document.body.innerHTML = await readFile(__dirname + "/html/create-lesson-form.html", {encoding:'utf8'}); document.body.querySelector('form').submit = () => {} @@ -204,7 +214,6 @@ test('Create lesson', async () => { body: JSON.stringify(expectedPost), headers: { "Content-Type": "application/json", - "X-Has-Changes": "0", }, cache: "no-cache", credentials: "omit", @@ -215,7 +224,7 @@ test('Create lesson', async () => { /* end mock of MutationObserver */ test('Edit lesson', async () => { - let expectedPost = { + let expectedForm = { "hlf":"0", "prt":"193000", "prti":"999999", @@ -230,6 +239,11 @@ test('Edit lesson', async () => { "grade":"2" }; + let expectedPost = { + hasChanges: false, + form: expectedForm, + } + // base HTML of page document.body.innerHTML = await readFile(__dirname + "/html/edit-lesson-form.html", {encoding:'utf8'}); document.body.querySelector('form').submit = () => {} @@ -259,7 +273,6 @@ test('Edit lesson', async () => { body: JSON.stringify(expectedPost), headers: { "Content-Type": "application/json", - "X-Has-Changes": "0", }, cache: "no-cache", credentials: "omit", @@ -269,7 +282,7 @@ test('Edit lesson', async () => { }); test('Delete lesson', async () => { - let expectedPost = { + let expectedForm = { "sesID":"00AB0000-0000-0000-0000-000CD0000AA0", "n":"11", "action":"delete", @@ -282,6 +295,11 @@ test('Delete lesson', async () => { "course":"undefined", }; + let expectedPost = { + hasChanges: true, + form: expectedForm, + } + // base HTML of page document.body.innerHTML = await readFile(__dirname + "/html/delete-lesson-link.html", {encoding:'utf8'}); @@ -319,7 +337,6 @@ test('Delete lesson', async () => { body: JSON.stringify(expectedPost), headers: { "Content-Type": "application/json", - "X-Has-Changes": "1", }, cache: "no-cache", credentials: "omit", diff --git a/tests/worker.test.js b/tests/worker.test.js index 9dff732..5d745e5 100644 --- a/tests/worker.test.js +++ b/tests/worker.test.js @@ -130,14 +130,13 @@ test('addEventListener_POST_with_payload_Request', async () => { }; - const sendAndAssertRequestResponse = async function(hasChangeHeader, expectHasChanges) { - mockSend.mockReset() - mockSendMessageCommand.mockReset() + const sendAndAssertRequestResponse = async function(expectHasChanges) { + mockSend.mockClear() + mockSendMessageCommand.mockClear() const headers = { "Cf-Connecting-Ip": "127.10.10.10", "Referer": "http://dekanat/index.html", - "X-Has-Changes": hasChangeHeader, } headers.get = (key) => headers[key] @@ -145,7 +144,10 @@ test('addEventListener_POST_with_payload_Request', async () => { method: "POST", url: "http://localhost/", headers: headers, - json: jest.fn().mockResolvedValue(form), + json: jest.fn().mockResolvedValue({ + hasChanges: expectHasChanges, + form: form, + }), }; await doRequest(request) @@ -161,14 +163,15 @@ test('addEventListener_POST_with_payload_Request', async () => { expect(sendMessageCommandConfig.QueueUrl).toBe(AwsSqsQueueUrl) const actualMessageBody = JSON.parse(sendMessageCommandConfig.MessageBody) + expect(typeof actualMessageBody).toBe('object') + expect(actualMessageBody.formHasChanges).toBe(expectHasChanges) expect(actualMessageBody.form).toStrictEqual(form) expect(actualMessageBody.ip).toBe(headers['Cf-Connecting-Ip']) expect(actualMessageBody.referer).toBe(headers['Referer']) expect(actualMessageBody.timestamp).toBe(currentTimestampInSeconds) - expect(actualMessageBody.formHasChanges).toBe(expectHasChanges) } - await sendAndAssertRequestResponse("1", true) - await sendAndAssertRequestResponse("0", false) + await sendAndAssertRequestResponse(true) + await sendAndAssertRequestResponse(false) }) \ No newline at end of file diff --git a/worker/index.js b/worker/index.js index aa91f33..751c2b0 100644 --- a/worker/index.js +++ b/worker/index.js @@ -14,12 +14,12 @@ function myCredentialProvider() { } /** - * @param {Object} form + * @param {Object} postData * @param {Headers} headers * @return {Promise} */ -async function sendMessage(form, headers) { - if (!Object.keys(form).length) { +async function sendMessage(postData, headers) { + if (!postData.form || !Object.keys(postData.form).length) { return Promise.resolve(); } @@ -27,8 +27,8 @@ async function sendMessage(form, headers) { timestamp: Date.now() / 1E3 | 0, ip: headers.get("Cf-Connecting-Ip"), referer: headers.get("Referer"), - formHasChanges: headers.get("X-Has-Changes") === "1", - form: form, + formHasChanges: Boolean(postData.hasChanges), + form: postData.form, } return clientSqs.send(new SendMessageCommand({