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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[Full changelog](https://github.com/mozilla/glean.js/compare/v0.4.0...main)

* [#101](https://github.com/mozilla/glean.js/pull/101): BUGFIX: Only validate Debug View Tag and Source Tags when they are present.
* [#102](https://github.com/mozilla/glean.js/pull/102): BUGFIX: Include a Glean User-Agent header in all pings.

# v0.4.0 (2021-03-10)

Expand Down
7 changes: 4 additions & 3 deletions glean/src/core/upload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ class PingUploader implements PingsDatabaseObserver {
*
* @returns The updated ping.
*/
private preparePingForUpload(ping: QueuedPing): {
private async preparePingForUpload(ping: QueuedPing): Promise<{
headers: Record<string, string>,
payload: string
} {
}> {
const stringifiedBody = JSON.stringify(ping.payload);

let headers = ping.headers || {};
Expand All @@ -131,6 +131,7 @@ class PingUploader implements PingsDatabaseObserver {
"Date": (new Date()).toISOString(),
"X-Client-Type": "Glean.js",
"X-Client-Version": GLEAN_VERSION,
"User-Agent": `Glean/${GLEAN_VERSION} (JS on ${await Glean.platform.info.os()})`
};

return {
Expand All @@ -152,7 +153,7 @@ class PingUploader implements PingsDatabaseObserver {
return { result: UploadResultStatus.RecoverableFailure };
}

const finalPing = this.preparePingForUpload(ping);
const finalPing = await this.preparePingForUpload(ping);
const result = await Glean.platform.uploader.post(
// We are sure that the applicationId is not `undefined` at this point,
// this function is only called when submitting a ping
Expand Down
40 changes: 28 additions & 12 deletions glean/tests/core/upload/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import sinon from "sinon";
import { v4 as UUIDv4 } from "uuid";

import Glean from "../../../src/core/glean";
import PingType from "../../../src/core/pings";
import collectAndStorePing from "../../../src/core/pings/maker";
import PingUploader, { UploadResultStatus } from "../../../src/core/upload";

const sandbox = sinon.createSandbox();
Expand All @@ -19,21 +21,15 @@ const sandbox = sinon.createSandbox();
* @returns The array of identifiers of the pings added to the database.
*/
async function fillUpPingsDatabase(numPings: number): Promise<string[]> {
const path = "some/random/path/doesnt/matter";
const payload = {
ping_info: {
seq: 1,
start_time: "2020-01-11+01:00",
end_time: "2020-01-12+01:00",
},
client_info: {
telemetry_sdk_build: "32.0.0"
}
};
const ping = new PingType({
name: "ping",
includeClientId: true,
sendIfEmpty: true,
});

const identifiers = Array.from({ length: numPings }, () => UUIDv4());
for (const identifier of identifiers) {
await Glean.pingsDatabase.recordPing(path, identifier, payload);
await collectAndStorePing(identifier, ping);
}

return identifiers;
Expand Down Expand Up @@ -190,4 +186,24 @@ describe("PingUploader", function() {
await waitForGleanUploader();
assert.strictEqual(stub.callCount, 3);
});

it("correctly build ping request", async function () {
const postSpy = sandbox.spy(Glean.platform.uploader, "post");

const expectedDocumentId = (await fillUpPingsDatabase(1))[0];
await waitForGleanUploader();

const url = postSpy.firstCall.args[0].split("/");
const documentId = url[url.length - 1];
const headers = postSpy.firstCall.args[2] || {};

assert.strictEqual(documentId, expectedDocumentId);

assert.ok("Date" in headers);
assert.ok("User-Agent" in headers);
assert.ok("Content-Type" in headers);
assert.ok("X-Client-Type" in headers);
assert.ok("X-Client-Version" in headers);
assert.ok("Content-Length" in headers);
});
});