Skip to content

Commit

Permalink
fix(clients): use correct user agent (#4103)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Nov 14, 2024
1 parent 66acf19 commit 0985a1c
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 11 deletions.
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

0 comments on commit 0985a1c

Please sign in to comment.