Skip to content

Commit d5e953e

Browse files
author
Beatriz Rizental
authored
Bug 1697208 - Include a Glean User-Agent header in all pings (#102)
1 parent 95f0e57 commit d5e953e

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[Full changelog](https://github.com/mozilla/glean.js/compare/v0.4.0...main)
44

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

78
# v0.4.0 (2021-03-10)
89

glean/src/core/upload/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ class PingUploader implements PingsDatabaseObserver {
117117
*
118118
* @returns The updated ping.
119119
*/
120-
private preparePingForUpload(ping: QueuedPing): {
120+
private async preparePingForUpload(ping: QueuedPing): Promise<{
121121
headers: Record<string, string>,
122122
payload: string
123-
} {
123+
}> {
124124
const stringifiedBody = JSON.stringify(ping.payload);
125125

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

136137
return {
@@ -152,7 +153,7 @@ class PingUploader implements PingsDatabaseObserver {
152153
return { result: UploadResultStatus.RecoverableFailure };
153154
}
154155

155-
const finalPing = this.preparePingForUpload(ping);
156+
const finalPing = await this.preparePingForUpload(ping);
156157
const result = await Glean.platform.uploader.post(
157158
// We are sure that the applicationId is not `undefined` at this point,
158159
// this function is only called when submitting a ping

glean/tests/core/upload/index.spec.ts

+28-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import sinon from "sinon";
77
import { v4 as UUIDv4 } from "uuid";
88

99
import Glean from "../../../src/core/glean";
10+
import PingType from "../../../src/core/pings";
11+
import collectAndStorePing from "../../../src/core/pings/maker";
1012
import PingUploader, { UploadResultStatus } from "../../../src/core/upload";
1113

1214
const sandbox = sinon.createSandbox();
@@ -19,21 +21,15 @@ const sandbox = sinon.createSandbox();
1921
* @returns The array of identifiers of the pings added to the database.
2022
*/
2123
async function fillUpPingsDatabase(numPings: number): Promise<string[]> {
22-
const path = "some/random/path/doesnt/matter";
23-
const payload = {
24-
ping_info: {
25-
seq: 1,
26-
start_time: "2020-01-11+01:00",
27-
end_time: "2020-01-12+01:00",
28-
},
29-
client_info: {
30-
telemetry_sdk_build: "32.0.0"
31-
}
32-
};
24+
const ping = new PingType({
25+
name: "ping",
26+
includeClientId: true,
27+
sendIfEmpty: true,
28+
});
3329

3430
const identifiers = Array.from({ length: numPings }, () => UUIDv4());
3531
for (const identifier of identifiers) {
36-
await Glean.pingsDatabase.recordPing(path, identifier, payload);
32+
await collectAndStorePing(identifier, ping);
3733
}
3834

3935
return identifiers;
@@ -190,4 +186,24 @@ describe("PingUploader", function() {
190186
await waitForGleanUploader();
191187
assert.strictEqual(stub.callCount, 3);
192188
});
189+
190+
it("correctly build ping request", async function () {
191+
const postSpy = sandbox.spy(Glean.platform.uploader, "post");
192+
193+
const expectedDocumentId = (await fillUpPingsDatabase(1))[0];
194+
await waitForGleanUploader();
195+
196+
const url = postSpy.firstCall.args[0].split("/");
197+
const documentId = url[url.length - 1];
198+
const headers = postSpy.firstCall.args[2] || {};
199+
200+
assert.strictEqual(documentId, expectedDocumentId);
201+
202+
assert.ok("Date" in headers);
203+
assert.ok("User-Agent" in headers);
204+
assert.ok("Content-Type" in headers);
205+
assert.ok("X-Client-Type" in headers);
206+
assert.ok("X-Client-Version" in headers);
207+
assert.ok("Content-Length" in headers);
208+
});
193209
});

0 commit comments

Comments
 (0)