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
23 changes: 23 additions & 0 deletions Contentstack/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,27 @@
*/
@property (nullable, retain) id<CSURLSessionDelegate> delegate;



/**
Early access features

//Obj-C
Config *config = [[Config alloc] init];
[config setEarlyAccess:@[@"Taxonomy", @"Teams", @"Terms", @"LivePreview"]];

//Swift
let config = Config()
config.setEarlyAccess(["Taxonomy", "Teams", "Terms", "LivePreview"])

*/
@property (nonatomic, strong, nullable) NSArray<NSString *> *setEarlyAccess;


/**
Set early access features

@param setearlyAccess An array of early access feature names
*/
- (NSDictionary<NSString *, NSString *> *)earlyAccessHeaders;
@end
10 changes: 9 additions & 1 deletion Contentstack/Config.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ -(instancetype)init {
if (self) {
_region = US;
_host = @"cdn.contentstack.io";
_version = kCSIO_ApiVersion;
_version = kCSIO_ApiVersion;
_setEarlyAccess = nil;
}
return self;
}
Expand All @@ -25,4 +26,11 @@ - (void)setRegion:(ContentstackRegion)region {
_host = [self hostURL:_region];
}
}
- (NSDictionary<NSString *, NSString *> *)earlyAccessHeaders {
if (_setEarlyAccess.count > 0) {
NSString *earlyAccessString = [_setEarlyAccess componentsJoinedByString:@","];
return @{@"x-header-ea": earlyAccessString};
}
return @{};
}
@end
2 changes: 2 additions & 0 deletions Contentstack/Stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ BUILT_ASSUME_NONNULL_BEGIN

- (Taxonomy *)taxonomy;

- (NSDictionary *)getHeaders;

//MARK: - Manually set headers
/**---------------------------------------------------------------------------------------
* @name Manually set headers
Expand Down
9 changes: 9 additions & 0 deletions Contentstack/Stack.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ - (instancetype)initWithAPIKey:(NSString*)apiKey andaccessToken:(NSString *)acce
_commonDateFormatter.includeTime = YES;

_requestOperationSet = [NSMutableSet set];
// Add early access headers only if they exist
NSDictionary *earlyAccessHeaders = [_config earlyAccessHeaders];
if (earlyAccessHeaders.count > 0) {
[_stackHeaders addEntriesFromDictionary:earlyAccessHeaders];
}


[self setHeader:_apiKey forKey:kCSIO_SiteApiKey];
Expand Down Expand Up @@ -128,6 +133,10 @@ - (void)removeHeaderForKey:(NSString *)headerKey {
}
}

- (NSDictionary *)getHeaders {
return [self.stackHeaders copy];
}

- (NSString *)imageTransformWithUrl:(NSString *)url andParams:(NSDictionary<NSString *, id> *)params{
if([url rangeOfString:@"?" options:NSCaseInsensitiveSearch].length==0) {
url = [url stringByAppendingString:@"?"];
Expand Down
55 changes: 55 additions & 0 deletions ContentstackTest/ContentstackTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,61 @@ - (void)tearDown {
#pragma mark Test Case - Header


- (void)testStackHeadersEarlyAccess {
XCTestExpectation *expectation = [self expectationWithDescription:@"EarlyAccessHeadersPassed"];
config = [[Config alloc] init];
config.setEarlyAccess = @[@"Taxonomy", @"Teams", @"Terms", @"LivePreview"];
csStack = [Contentstack stackWithAPIKey:@"apikey" accessToken:@"delivery_token" environmentName:@"environment" config:config];
// Check the headers in the stack
NSDictionary *headers = [csStack getHeaders];
// Check if the early access headers are set correctly
NSString *expectedHeaderValue = @"Taxonomy,Teams,Terms,LivePreview";
NSString *earlyAccessHeader = headers[@"x-header-ea"];

XCTAssertNotNil(earlyAccessHeader, @"Early access header should be present");
XCTAssertEqualObjects(earlyAccessHeader, expectedHeaderValue, @"Early access header should match the expected value");

// Fulfill the expectation to mark the test as completed
[expectation fulfill];

// Wait for the request to complete
[self waitForExpectationsWithTimeout:kRequestTimeOutInSeconds handler:^(NSError *error) {
if (error) {
XCTFail(@"Test timed out: %@", error.localizedDescription);
}
}];
}

- (void)testNoEarlyAccessHeaders {
XCTestExpectation *expectation = [self expectationWithDescription:@"NoEarlyAccessHeaders"];
config = [[Config alloc] init];
csStack = [Contentstack stackWithAPIKey:@"apikey" accessToken:@"delivery_token" environmentName:@"environment" config:config];

NSDictionary *headers = [csStack getHeaders];
NSString *earlyAccessHeader = headers[@"x-header-ea"];
XCTAssertNil(earlyAccessHeader, @"Early access header should not be present when no early access features are set");

[expectation fulfill];
[self waitForExpectationsWithTimeout:kRequestTimeOutInSeconds handler:nil];
}

- (void)testSingleEarlyAccessHeader {
XCTestExpectation *expectation = [self expectationWithDescription:@"SingleEarlyAccessHeader"];
config = [[Config alloc] init];
config.setEarlyAccess = @[@"LivePreview"];
csStack = [Contentstack stackWithAPIKey:@"apikey" accessToken:@"delivery_token" environmentName:@"environment" config:config];

NSDictionary *headers = [csStack getHeaders];
NSString *expectedHeaderValue = @"LivePreview";
NSString *earlyAccessHeader = headers[@"x-header-ea"];
XCTAssertNotNil(earlyAccessHeader, @"Early access header should be present");
XCTAssertEqualObjects(earlyAccessHeader, expectedHeaderValue, @"Single early access header should match the expected value");

[expectation fulfill];
[self waitForExpectationsWithTimeout:kRequestTimeOutInSeconds handler:nil];
}


- (void)test01FetchSourceEntries {
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch All Entries"];

Expand Down