From 33a7f9ad19bd8b621d31b8b52d32293339618349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20=C3=96zg=C3=BCl?= Date: Tue, 5 Nov 2024 16:20:05 +0300 Subject: [PATCH] Fix insentive http headers for mock flags --- .../Extensions/Dictionary+Extension.swift | 15 +++++++++++++++ .../Sources/MockingStarCore/MockServerFlag.swift | 4 ++-- .../Sources/MockingStarCore/MockingStar.swift | 16 ++++++++-------- 3 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 Core/CommonKit/Sources/CommonKit/Extensions/Dictionary+Extension.swift diff --git a/Core/CommonKit/Sources/CommonKit/Extensions/Dictionary+Extension.swift b/Core/CommonKit/Sources/CommonKit/Extensions/Dictionary+Extension.swift new file mode 100644 index 0000000000..540acfefa5 --- /dev/null +++ b/Core/CommonKit/Sources/CommonKit/Extensions/Dictionary+Extension.swift @@ -0,0 +1,15 @@ +// +// Dictionary+Extension.swift +// CommonKit +// +// Created by Yusuf Özgül on 5.11.2024. +// + +import Foundation + +public extension Dictionary where Key == String, Value == String { + func caseInsensitiveSearch(for key: String) -> String? { + guard let value = self.first(where: { NSString(string: $0.key).caseInsensitiveCompare(key) == .orderedSame })?.value else { return nil } + return value.isEmpty ? nil : value + } +} diff --git a/Core/MockingStarCore/Sources/MockingStarCore/MockServerFlag.swift b/Core/MockingStarCore/Sources/MockingStarCore/MockServerFlag.swift index c003cffaf9..3522ef75be 100644 --- a/Core/MockingStarCore/Sources/MockingStarCore/MockServerFlag.swift +++ b/Core/MockingStarCore/Sources/MockingStarCore/MockServerFlag.swift @@ -23,9 +23,9 @@ struct MockServerFlags { case `default` init(from rawFlags: [String:String]) { - if rawFlags["disableLiveEnvironment", default: "false"] == "true" { + if rawFlags.caseInsensitiveSearch(for: "disableLiveEnvironment") == "true" { self = .onlyMock - } else if rawFlags["disableMockResponse", default: "false"] == "true" { + } else if rawFlags.caseInsensitiveSearch(for: "disableMockResponse") == "true" { self = .onlyLive } else { self = .default diff --git a/Core/MockingStarCore/Sources/MockingStarCore/MockingStar.swift b/Core/MockingStarCore/Sources/MockingStarCore/MockingStar.swift index f0d07fd90a..fb2391c911 100644 --- a/Core/MockingStarCore/Sources/MockingStarCore/MockingStar.swift +++ b/Core/MockingStarCore/Sources/MockingStarCore/MockingStar.swift @@ -267,18 +267,18 @@ extension MockingStarCore: ServerMockHandlerInterface { request.httpBody = body let scenario: String - let mockDomain = rawFlags["mockDomain"].isNilOrEmpty ? "Dev" : rawFlags["mockDomain", default: "Dev"] - let deviceId = rawFlags["deviceId", default: ""] + let mockDomain = rawFlags.caseInsensitiveSearch(for: "mockDomain") ?? "Dev" + let deviceId = rawFlags.caseInsensitiveSearch(for: "deviceId").orEmpty - if rawFlags["scenario"].isNilOrEmpty { + if rawFlags.caseInsensitiveSearch(for: "scenario").isNilOrEmpty { scenario = await scenariosActor.decider(for: mockDomain).decideScenario(request: request, deviceId: deviceId).orEmpty } else { - scenario = rawFlags["scenario", default: ""] + scenario = rawFlags.caseInsensitiveSearch(for: "scenario").orEmpty } let flags = MockServerFlags(mockSource: .init(from: rawFlags), scenario: scenario, - shouldNotMock: rawFlags["shouldNotMock", default: "false"] == "true", + shouldNotMock: rawFlags.caseInsensitiveSearch(for: "shouldNotMock").orEmpty == "true", domain: mockDomain, deviceId: deviceId) @@ -301,12 +301,12 @@ extension MockingStarCore: ServerMockSearchHandlerInterface { /// - `body`: The response body as `Data`. /// - `headers`: The response headers as `[String : String]`. public func search(path: String, method: String, scenario: String?, rawFlags: [String : String]) async throws -> (status: Int, body: Data, headers: [String : String]) { - let mockDomain = rawFlags["mockDomain"].isNilOrEmpty ? "Dev" : rawFlags["mockDomain", default: "Dev"] - let deviceId = rawFlags["deviceId", default: ""] + let mockDomain = rawFlags.caseInsensitiveSearch(for: "mockDomain") ?? "Dev" + let deviceId = rawFlags.caseInsensitiveSearch(for: "deviceId").orEmpty let flags = MockServerFlags(mockSource: .init(from: rawFlags), scenario: scenario, - shouldNotMock: rawFlags["shouldNotMock", default: "false"] == "true", + shouldNotMock: rawFlags.caseInsensitiveSearch(for: "shouldNotMock").orEmpty == "true", domain: mockDomain, deviceId: deviceId)