Skip to content

Commit 94fc027

Browse files
authored
Merge pull request #53 from contentstack/feat/DX-1228-Early-access-support
feat: early access header support added
2 parents e9bc305 + 734b8b4 commit 94fc027

File tree

5 files changed

+98
-1
lines changed

5 files changed

+98
-1
lines changed

Contentstack/Config.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,27 @@
8888
*/
8989
@property (nullable, retain) id<CSURLSessionDelegate> delegate;
9090

91+
92+
93+
/**
94+
Early access features
95+
96+
//Obj-C
97+
Config *config = [[Config alloc] init];
98+
[config setEarlyAccess:@[@"Taxonomy", @"Teams", @"Terms", @"LivePreview"]];
99+
100+
//Swift
101+
let config = Config()
102+
config.setEarlyAccess(["Taxonomy", "Teams", "Terms", "LivePreview"])
103+
104+
*/
105+
@property (nonatomic, strong, nullable) NSArray<NSString *> *setEarlyAccess;
106+
107+
108+
/**
109+
Set early access features
110+
111+
@param setearlyAccess An array of early access feature names
112+
*/
113+
- (NSDictionary<NSString *, NSString *> *)earlyAccessHeaders;
91114
@end

Contentstack/Config.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ -(instancetype)init {
1515
if (self) {
1616
_region = US;
1717
_host = @"cdn.contentstack.io";
18-
_version = kCSIO_ApiVersion;
18+
_version = kCSIO_ApiVersion;
19+
_setEarlyAccess = nil;
1920
}
2021
return self;
2122
}
@@ -25,4 +26,11 @@ - (void)setRegion:(ContentstackRegion)region {
2526
_host = [self hostURL:_region];
2627
}
2728
}
29+
- (NSDictionary<NSString *, NSString *> *)earlyAccessHeaders {
30+
if (_setEarlyAccess.count > 0) {
31+
NSString *earlyAccessString = [_setEarlyAccess componentsJoinedByString:@","];
32+
return @{@"x-header-ea": earlyAccessString};
33+
}
34+
return @{};
35+
}
2836
@end

Contentstack/Stack.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ BUILT_ASSUME_NONNULL_BEGIN
6868

6969
- (Taxonomy *)taxonomy;
7070

71+
- (NSDictionary *)getHeaders;
72+
7173
//MARK: - Manually set headers
7274
/**---------------------------------------------------------------------------------------
7375
* @name Manually set headers

Contentstack/Stack.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ - (instancetype)initWithAPIKey:(NSString*)apiKey andaccessToken:(NSString *)acce
4747
_commonDateFormatter.includeTime = YES;
4848

4949
_requestOperationSet = [NSMutableSet set];
50+
// Add early access headers only if they exist
51+
NSDictionary *earlyAccessHeaders = [_config earlyAccessHeaders];
52+
if (earlyAccessHeaders.count > 0) {
53+
[_stackHeaders addEntriesFromDictionary:earlyAccessHeaders];
54+
}
5055

5156

5257
[self setHeader:_apiKey forKey:kCSIO_SiteApiKey];
@@ -128,6 +133,10 @@ - (void)removeHeaderForKey:(NSString *)headerKey {
128133
}
129134
}
130135

136+
- (NSDictionary *)getHeaders {
137+
return [self.stackHeaders copy];
138+
}
139+
131140
- (NSString *)imageTransformWithUrl:(NSString *)url andParams:(NSDictionary<NSString *, id> *)params{
132141
if([url rangeOfString:@"?" options:NSCaseInsensitiveSearch].length==0) {
133142
url = [url stringByAppendingString:@"?"];

ContentstackTest/ContentstackTest.m

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,61 @@ - (void)tearDown {
119119
#pragma mark Test Case - Header
120120

121121

122+
- (void)testStackHeadersEarlyAccess {
123+
XCTestExpectation *expectation = [self expectationWithDescription:@"EarlyAccessHeadersPassed"];
124+
config = [[Config alloc] init];
125+
config.setEarlyAccess = @[@"Taxonomy", @"Teams", @"Terms", @"LivePreview"];
126+
csStack = [Contentstack stackWithAPIKey:@"apikey" accessToken:@"delivery_token" environmentName:@"environment" config:config];
127+
// Check the headers in the stack
128+
NSDictionary *headers = [csStack getHeaders];
129+
// Check if the early access headers are set correctly
130+
NSString *expectedHeaderValue = @"Taxonomy,Teams,Terms,LivePreview";
131+
NSString *earlyAccessHeader = headers[@"x-header-ea"];
132+
133+
XCTAssertNotNil(earlyAccessHeader, @"Early access header should be present");
134+
XCTAssertEqualObjects(earlyAccessHeader, expectedHeaderValue, @"Early access header should match the expected value");
135+
136+
// Fulfill the expectation to mark the test as completed
137+
[expectation fulfill];
138+
139+
// Wait for the request to complete
140+
[self waitForExpectationsWithTimeout:kRequestTimeOutInSeconds handler:^(NSError *error) {
141+
if (error) {
142+
XCTFail(@"Test timed out: %@", error.localizedDescription);
143+
}
144+
}];
145+
}
146+
147+
- (void)testNoEarlyAccessHeaders {
148+
XCTestExpectation *expectation = [self expectationWithDescription:@"NoEarlyAccessHeaders"];
149+
config = [[Config alloc] init];
150+
csStack = [Contentstack stackWithAPIKey:@"apikey" accessToken:@"delivery_token" environmentName:@"environment" config:config];
151+
152+
NSDictionary *headers = [csStack getHeaders];
153+
NSString *earlyAccessHeader = headers[@"x-header-ea"];
154+
XCTAssertNil(earlyAccessHeader, @"Early access header should not be present when no early access features are set");
155+
156+
[expectation fulfill];
157+
[self waitForExpectationsWithTimeout:kRequestTimeOutInSeconds handler:nil];
158+
}
159+
160+
- (void)testSingleEarlyAccessHeader {
161+
XCTestExpectation *expectation = [self expectationWithDescription:@"SingleEarlyAccessHeader"];
162+
config = [[Config alloc] init];
163+
config.setEarlyAccess = @[@"LivePreview"];
164+
csStack = [Contentstack stackWithAPIKey:@"apikey" accessToken:@"delivery_token" environmentName:@"environment" config:config];
165+
166+
NSDictionary *headers = [csStack getHeaders];
167+
NSString *expectedHeaderValue = @"LivePreview";
168+
NSString *earlyAccessHeader = headers[@"x-header-ea"];
169+
XCTAssertNotNil(earlyAccessHeader, @"Early access header should be present");
170+
XCTAssertEqualObjects(earlyAccessHeader, expectedHeaderValue, @"Single early access header should match the expected value");
171+
172+
[expectation fulfill];
173+
[self waitForExpectationsWithTimeout:kRequestTimeOutInSeconds handler:nil];
174+
}
175+
176+
122177
- (void)test01FetchSourceEntries {
123178
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch All Entries"];
124179

0 commit comments

Comments
 (0)