Skip to content

Bugfix FXIOS-12033 [SEC] Fix check for {searchTerms} in base URL #26223

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 3 commits into from
Apr 24, 2025
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 @@ -46,11 +46,13 @@ struct ASSearchEngineUtilities {
return URLQueryItem(name: $0.name, value: value)
}
// From API docs: "This may be skipped if `{searchTerm}` is included in the base."
if let searchArg = searchURL.searchTermParamName, !searchURL.base.contains("{searchTerm}") {
// Note: term vs terms is not a typo.
// Note: there is a typo in the docs, the value is searchTerms (plural).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should we reference the docs? not sure where the typo was. Or we can add a JIRA ticket number here with more context.

Also interesting to see that the previous comment says:
// Note: term vs terms is not a typo. Do you have context on this 🤔 or seems like this was a mistake?

Copy link
Collaborator Author

@mattreaganmozilla mattreaganmozilla Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The short version is that I was going by the docs we had and that was the origin of the first comment. But I double-checked the data eventually (and then checked with AS team) and it turned out the docs were wrong. Those headers are being fixed, but I wanted to be sure to document which version (searchTerm vs searchTerms) was correct.

if let searchArg = searchURL.searchTermParamName, !searchURL.base.contains("{searchTerms}") {
queryItems.append(URLQueryItem(name: searchArg, value: "{searchTerms}"))
}
components.queryItems = queryItems
if !queryItems.isEmpty {
components.queryItems = queryItems
}

return components.url?.absoluteString.removingPercentEncoding
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,59 @@ import WebKit
import MozillaAppServices

class ASSearchEngineUtilitiesTests: XCTestCase {
private let leo_eng_deu_engine_no_search_params =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 thanks for adding tests!

SearchEngineDefinition(
aliases: [],
charset: "UTF-8",
classification: .unknown,
identifier: "leo_ende_de",
name: "LEO Eng-Deu",
optional: false,
partnerCode: "",
telemetrySuffix: "",
urls: SearchEngineUrls(
search: SearchEngineUrl(
base: "https://dict.leo.org/englisch-deutsch/{searchTerms}",
method: "GET",
params: [],
searchTermParamName: nil
),
suggestions: nil,
trending: nil,
searchForm: nil
),
orderHint: nil,
clickUrl: nil
)
private let leo_eng_deu_engine =
SearchEngineDefinition(
aliases: [],
charset: "UTF-8",
classification: .unknown,
identifier: "leo_ende_de",
name: "LEO Eng-Deu",
optional: false,
partnerCode: "",
telemetrySuffix: "",
urls: SearchEngineUrls(
search: SearchEngineUrl(
base: "https://dict.leo.org/englisch-deutsch/{searchTerms}",
method: "GET",
params: [SearchUrlParam(
name: "foo",
value: "bar",
enterpriseValue: nil,
experimentConfig: nil
)],
searchTermParamName: nil
),
suggestions: nil,
trending: nil,
searchForm: nil
),
orderHint: nil,
clickUrl: nil
)
private let google_US_testEngine =
SearchEngineDefinition(
aliases: ["google"],
Expand Down Expand Up @@ -89,4 +142,34 @@ class ASSearchEngineUtilitiesTests: XCTestCase {
let expected = "https://www.google.com/complete/search?client=firefox&q={searchTerms}"
XCTAssertEqual(result, expected)
}

func testEmptyPartnerCodeSearchURL() {
var engine = google_US_testEngine

// Force an empty partnerCode string:
engine.partnerCode = ""

let result = ASSearchEngineUtilities.convertASSearchURLToOpenSearchURL(engine.urls.search,
for: engine)
let expected = "https://www.google.com/search?client=&q={searchTerms}"
XCTAssertEqual(result, expected)
}

func testSearchTermIncludedInBaseURL() {
let engine = leo_eng_deu_engine_no_search_params

let result = ASSearchEngineUtilities.convertASSearchURLToOpenSearchURL(engine.urls.search,
for: engine)
let expected = "https://dict.leo.org/englisch-deutsch/{searchTerms}"
XCTAssertEqual(result, expected)
Comment on lines +163 to +164
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious, how come we include searchTerms if we don't have search params?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The additional parameters are for things besides the search parameters themselves (they're for additional arguments like the client or partner code identifier, etc.). So it's valid to have a search URL without them.

}

func testSearchTermIncludedInBaseURLAndParams() {
let engine = leo_eng_deu_engine

let result = ASSearchEngineUtilities.convertASSearchURLToOpenSearchURL(engine.urls.search,
for: engine)
let expected = "https://dict.leo.org/englisch-deutsch/{searchTerms}?foo=bar"
XCTAssertEqual(result, expected)
}
}