From 1d057ea4e3ac21f55cdb8e5afcde273ec6311c54 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Wed, 25 Sep 2024 15:07:37 +0530 Subject: [PATCH] Early access header support and testcases --- .../xcschemes/Contentstack iOS.xcscheme | 2 +- Sources/ContentstackConfig.swift | 18 ++++++ Tests/ContentstackConfigTest.swift | 59 ++++++++++++++++++- 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/Contentstack.xcworkspace/xcshareddata/xcschemes/Contentstack iOS.xcscheme b/Contentstack.xcworkspace/xcshareddata/xcschemes/Contentstack iOS.xcscheme index 82c0c141..1b99140d 100644 --- a/Contentstack.xcworkspace/xcshareddata/xcschemes/Contentstack iOS.xcscheme +++ b/Contentstack.xcworkspace/xcshareddata/xcschemes/Contentstack iOS.xcscheme @@ -1,6 +1,6 @@ String? { @@ -111,4 +115,18 @@ public struct ContentstackConfig { return appBundleId + "/" + versionNumberString } + + /// Internal method to get headers including early access + internal func getHeaders() -> [String: String] { + var headers: [String: String] = [ + "X-User-Agent": sdkVersionString(), + "User-Agent": userAgentString() + ] + + if let earlyAccess = earlyAccess, !earlyAccess.isEmpty { + headers["x-header-ea"] = earlyAccess.joined(separator: ",") + } + + return headers + } } diff --git a/Tests/ContentstackConfigTest.swift b/Tests/ContentstackConfigTest.swift index 5c76e9ad..54c859eb 100644 --- a/Tests/ContentstackConfigTest.swift +++ b/Tests/ContentstackConfigTest.swift @@ -87,10 +87,67 @@ class ContentstackConfigTest: XCTestCase { let stack = makeStackSut(config: config) XCTAssertEqual(stack.jsonDecoder.userInfo[.timeZoneContextKey] as? TimeZone, timeZone) } + func testEarlyAccessMultipleValues() { + var config = ContentstackConfig() + let earlyAccess : [String] = ["Taxonomy","Teams"] + config.setEarlyAccess(earlyAccess) + _ = makeStackSut(config: config) + let headers = config.getHeaders() + XCTAssertTrue(headers.keys.contains("x-header-ea")) + XCTAssertEqual(headers["x-header-ea"], "Taxonomy,Teams") + } + + func testDefaultEarlyAccessIsNil() { + var config = ContentstackConfig() + config.setEarlyAccess([]) + _ = makeStackSut(config: config) + let headers = config.getHeaders() + print("headers::",headers) + XCTAssertFalse(headers.keys.contains("x-header-ea"), "The headers should not contain the 'x-header-ea' key when early access is not set.") + } + + func testEarlyAccessSingleValue() { + var config = ContentstackConfig() + let earlyAccessFeatures = ["feature1"] + config.setEarlyAccess(earlyAccessFeatures) + _ = makeStackSut(config: config) + let headers = config.getHeaders() + XCTAssertTrue(headers.keys.contains("x-header-ea"), "The headers should contain the 'x-header-ea' key.") + XCTAssertEqual(headers["x-header-ea"], "feature1", "The 'x-header-ea' value should match the single early access value passed.") + } + + func testGetHeadersWithoutEarlyAccess() { + let config = ContentstackConfig() + let headers = config.getHeaders() + XCTAssertFalse(headers.keys.contains("x-header-ea")) + } + + func testMultipleEarlyAccessWithSpaces() { + var config = ContentstackConfig() + let earlyAccess: [String] = ["Feature One", "Feature Two"] + config.setEarlyAccess(earlyAccess) + _ = makeStackSut(config: config) + let headers = config.getHeaders() + XCTAssertTrue(headers.keys.contains("x-header-ea"), "The headers should contain the 'x-header-ea' key.") + XCTAssertEqual(headers["x-header-ea"], "Feature One,Feature Two", "The 'x-header-ea' value should match the early access values with spaces.") + } + + func testDefaultConfigHasNoEarlyAccessHeaders() { + let config = ContentstackConfig() + _ = makeStackSut(config: config) + let headers = config.getHeaders() + XCTAssertFalse(headers.keys.contains("x-header-ea"), "The default config should not contain the 'x-header-ea' key.") + } static var allTests = [ ("testUserAgent", testUserAgent), ("testXUserAgent", testXUserAgent), - ("testTimeZone_changetoCurrent", testTimeZone_changetoCurrent) + ("testTimeZone_changetoCurrent", testTimeZone_changetoCurrent), + ("testEarlyAccessMultipleValues", testEarlyAccessMultipleValues), + ("testDefaultEarlyAccessIsNil", testDefaultEarlyAccessIsNil), + ("testEarlyAccessSingleValue", testEarlyAccessSingleValue), + ("testGetHeadersWithoutEarlyAccess", testGetHeadersWithoutEarlyAccess), + ("testMultipleEarlyAccessWithSpaces", testMultipleEarlyAccessWithSpaces), + ("testDefaultConfigHasNoEarlyAccessHeaders", testDefaultConfigHasNoEarlyAccessHeaders), ] }