|
| 1 | +// |
| 2 | +// LPRequestSenderTest.m |
| 3 | +// Leanplum-SDK_Tests |
| 4 | +// |
| 5 | +// Created by Grace Gu on 10/01/18. |
| 6 | +// Copyright © 2018 Leanplum. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import <XCTest/XCTest.h> |
| 10 | +#import <OCMock/OCMock.h> |
| 11 | +#import <Leanplum/LPAPIConfig.h> |
| 12 | +#import <Leanplum/LPEventDataManager.h> |
| 13 | +#import <Leanplum/LPNetworkProtocol.h> |
| 14 | +#import <Leanplum/LPRequestSender.h> |
| 15 | +#import <Leanplum/LPRequestFactory.h> |
| 16 | +#import <Leanplum/LPRequest.h> |
| 17 | +#import <Leanplum/LPCountAggregator.h> |
| 18 | + |
| 19 | +@interface LPRequestSender(UnitTest) |
| 20 | + |
| 21 | +@property (nonatomic, strong) id<LPNetworkEngineProtocol> engine; |
| 22 | + |
| 23 | +- (void)sendNow:(id<LPRequesting>)request sync:(BOOL)sync; |
| 24 | +- (void)sendRequests:(BOOL)sync; |
| 25 | +- (NSOperationQueue *)sendNowQueue; |
| 26 | + |
| 27 | +@end |
| 28 | + |
| 29 | +@interface LPRequestSenderTest : XCTestCase |
| 30 | + |
| 31 | +@end |
| 32 | + |
| 33 | +@implementation LPRequestSenderTest |
| 34 | + |
| 35 | +- (void)setUp { |
| 36 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 37 | +} |
| 38 | + |
| 39 | +- (void)tearDown { |
| 40 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 41 | +} |
| 42 | + |
| 43 | +- (void)testCreateArgsDictionaryForRequest { |
| 44 | + LPRequest *request = [LPRequest post:@"test" params:@{}]; |
| 45 | + LPRequestSender *requestSender = [[LPRequestSender alloc] init]; |
| 46 | + LPConstantsState *constants = [LPConstantsState sharedState]; |
| 47 | + NSString *timestamp = [NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]]; |
| 48 | + NSMutableDictionary *testArgs = [@{ |
| 49 | + LP_PARAM_ACTION: @"test", |
| 50 | + LP_PARAM_DEVICE_ID: @"", |
| 51 | + LP_PARAM_USER_ID: @"", |
| 52 | + LP_PARAM_SDK_VERSION: constants.sdkVersion, |
| 53 | + LP_PARAM_CLIENT: constants.client, |
| 54 | + LP_PARAM_DEV_MODE: @(constants.isDevelopmentModeEnabled), |
| 55 | + LP_PARAM_TIME: timestamp, |
| 56 | + LP_PARAM_UUID: @"uuid", |
| 57 | + } mutableCopy]; |
| 58 | + NSMutableDictionary *args = [requestSender createArgsDictionaryForRequest:request]; |
| 59 | + args[LP_PARAM_UUID] = @"uuid"; |
| 60 | + args[LP_PARAM_TIME] = timestamp; |
| 61 | + |
| 62 | + XCTAssertEqualObjects(testArgs, args); |
| 63 | +} |
| 64 | + |
| 65 | +- (void)testSendNow { |
| 66 | + LPRequest *request = [LPRequest post:@"test" params:@{}]; |
| 67 | + LPRequestSender *requestSender = [[LPRequestSender alloc] init]; |
| 68 | + id requestSenderMock = OCMPartialMock(requestSender); |
| 69 | + id configMock = OCMClassMock([LPAPIConfig class]); |
| 70 | + OCMStub([configMock sharedConfig]).andReturn(configMock); |
| 71 | + OCMStub([configMock appId]).andReturn(@"appID"); |
| 72 | + OCMStub([configMock accessKey]).andReturn(@"accessKey"); |
| 73 | + [requestSender sendNow:request]; |
| 74 | + |
| 75 | + OCMVerify([requestSenderMock sendNow:request sync:false]); |
| 76 | + OCMVerify([requestSenderMock sendEventually:request]); |
| 77 | + OCMVerify([requestSenderMock sendRequests:false]); |
| 78 | + |
| 79 | + [requestSenderMock stopMocking]; |
| 80 | + [configMock stopMocking]; |
| 81 | +} |
| 82 | + |
| 83 | +- (void)testSendEventually { |
| 84 | + LPRequest *request = [LPRequest post:@"test" params:@{}]; |
| 85 | + LPRequestSender *requestSender = [[LPRequestSender alloc] init]; |
| 86 | + id eventDataManagerMock = OCMClassMock([LPEventDataManager class]); |
| 87 | + [requestSender sendEventually:request]; |
| 88 | + |
| 89 | + OCMVerify([eventDataManagerMock addEvent:[OCMArg isNotNil]]); |
| 90 | +} |
| 91 | + |
| 92 | +- (void)testSendIfConnected { |
| 93 | + LPRequest *request = [LPRequest post:@"test" params:@{}]; |
| 94 | + LPRequestSender *requestSender = [[LPRequestSender alloc] init]; |
| 95 | + id requestSenderMock = OCMPartialMock(requestSender); |
| 96 | + id reachabilityMock = OCMClassMock([Leanplum_Reachability class]); |
| 97 | + OCMStub([reachabilityMock reachabilityForInternetConnection]).andReturn(reachabilityMock); |
| 98 | + OCMStub([reachabilityMock isReachable]).andReturn(true); |
| 99 | + [requestSender sendIfConnected:request]; |
| 100 | + |
| 101 | + OCMVerify([requestSenderMock sendNow:request]); |
| 102 | +} |
| 103 | + |
| 104 | +- (void)testSendIfNotConnected { |
| 105 | + LPRequest *request = [LPRequest post:@"test" params:@{}]; |
| 106 | + LPRequestSender *requestSender = [[LPRequestSender alloc] init]; |
| 107 | + id requestSenderMock = OCMPartialMock(requestSender); |
| 108 | + id reachabilityMock = OCMClassMock([Leanplum_Reachability class]); |
| 109 | + OCMStub([reachabilityMock reachabilityForInternetConnection]).andReturn(reachabilityMock); |
| 110 | + OCMStub([reachabilityMock isReachable]).andReturn(false); |
| 111 | + [requestSender sendIfConnected:request]; |
| 112 | + |
| 113 | + OCMVerify([requestSenderMock sendEventually:request]); |
| 114 | + |
| 115 | + [requestSenderMock stopMocking]; |
| 116 | + [reachabilityMock stopMocking]; |
| 117 | +} |
| 118 | + |
| 119 | +- (void)testSendNowWithData { |
| 120 | + LPRequest *request = [LPRequest post:@"test" params:@{}]; |
| 121 | + LPRequestSender *requestSender = [[LPRequestSender alloc] init]; |
| 122 | + id requestSenderMock = OCMPartialMock(requestSender); |
| 123 | + |
| 124 | + NSMutableDictionary *data = [[NSMutableDictionary alloc] init]; |
| 125 | + data [@"key"] = [@"value" dataUsingEncoding:NSUTF8StringEncoding]; |
| 126 | + [requestSender sendNow:request withData:data[@"key"] forKey:@"key"]; |
| 127 | + |
| 128 | + OCMVerify([requestSenderMock sendNow:request withDatas:data]); |
| 129 | +} |
| 130 | + |
| 131 | +- (void)testSendNowWithDatas { |
| 132 | + LPRequest *request = [LPRequest post:@"test" params:@{}]; |
| 133 | + LPRequestSender *requestSender = [[LPRequestSender alloc] init]; |
| 134 | + requestSender.engine = OCMProtocolMock(@protocol(LPNetworkEngineProtocol)); |
| 135 | + id opMock = OCMProtocolMock(@protocol(LPNetworkOperationProtocol)); |
| 136 | + OCMStub([requestSender.engine operationWithPath:[OCMArg any] params:[OCMArg any] httpMethod:[OCMArg any] ssl:[OCMArg any] timeoutSeconds:60]).andReturn(opMock); |
| 137 | + |
| 138 | + NSMutableDictionary *datas = [[NSMutableDictionary alloc] init]; |
| 139 | + datas[@"key"] = [@"value" dataUsingEncoding:NSUTF8StringEncoding]; |
| 140 | + [requestSender sendNow:request withDatas:datas]; |
| 141 | + |
| 142 | + OCMVerify([requestSender.engine enqueueOperation:opMock]); |
| 143 | + OCMVerify([opMock addData:datas[@"key"] forKey:@"key"]); |
| 144 | + OCMVerify([opMock addCompletionHandler:[OCMArg any] errorHandler:[OCMArg any]]); |
| 145 | +} |
| 146 | + |
| 147 | +- (void) testSendRequestsSync { |
| 148 | + LPRequestSender *requestSender = [[LPRequestSender alloc] init]; |
| 149 | + id requestOperationMock = OCMClassMock([NSBlockOperation class]); |
| 150 | + OCMStub([NSBlockOperation new]).andReturn(requestOperationMock); |
| 151 | + id countAggregatorMock = OCMClassMock([LPCountAggregator class]); |
| 152 | + OCMStub([countAggregatorMock sharedAggregator]).andReturn(countAggregatorMock); |
| 153 | + id eventDataManagerMock = OCMClassMock([LPEventDataManager class]); |
| 154 | + |
| 155 | + NSMutableArray *requestsToSend = [[NSMutableArray alloc] init]; |
| 156 | + [requestsToSend addObject:[[NSMutableDictionary alloc] init]]; |
| 157 | + |
| 158 | + OCMStub([eventDataManagerMock eventsWithLimit:MAX_EVENTS_PER_API_CALL]).andReturn(requestsToSend); |
| 159 | + requestSender.engine = OCMProtocolMock(@protocol(LPNetworkEngineProtocol)); |
| 160 | + |
| 161 | + LPConstantsState *constants = [LPConstantsState sharedState]; |
| 162 | + int timeout = 5 * constants.syncNetworkTimeoutSeconds; |
| 163 | + |
| 164 | + id opMock = OCMProtocolMock(@protocol(LPNetworkOperationProtocol)); |
| 165 | + OCMStub([requestSender.engine operationWithPath:[OCMArg any] params:[OCMArg any] httpMethod:[OCMArg any] ssl:[OCMArg any] timeoutSeconds:timeout]).andReturn(opMock); |
| 166 | + [requestSender sendRequests:true]; |
| 167 | + |
| 168 | + OCMVerify([countAggregatorMock sendAllCounts]); |
| 169 | + OCMVerify([eventDataManagerMock eventsWithLimit:MAX_EVENTS_PER_API_CALL]); |
| 170 | + OCMVerify([requestSender.engine operationWithPath:[OCMArg any] params:[OCMArg any] httpMethod:[OCMArg any] ssl:[OCMArg any] timeoutSeconds:timeout]); |
| 171 | + OCMVerify([opMock addCompletionHandler:[OCMArg any] errorHandler:[OCMArg any]]); |
| 172 | + OCMVerify([requestSender.engine enqueueOperation:opMock]); |
| 173 | + [countAggregatorMock stopMocking]; |
| 174 | +} |
| 175 | + |
| 176 | +- (void) testSendRequestsAsync { |
| 177 | + LPRequestSender *requestSender = OCMPartialMock([[LPRequestSender alloc] init]); |
| 178 | + id requestOperationMock = OCMClassMock([NSBlockOperation class]); |
| 179 | + OCMStub([requestOperationMock new]).andReturn(requestOperationMock); |
| 180 | + id mockQueue = OCMClassMock([NSOperationQueue class]); |
| 181 | + OCMStub([requestSender sendNowQueue]).andReturn(mockQueue); |
| 182 | + [requestSender sendRequests:false]; |
| 183 | + |
| 184 | + OCMVerify([requestOperationMock addExecutionBlock:[OCMArg any]]); |
| 185 | + OCMVerify([mockQueue addOperation:requestOperationMock]); |
| 186 | +} |
| 187 | +@end |
0 commit comments