Skip to content

Commit

Permalink
refactor: add support user defined UA
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Polyakov <[email protected]>
  • Loading branch information
polRk committed Oct 19, 2024
1 parent b4d1f11 commit cc5907b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 4 deletions.
17 changes: 17 additions & 0 deletions packages/connect/src/protocol-connect/request-header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ describe("requestHeader", () => {
expect(headers.get("Connect-Timeout-Ms")).toBe("10");
});

it("should create request headers with user provided user-agent", () => {
const headers = requestHeader(
"unary",
true,
10,
{ "User-Agent": "grpc-es/0.0.0" },
true,
);
expect(listHeaderKeys(headers)).toEqual([
"connect-protocol-version",
"connect-timeout-ms",
"content-type",
"user-agent",
]);
expect(headers.get("User-Agent")).toBe("grpc-es/0.0.0");
});

it("should exclude user-agent", () => {
const headers = requestHeader("unary", true, undefined, undefined, false);
expect(listHeaderKeys(headers)).toEqual([
Expand Down
7 changes: 6 additions & 1 deletion packages/connect/src/protocol-connect/request-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ export function requestHeader(
: contentTypeStreamJson,
);
result.set(headerProtocolVersion, protocolVersion);
if (setUserAgent) {

if (!result.has(headerUserAgent) && setUserAgent) {
// Note that we do not strictly comply with gRPC user agents.
// We use "connect-es/1.2.3" where gRPC would use "grpc-es/1.2.3".
// See https://github.com/grpc/grpc/blob/c462bb8d485fc1434ecfae438823ca8d14cf3154/doc/PROTOCOL-HTTP2.md#user-agents
result.set(headerUserAgent, "CONNECT_ES_USER_AGENT");
}

return result;
}

Expand Down
36 changes: 36 additions & 0 deletions packages/connect/src/protocol-grpc-web/request-header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,42 @@ describe("requestHeader", () => {
expect(headers.get("Grpc-Timeout")).toBe("10m");
});

it("should set user provided user-agent header", () => {
const headers = requestHeader(
true,
10,
{ "User-Agent": "grpc-es/0.0.0" },
true,
);
expect(listHeaderKeys(headers)).toEqual([
"content-type",
"grpc-timeout",
"user-agent",
"x-grpc-web",
"x-user-agent",
]);
expect(headers.get("User-Agent")).toBe("grpc-es/0.0.0");
expect(headers.get("X-User-Agent")).toBe("grpc-es/0.0.0");
});

it("should set user provided x-user-agent header", () => {
const headers = requestHeader(
true,
10,
{ "X-User-Agent": "grpc-es/0.0.0" },
true,
);
expect(listHeaderKeys(headers)).toEqual([
"content-type",
"grpc-timeout",
"user-agent",
"x-grpc-web",
"x-user-agent",
]);
expect(headers.get("User-Agent")).toBe("grpc-es/0.0.0");
expect(headers.get("X-User-Agent")).toBe("grpc-es/0.0.0");
});

it("should create request headers with compression", () => {
const compressionMock: Compression = {
name: "gzip",
Expand Down
11 changes: 9 additions & 2 deletions packages/connect/src/protocol-grpc-web/request-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,16 @@ export function requestHeader(
// Note that we do not strictly comply with gRPC user agents.
// We use "connect-es/1.2.3" where gRPC would use "grpc-es/1.2.3".
// See https://github.com/grpc/grpc/blob/c462bb8d485fc1434ecfae438823ca8d14cf3154/doc/PROTOCOL-HTTP2.md#user-agents
result.set(headerXUserAgent, "CONNECT_ES_USER_AGENT");
let userAgent = "CONNECT_ES_USER_AGENT";
userAgent = result.has(headerUserAgent)
? result.get(headerUserAgent)!
: result.has(headerXUserAgent)
? result.get(headerXUserAgent)!
: userAgent;

result.set(headerXUserAgent, userAgent);
if (setUserAgent) {
result.set(headerUserAgent, "CONNECT_ES_USER_AGENT");
result.set(headerUserAgent, userAgent);
}
if (timeoutMs !== undefined) {
result.set(headerTimeout, `${timeoutMs}m`);
Expand Down
11 changes: 11 additions & 0 deletions packages/connect/src/protocol-grpc/request-header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ describe("requestHeader", () => {
expect(headers.get("Grpc-Timeout")).toBe("10m");
});

it("should create request headers with user provided user-agent", () => {
const headers = requestHeader(true, 10, { "User-Agent": "grpc-es/0.0.0" });
expect(listHeaderKeys(headers)).toEqual([
"content-type",
"grpc-timeout",
"te",
"user-agent",
]);
expect(headers.get("User-Agent")).toBe("grpc-es/0.0.0");
});

it("should create request headers with compression", () => {
const compressionMock: Compression = {
name: "gzip",
Expand Down
9 changes: 8 additions & 1 deletion packages/connect/src/protocol-grpc/request-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ export function requestHeader(
timeoutMs: number | undefined,
userProvidedHeaders: HeadersInit | undefined,
): Headers {
const result = new Headers(userProvidedHeaders);
const result = new Headers(userProvidedHeaders ?? {});
result.set(
headerContentType,
useBinaryFormat ? contentTypeProto : contentTypeJson,
);

if (!result.has(headerUserAgent)) {
// Note that we do not strictly comply with gRPC user agents.
// We use "connect-es/1.2.3" where gRPC would use "grpc-es/1.2.3".
// See https://github.com/grpc/grpc/blob/c462bb8d485fc1434ecfae438823ca8d14cf3154/doc/PROTOCOL-HTTP2.md#user-agents
result.set(headerUserAgent, "CONNECT_ES_USER_AGENT");
}

if (timeoutMs !== undefined) {
result.set(headerTimeout, `${timeoutMs}m`);
}
Expand Down

0 comments on commit cc5907b

Please sign in to comment.