Skip to content
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
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1100"
LastUpgradeVersion = "1540"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
18 changes: 18 additions & 0 deletions Sources/ContentstackConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public struct ContentstackConfig {

/// The singleton instance of 'ContentstackConfig'.
public static let `default` = ContentstackConfig()
public var earlyAccess: [String]?

/// Initialize 'ContentstackConfig' default values.
public init() {}
Expand Down Expand Up @@ -52,6 +53,9 @@ public struct ContentstackConfig {

return userAgentString
}
public mutating func setEarlyAccess(_ earlyAccess: [String]) {
self.earlyAccess = earlyAccess
}

// MARK: Private
private func platformVersionString() -> String? {
Expand Down Expand Up @@ -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
}
}
59 changes: 58 additions & 1 deletion Tests/ContentstackConfigTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
}