Skip to content

Commit 119b3aa

Browse files
committed
MOBILE-130: Store operationsDomain from JSON config in canonical scheme://host form
Backend may send `http://` for some anonymizer setups; previous logic stored the raw string verbatim, so trailing slashes / scheme-vs-host mismatches caused spurious re-saves. Normalize via HostNormalizer.toBaseURLString on save: scheme preserved (http/https), trailing slash stripped, missing scheme defaults to https. Idempotent — first config fetch after upgrade rewrites legacy values once, then `.keep` stably. Adapts existing Policy tests to the canonical form and adds coverage for the trailing-slash case from JSON config, http preservation, canonical-equality keep, and the legacy-value upgrade path.
1 parent b2fd251 commit 119b3aa

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

Mindbox/InAppMessages/Configuration/Services/OperationsDomainConfigPolicy.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ enum OperationsDomainConfigPolicy {
3030
return .keep
3131
}
3232

33-
return value == currentlyStored ? .keep : .save(value)
33+
// Store canonical `scheme://host` so backend's choice of `http`/`https`
34+
// is preserved across restarts and trailing slashes don't cause re-saves.
35+
let normalized = HostNormalizer.toBaseURLString(value)
36+
return normalized == currentlyStored ? .keep : .save(normalized)
3437
}
3538
}

MindboxTests/Network/OperationsURLRoutingTests.swift

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,27 +198,27 @@ struct OperationsURLRoutingTests {
198198

199199
@Test("Policy — saves a new valid value when storage is empty")
200200
func policySavesNewValueFromEmpty() {
201-
#expect(OperationsDomainConfigPolicy.action(for: "x.ru", currentlyStored: nil) == .save("x.ru"))
201+
#expect(OperationsDomainConfigPolicy.action(for: "x.ru", currentlyStored: nil) == .save("https://x.ru"))
202202
}
203203

204204
@Test("Policy — saves when value changes")
205205
func policySavesOnChange() {
206-
#expect(OperationsDomainConfigPolicy.action(for: "new.ru", currentlyStored: "old.ru") == .save("new.ru"))
206+
#expect(OperationsDomainConfigPolicy.action(for: "new.ru", currentlyStored: "https://old.ru") == .save("https://new.ru"))
207207
}
208208

209209
@Test("Policy — keeps when incoming value equals stored")
210210
func policyKeepsOnIdenticalValue() {
211-
#expect(OperationsDomainConfigPolicy.action(for: "x.ru", currentlyStored: "x.ru") == .keep)
211+
#expect(OperationsDomainConfigPolicy.action(for: "https://x.ru", currentlyStored: "https://x.ru") == .keep)
212212
}
213213

214214
@Test("Policy — clears on null/missing config when something is stored")
215215
func policyClearsOnNullWhenStored() {
216-
#expect(OperationsDomainConfigPolicy.action(for: nil, currentlyStored: "old.ru") == .clear)
216+
#expect(OperationsDomainConfigPolicy.action(for: nil, currentlyStored: "https://old.ru") == .clear)
217217
}
218218

219219
@Test("Policy — clears on empty string when something is stored")
220220
func policyClearsOnEmptyWhenStored() {
221-
#expect(OperationsDomainConfigPolicy.action(for: "", currentlyStored: "old.ru") == .clear)
221+
#expect(OperationsDomainConfigPolicy.action(for: "", currentlyStored: "https://old.ru") == .clear)
222222
}
223223

224224
@Test("Policy — no-ops when nothing stored and nothing came")
@@ -229,7 +229,43 @@ struct OperationsURLRoutingTests {
229229

230230
@Test("Policy — preserves previous value when incoming host is format-broken")
231231
func policyKeepsOnInvalidFormat() {
232-
#expect(OperationsDomainConfigPolicy.action(for: "host with spaces", currentlyStored: "good.ru") == .keep)
232+
#expect(OperationsDomainConfigPolicy.action(for: "host with spaces", currentlyStored: "https://good.ru") == .keep)
233+
}
234+
235+
@Test("Policy — normalizes scheme + trailing slash to canonical form")
236+
func policyNormalizesSchemeAndTrailingSlash() {
237+
#expect(
238+
OperationsDomainConfigPolicy.action(
239+
for: "https://anonymizer-api-regular.client.ru/",
240+
currentlyStored: nil
241+
) == .save("https://anonymizer-api-regular.client.ru")
242+
)
243+
}
244+
245+
@Test("Policy — preserves http scheme from config (does not force https)")
246+
func policyPreservesHttpScheme() {
247+
#expect(
248+
OperationsDomainConfigPolicy.action(for: "http://x.ru/", currentlyStored: nil)
249+
== .save("http://x.ru")
250+
)
251+
}
252+
253+
@Test("Policy — keeps when canonical form equals stored despite raw differences")
254+
func policyKeepsWhenCanonicalFormMatches() {
255+
#expect(
256+
OperationsDomainConfigPolicy.action(
257+
for: "https://x.ru/",
258+
currentlyStored: "https://x.ru"
259+
) == .keep
260+
)
261+
}
262+
263+
@Test("Policy — upgrade path: legacy bare-host stored value re-saves once as canonical")
264+
func policyUpgradesLegacyStoredValue() {
265+
#expect(
266+
OperationsDomainConfigPolicy.action(for: "x.ru", currentlyStored: "x.ru")
267+
== .save("https://x.ru")
268+
)
233269
}
234270

235271
// MARK: - Persistence lifecycle

0 commit comments

Comments
 (0)