Skip to content
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

Fix insentive http headers for mock flags #34

Merged
merged 1 commit into from
Nov 5, 2024
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
@@ -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
Loading