Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stub darkly requests #46

Merged
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
4 changes: 2 additions & 2 deletions Darkly/LDRequestManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ - (void)dealloc
-(void)performFeatureFlagRequest:(NSString *)encodedUser
{
DEBUG_LOGX(@"RequestManager syncing config to server");
NSMutableURLRequest *request = [self featuresWithEncodedUserRequest:encodedUser];

if (!configRequestInProgress) {
if (apiKey) {
if (encodedUser) {
configRequestInProgress = YES;
NSMutableURLRequest *request = [self featuresWithEncodedUserRequest:encodedUser];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];

Expand Down Expand Up @@ -135,12 +135,12 @@ -(void)performFeatureFlagRequest:(NSString *)encodedUser
-(void)performEventRequest:(NSData *)jsonEventArray
{
DEBUG_LOGX(@"RequestManager syncing events to server");
NSMutableURLRequest *request = [self eventsRequestWithJsonEvents:jsonEventArray];

if (!eventRequestInProgress) {
if (apiKey) {
if (jsonEventArray) {
eventRequestInProgress = YES;
NSMutableURLRequest *request = [self eventsRequestWithJsonEvents:jsonEventArray];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];

Expand Down
3 changes: 3 additions & 0 deletions DarklyTests/DarklyXCTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

#import "DarklyXCTestCase.h"
#import "LDDataManager.h"
#import <OHPathHelpers.h>
#import <OCMock.h>

@implementation DarklyXCTestCase

- (void)setUp {
[super setUp];

[[NSUserDefaults standardUserDefaults] removeObjectForKey:kUserDictionaryStorageKey];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kEventDictionaryStorageKey];
}
Expand Down
95 changes: 71 additions & 24 deletions DarklyTests/LDRequestManagerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,112 @@
#import "DarklyXCTestCase.h"
#import "LDRequestManager.h"
#import "LDDataManager.h"

#import "LDClientManager.h"
#import "LDUserBuilder.h"
#import "LDConfig.h"
#import "LDClient.h"
#import <OCMock.h>
#import <OHHTTPStubs/OHHTTPStubs.h>

@interface LDRequestManagerTest : DarklyXCTestCase
@property (nonatomic) id clientManagerMock;
@property (nonatomic) id ldClientMock;
@property (nonatomic) int tempFlushInterval;
@end

@implementation LDRequestManagerTest
@synthesize clientManagerMock;
@synthesize ldClientMock;
@synthesize tempFlushInterval;

- (void)setUp {
[super setUp];

// Put setup code here. This method is called before the invocation of each test method in the class.
LDUserBuilder *userBuilder = [[LDUserBuilder alloc] init];
userBuilder = [userBuilder withKey:@"jeff@test.com"];
LDUserModel *user = [userBuilder build];

LDConfigBuilder *configBuilder = [[LDConfigBuilder alloc] init];
tempFlushInterval = 30;
configBuilder = [configBuilder withFlushInterval:tempFlushInterval];
LDConfig *config = [configBuilder build];

ldClientMock = OCMClassMock([LDClient class]);
OCMStub(ClassMethod([ldClientMock sharedInstance])).andReturn(ldClientMock);
OCMStub([ldClientMock ldUser]).andReturn(user);
OCMStub([ldClientMock ldConfig]).andReturn(config);

clientManagerMock = OCMClassMock([LDClientManager class]);
OCMStub(ClassMethod([clientManagerMock sharedInstance])).andReturn(clientManagerMock);
}

- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}

- (void)testFeatureFlagRequest {
- (void)testFeatureFlagRequestMakesHttpRequestWithApiKey {

XCTestExpectation* responseArrived = [self expectationWithDescription:@"response of async request has arrived"];
__block BOOL httpRequestAttempted = NO;
NSData *data = [[NSData alloc] initWithBase64EncodedString:@"" options: 0] ;

[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.host isEqualToString:@"app.launchdarkly.com"];
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
httpRequestAttempted = YES;
[responseArrived fulfill];
return [OHHTTPStubsResponse responseWithData: data statusCode:200 headers:@{@"Content-Type":@"application/json"}];
}];

NSString *apiKey = @"YOUR_MOBILE_KEY";
NSString *encodedUserString = @"eyJrZXkiOiAiamVmZkB0ZXN0LmNvbSJ9";
LDRequestManager *requestManager = [LDRequestManager sharedInstance];
[requestManager setApiKey:apiKey];
[requestManager setBaseUrl:kBaseUrl];
[requestManager setConnectionTimeout:10];

BOOL requestInProgress = YES;
[requestManager performFeatureFlagRequest:encodedUserString];

NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:1.0];

while (requestInProgress == YES && ([timeoutDate timeIntervalSinceNow] > 0)) {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01, YES);
requestInProgress = [requestManager configRequestInProgress];
}
XCTAssertFalse(requestInProgress);

[self waitForExpectationsWithTimeout:10 handler:^(NSError *error){
// By the time we reach this code, the while loop has exited
// so the response has arrived or the test has timed out
XCTAssertTrue(httpRequestAttempted);
[OHHTTPStubs removeAllStubs];
}];
}

- (void)testEventRequest {
- (void)testEventRequestMakesHttpRequestWithApiKey {

NSString *apiKey = @"YOUR_MOBILE_KEY";
XCTestExpectation* responseArrived = [self expectationWithDescription:@"response of async request has arrived"];
__block BOOL httpRequestAttempted = NO;
NSData *data = [[NSData alloc] initWithBase64EncodedString:@"" options: 0] ;

[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.host isEqualToString:@"app.launchdarkly.com"];
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
httpRequestAttempted = YES;
[responseArrived fulfill];
return [OHHTTPStubsResponse responseWithData: data statusCode:200 headers:@{@"Content-Type":@"application/json"}];
}];

NSString *apiKey = @"YOUR_MOBILE_KEY";
NSString *jsonEventString = @"[{\"kind\": \"feature\", \"user\": {\"key\" : \"jeff@test.com\", \"custom\" : {\"groups\" : [\"microsoft\", \"google\"]}}, \"creationDate\": 1438468068, \"key\": \"isConnected\", \"value\": true, \"default\": false}]";
NSData* eventData = [jsonEventString dataUsingEncoding:NSUTF8StringEncoding];

LDRequestManager *requestManager = [LDRequestManager sharedInstance];
[requestManager setApiKey:apiKey];
[requestManager setBaseUrl:kBaseUrl];
[requestManager setConnectionTimeout:10];

BOOL requestInProgress = YES;
[requestManager performEventRequest:eventData];

NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:1.0];

while (requestInProgress == YES && ([timeoutDate timeIntervalSinceNow] > 0)) {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01, YES);
requestInProgress = [requestManager eventRequestInProgress];
}
XCTAssertFalse(requestInProgress);


[self waitForExpectationsWithTimeout:10 handler:^(NSError *error){
// By the time we reach this code, the while loop has exited
// so the response has arrived or the test has timed out
XCTAssertTrue(httpRequestAttempted);
[OHHTTPStubs removeAllStubs];
}];
}

@end