From e996aaf5a7dd77183780454fb852f50f32de35ed Mon Sep 17 00:00:00 2001 From: Ivan Todinov Date: Fri, 7 Aug 2026 17:32:54 +0300 Subject: [PATCH] feat: modify request headers --- src/client.js | 2 +- src/http.js | 9 ++++++--- test/test-http.js | 36 +++++++++++++++++++++++------------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/client.js b/src/client.js index bea448e..6df0b91 100644 --- a/src/client.js +++ b/src/client.js @@ -58,7 +58,7 @@ export default function Client(options) { this.options = options; - this.http = new HTTP(options.requestTimeout, options.clientName, options.projectId, options.cors); + this.http = new HTTP(options.requestTimeout, options.clientName, this._getDeviceName(), this._getDeviceTag(), options.projectId, options.cors); this.crypto = new Crypto(options.seed); diff --git a/src/http.js b/src/http.js index 9fd66ff..fe26132 100644 --- a/src/http.js +++ b/src/http.js @@ -1,6 +1,8 @@ -export default function HTTP(timeout, clientName, projectId, cors) { +export default function HTTP(timeout, clientName, deviceName, deviceTag, projectId, cors) { this.requestTimeout = timeout; this.clientName = clientName; + this.deviceName = deviceName; + this.deviceTag = deviceTag; this.projectId = projectId; this.cors = cors; } @@ -63,8 +65,9 @@ HTTP.prototype.request = function (options, callback) { request.timeout = this.requestTimeout; - request.setRequestHeader("X-MIRACL-CID", this.projectId); - request.setRequestHeader("X-MIRACL-CLIENT", this.clientName); + request.setRequestHeader("X-Miracl-Client", this.clientName); + request.setRequestHeader("X-Miracl-Device-Name", this.deviceName); + request.setRequestHeader("X-Miracl-Device-Tag", this.deviceTag); // Set authorization header if provided if (options.authorization) { diff --git a/test/test-http.js b/test/test-http.js index fab9f62..cdeec34 100644 --- a/test/test-http.js +++ b/test/test-http.js @@ -15,7 +15,7 @@ describe("HTTP request", () => { beforeEach(() => { requests = []; - client = new HTTP(4000, "clientName", "projectID", false); + client = new HTTP(4000, "clientName", "deviceName", "deviceTag", "projectID", false); }); it("should throw error missing callback", () => { @@ -136,34 +136,44 @@ describe("HTTP request", () => { expect(callback.firstCall.args[1].status).to.equal(0); }); - it("should set project ID header", () => { + it("should add project ID parameter for CORS requests", () => { + client = new HTTP(4000, "clientName", "deviceName", "deviceTag", "projectID", true); + client.request({ - url: "/test-project-id-header", + url: "/test-project-id-parameter", }, () => {}); expect(requests.length).to.equal(1); - expect(requests[0].requestHeaders).to.have.property("X-MIRACL-CID"); - expect(requests[0].requestHeaders["X-MIRACL-CID"]).to.equal("projectID"); + expect(requests[0].url).to.contain("?project_id=projectID"); }); - it("should add project ID parameter for CORS requests", () => { - client = new HTTP(4000, "clientName", "projectID", true); + it("should set client version header", () => { + client.request({ + url: "/test-client-version-header", + }, () => {}); + + expect(requests.length).to.equal(1); + expect(requests[0].requestHeaders).to.have.property("X-Miracl-Client"); + expect(requests[0].requestHeaders["X-Miracl-Client"]).to.equal("clientName"); + }); + it("should set device name header", () => { client.request({ - url: "/test-project-id-parameter", + url: "/test-device-name-header", }, () => {}); expect(requests.length).to.equal(1); - expect(requests[0].url).to.contain("?project_id=projectID"); + expect(requests[0].requestHeaders).to.have.property("X-Miracl-Device-Name"); + expect(requests[0].requestHeaders["X-Miracl-Device-Name"]).to.equal("deviceName"); }); - it("should set client version header", () => { + it("should set device tag header", () => { client.request({ - url: "/test-client-version-header", + url: "/test-device-tag-header", }, () => {}); expect(requests.length).to.equal(1); - expect(requests[0].requestHeaders).to.have.property("X-MIRACL-CLIENT"); - expect(requests[0].requestHeaders["X-MIRACL-CLIENT"]).to.equal("clientName"); + expect(requests[0].requestHeaders).to.have.property("X-Miracl-Device-Tag"); + expect(requests[0].requestHeaders["X-Miracl-Device-Tag"]).to.equal("deviceTag"); }); });