Skip to content
Open
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
5 changes: 5 additions & 0 deletions Sources/GOFeatureFlag/controller/goff_api.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class GoFeatureFlagAPI {
"application/json",
forHTTPHeaderField: "Content-Type"
)
if let headers = self.options.customHeaders {
for (key, value) in headers {
request.SetValue(value, forHTTPHeaderField: key)
}
}
if let apiKey = self.options.apiKey {
request.setValue("Bearer \(apiKey)", forHTTPHeaderField:"Authorization")
}
Expand Down
7 changes: 7 additions & 0 deletions Sources/GOFeatureFlag/options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public struct GoFeatureFlagProviderOptions {
* Default: null
*/
public var apiKey: String?
/**
* (optional) custom headers to be sent for every HTTP request.
* default: empty
*/
public var customHeaders: [String:String]? = [:]
/**
* (optional) interval time we publish statistics collection data to the proxy.
* The parameter is used only if the cache is enabled, otherwise the collection of the data is done directly
Expand All @@ -42,12 +47,14 @@ public struct GoFeatureFlagProviderOptions {
endpoint: String,
pollInterval: TimeInterval = 60,
apiKey: String? = nil,
customHeaders: [String:String]? = [:],
dataFlushInterval: TimeInterval = 600,
exporterMetadata: [String:ExporterMetadataValue]? = [:],
networkService: NetworkingService? = URLSession.shared) {
self.endpoint = endpoint
self.pollInterval = pollInterval
self.apiKey = apiKey
self.customHeaders = customHeaders
self.networkService = networkService
self.dataCollectorInterval = dataFlushInterval
self.exporterMetadata = exporterMetadata
Expand Down
2 changes: 1 addition & 1 deletion Sources/GOFeatureFlag/provider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public final class GoFeatureFlagProvider: FeatureProvider {
networkService = netSer
}

var headers: [String:String] = [:]
var headers: [String:String] = options.customHeaders ?? [:]
if let apiKey = options.apiKey {
headers["Authorization"] = "Bearer \(apiKey)"
}
Expand Down
25 changes: 25 additions & 0 deletions Tests/GOFeatureFlagTests/goff_api_tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,29 @@ class GoffApiTests: XCTestCase {
XCTFail("Expected GoFeatureFlagError.noEventToSend but got a different error: \(error).")
}
}

func testShouldSendCustomHeaders() async throws{
let mockService = MockNetworkingService(mockStatus: 200)
let options = GeFeatureFlagProviderOptions(
endpoint: "http://localhost:1031/",
customHeaders: ["X-Custom-Headers": "custom-value"]
)
let goffAPI = GoFeatureFlagAPI(networkingService: mockService, options: options)

let events: [FeatureEvent] = [
FeatureEvent(kind: "feature", userKey: "981f2662-1fb4-4732-ac6d-8399d9205aa9", creationDate: Int64(Date().timeIntervalSince1970),
key: "flag-1", variation: "enabled", value: JSONValue.bool(true), default: false, version: nil, source: "PROVIDER_CACHE")
]

do {
_ = try await goffAPI.postDataCollector(events: events)
guard let request = mockService.requests.last else {
XCTFail("No request captured")
return
}
XCTAssertEqual("custom-value", request.allHTTPHeaderFields?["X-Custom-Header"])
} catch {
XCTFail("exception thrown when doing the evaluation: \(error)")
}
}
}
29 changes: 29 additions & 0 deletions Tests/GOFeatureFlagTests/provider_tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,33 @@ class GoFeatureFlagProviderTests: XCTestCase {
XCTAssertEqual(2, mockNetworkService.dataCollectorCallCounter)
XCTAssertEqual(6, mockNetworkService.dataCollectorEventCounter)
}

func testProviderCustomHeaders() async {
let mockNetworkService = MockNetworkingService(mockStatus: 200)
let provider = GoFeatureFlagProvider(
options: GoFeatureFlagProviderOptions(
endpoint: "https://localhost:1031",
apiKey: "apiKey1"
customHeaders: [
"X-Custom-Header": "custom-value",
"Authorization": "Bearer custom" // should be overwritten by apiKey
]
networkService: mockNetworkService
)
)
let evaluationCtx = MutableContext(targetingKey: "ede04e44-463d-40d1-8fc0-b1d6855578d0")
let api = OpenFeatureAPI()
await api.setProviderAndWait(provider: provider, initialContext: evaluationCtx)
let client = api.getClient()

_ = client.getBooleanDetails(key: "my-flag", defaultValue: false)

guard let request = mockNetworkService.requests.last else {
XCTFail("No request captured")
return
}

XCTAssertEqual("custom-value", request.allHTTPHeaderFields?["X-Custom-Header"])
XCTAssertEqual("Bearer apiKey1", request.allHTTPHeaderFields?["Authorization"])
}
}