Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 6 additions & 3 deletions src/http.js
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
36 changes: 23 additions & 13 deletions test/test-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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");
});
});
Loading