Skip to content

Commit

Permalink
Fix insentive http headers for mock flags
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufozgul committed Nov 5, 2024
1 parent 55232aa commit 33a7f9a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions Core/MockingStarCore/Sources/MockingStarCore/MockingStar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 33a7f9a

Please sign in to comment.