Skip to content

Commit 49f8e09

Browse files
author
Alexis Oyama
committed
fix(network): Handle http response from the server correctly
If the server responds correctly but without a success code (200), it did not call the error handler. This commit fixes this.
1 parent 42cdfb0 commit 49f8e09

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

Example/Tests/Classes/EventDataManagerTest.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,34 @@ - (void)test_request_synchronous
200200
[OHHTTPStubs removeAllStubs];
201201
}
202202

203+
- (void)test_response_code
204+
{
205+
[LeanplumHelper setup_method_swizzling];
206+
[LeanplumHelper start_production_test];
207+
[LPEventDataManager deleteEventsWithLimit:10000];
208+
[LPNetworkEngine setupValidateOperation];
209+
[Leanplum_Reachability online:YES];
210+
211+
// Simulate error from http response code.
212+
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
213+
return [request.URL.host isEqualToString:API_HOST];;
214+
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
215+
NSData *data = [@"Fail" dataUsingEncoding:NSUTF8StringEncoding];
216+
return [OHHTTPStubsResponse responseWithData:data statusCode:500 headers:nil];
217+
}];
218+
219+
NSArray *events = [LPEventDataManager eventsWithLimit:10000];
220+
XCTAssertTrue(events.count == 0);
221+
222+
[[LeanplumRequest post:@"sample3" params:nil] sendNow:YES];
223+
[NSThread sleepForTimeInterval:0.2];
224+
events = [LPEventDataManager eventsWithLimit:10000];
225+
XCTAssertTrue(events.count == 1);
226+
227+
[LPEventDataManager deleteEventsWithLimit:10000];
228+
[OHHTTPStubs removeAllStubs];
229+
}
230+
203231
- (void)test_uuid
204232
{
205233
[LeanplumHelper setup_method_swizzling];

Leanplum-SDK/Classes/LPNetworkOperation.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,18 @@ - (void)runSynchronously:(BOOL)synchronous
165165
if (synchronous) {
166166
dispatch_semaphore_signal(sem);
167167
}
168+
169+
// Handle unsuccessful http response code.
170+
NSError *responseError = error;
171+
if (!responseError && [self HTTPStatusCode] != 200) {
172+
responseError = [NSError errorWithDomain:NSURLErrorDomain
173+
code:[self HTTPStatusCode]
174+
userInfo:nil];
175+
}
168176

169-
if (error) {
177+
if (responseError) {
170178
if (self.errorBlock) {
171-
self.errorBlock(self, error);
179+
self.errorBlock(self, responseError);
172180
}
173181
} else {
174182
if (self.responseBlock) {

0 commit comments

Comments
 (0)