Skip to content

Commit

Permalink
Merge pull request #46 from launchdarkly/cm-stub-darkly-requests2
Browse files Browse the repository at this point in the history
Stub darkly requests
  • Loading branch information
jeff-byrnesinnovation committed Mar 29, 2016
2 parents 22c4963 + de84d11 commit 2b21a9e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 26 deletions.
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

0 comments on commit 2b21a9e

Please sign in to comment.