Skip to content

fix(clients): use correct user agent #4103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 14, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ Iterable<AgentSegment> platformAgentSegments() => [

/// [AlgoliaAgent] for web platforms as query param.
void platformAlgoliaAgent(dio.RequestOptions options, String agent) {
options.queryParameters["X-Algolia-Agent"] = agent;
options.queryParameters["x-algolia-agent"] = agent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ final class EchoRequestBuilder: RequestBuilder {
originalBodyData: urlRequest.httpBody,
path: urlComponents?.percentEncodedPath ?? "",
host: url.host ?? "",
algoliaAgent: headers["X-Algolia-Agent"] ?? "",
algoliaAgent: headers["User-Agent"] ?? "",
queryParameters: queryParameters,
headers: headers
)
Expand Down
8 changes: 1 addition & 7 deletions templates/swift/tests/client/tests.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,8 @@
{{/shouldScope}}
{{#dynamicTemplate}}{{/dynamicTemplate}}
{{#testUserAgent}}
let userAgent = try XCTUnwrap(echoResponse.headers?["User-Agent"])
guard let userAgent = userAgent else {
XCTFail("Expected user-agent header")
return
}

let pattern = "{{#lambda.escapeSlash}}{{{match.value}}}{{/lambda.escapeSlash}}"
XCTAssertNoThrow(try regexMatch(userAgent, against: pattern), "Expected " + userAgent + " to match the following regex: " + pattern);
XCTAssertNoThrow(try regexMatch(echoResponse.algoliaAgent, against: pattern), "Expected " + echoResponse.algoliaAgent + " to match the following regex: " + pattern);
{{/testUserAgent}}
{{#testTimeouts}}
XCTAssertEqual(TimeInterval({{{matchResponseTimeout}}} / 1000), echoResponse.timeout);
Expand Down
5 changes: 4 additions & 1 deletion tests/output/swift/Tests/Utils/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public func union(expected: Any?, received: Any?) -> Any? {
}

public func regexMatch(_ received: String, against: String) -> Bool {
let matches = try! NSRegularExpression(pattern: against).matches(in: received, range: .init(location: 0, length: received.utf16.count))
let matches = try! NSRegularExpression(pattern: against).matches(
in: received,
range: .init(location: 0, length: received.utf16.count)
)
return !matches.isEmpty
}
2 changes: 1 addition & 1 deletion website/docs/add-new-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ The `User-Agent` MUST match the following regular expression:
^Algolia for <LANGUAGE> \\(\\d+\\.\\d+\\.\\d+(-?.*)?\\)(; [a-zA-Z. ]+ (\\(\\d+((\\.\\d+)?\\.\\d+)?(-?.*)?\\))?)*(; <CLIENT> (\\(\\d+\\.\\d+\\.\\d+(-?.*)?\\)))(; [a-zA-Z. ]+ (\\(\\d+((\\.\\d+)?\\.\\d+)?(-?.*)?\\))?)*$
```

The function MUST be named `addAlgoliaAgent` because of JavaScript exception that doesn't allow custom `User-Agent` in the header, and must use `x-algolia-agent` for JavaScript.
The function MUST be named `addAlgoliaAgent` because of JavaScript exception that doesn't allow custom `User-Agent` in the header, and must use `x-algolia-agent` for JavaScript and Dart.

### Dependencies

Expand Down
Loading