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
6 changes: 5 additions & 1 deletion Sources/GRPC/GRPCClientStateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,6 @@ extension GRPCClientStateMachine.State {
":scheme": scheme,
"content-type": "application/grpc",
"te": "trailers", // Used to detect incompatible proxies, part of the gRPC specification.
"user-agent": "grpc-swift-nio", // TODO: Add a more specific user-agent.
]

switch compression {
Expand Down Expand Up @@ -549,6 +548,11 @@ extension GRPCClientStateMachine.State {
return (name.lowercased(), value, indexing)
})

// Add default user-agent value, if `customMetadata` didn't contain user-agent
if headers["user-agent"].isEmpty {
headers.add(name: "user-agent", value: "grpc-swift-nio") // TODO: Add a more specific user-agent.
}

return headers
}

Expand Down
19 changes: 19 additions & 0 deletions Tests/GRPCTests/GRPCClientStateMachineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,25 @@ extension GRPCClientStateMachineTests {
}
}

func testSendRequestHeadersWithCustomUserAgent() throws {
let customMetadata: HPACKHeaders = [
"user-agent": "test-user-agent"
]

var stateMachine = self.makeStateMachine(.clientIdleServerIdle(pendingWriteState: .one(), readArity: .one))
stateMachine.sendRequestHeaders(requestHead: .init(
method: "POST",
scheme: "http",
path: "/echo/Get",
host: "localhost",
deadline: .distantFuture,
customMetadata: customMetadata,
encoding: .enabled(.init(forRequests: nil, acceptableForResponses: [], decompressionLimit: .ratio(10)))
)).assertSuccess { headers in
XCTAssertEqual(headers["user-agent"], ["test-user-agent"])
}
}

func testSendRequestHeadersWithNoCompressionInEitherDirection() throws {
var stateMachine = self.makeStateMachine(.clientIdleServerIdle(pendingWriteState: .one(), readArity: .one))
stateMachine.sendRequestHeaders(requestHead: .init(
Expand Down
1 change: 1 addition & 0 deletions Tests/GRPCTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ extension GRPCClientStateMachineTests {
("testSendRequestHeadersFromClosed", testSendRequestHeadersFromClosed),
("testSendRequestHeadersFromIdle", testSendRequestHeadersFromIdle),
("testSendRequestHeadersNormalizesCustomMetadata", testSendRequestHeadersNormalizesCustomMetadata),
("testSendRequestHeadersWithCustomUserAgent", testSendRequestHeadersWithCustomUserAgent),
("testSendRequestHeadersWithNoCompressionForRequests", testSendRequestHeadersWithNoCompressionForRequests),
("testSendRequestHeadersWithNoCompressionForResponses", testSendRequestHeadersWithNoCompressionForResponses),
("testSendRequestHeadersWithNoCompressionInEitherDirection", testSendRequestHeadersWithNoCompressionInEitherDirection),
Expand Down