Skip to content

Commit c295991

Browse files
authored
Fix Networking to receive data for POST requests (#3940)
* Fix Networking to receive data for POST requests Currently, GULNetworking doesn't receive the data when sending a POST request. Tracked internally at b/141712804 * Added unit tests. * Update to handle the small data increments.
1 parent 1044299 commit c295991

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

GoogleUtilities/Example/Tests/Network/GULNetworkTest.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ - (void)testSessionNetwork_POST_foreground {
130130
queue:_backgroundQueue
131131
usingBackgroundSession:NO
132132
completionHandler:^(NSHTTPURLResponse *response, NSData *data, NSError *error) {
133+
XCTAssertNotNil(data);
134+
NSString *responseBody = [[NSString alloc] initWithData:data
135+
encoding:NSUTF8StringEncoding];
136+
XCTAssertEqualObjects(responseBody, @"<html><body>Hello, World!</body></html>");
133137
[self verifyResponse:response error:error];
134138
[self verifyRequest];
135139
XCTAssertFalse(self->_network.hasUploadInProgress, "There must be no pending request");
@@ -317,6 +321,10 @@ - (void)testSessionNetwork_POST_background {
317321
queue:_backgroundQueue
318322
usingBackgroundSession:YES
319323
completionHandler:^(NSHTTPURLResponse *response, NSData *data, NSError *error) {
324+
XCTAssertNotNil(data);
325+
NSString *responseBody = [[NSString alloc] initWithData:data
326+
encoding:NSUTF8StringEncoding];
327+
XCTAssertEqualObjects(responseBody, @"<html><body>Hello, World!</body></html>");
320328
[self verifyResponse:response error:error];
321329
[self verifyRequest];
322330
XCTAssertFalse(self->_network.hasUploadInProgress, "There must be no pending request");

GoogleUtilities/Network/GULNetworkURLSession.m

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
#import "Private/GULNetworkMessageCode.h"
2323

2424
@interface GULNetworkURLSession () <NSURLSessionDelegate,
25-
NSURLSessionTaskDelegate,
26-
NSURLSessionDownloadDelegate>
25+
NSURLSessionDataDelegate,
26+
NSURLSessionDownloadDelegate,
27+
NSURLSessionTaskDelegate>
2728
@end
2829

2930
@implementation GULNetworkURLSession {
@@ -221,6 +222,24 @@ - (nullable NSString *)sessionIDFromAsyncGETRequest:(NSURLRequest *)request
221222
return _sessionID;
222223
}
223224

225+
#pragma mark - NSURLSessionDataDelegate
226+
227+
/// Called by the NSURLSession when the data task has received some of the expected data.
228+
/// Once the session is completed, URLSession:task:didCompleteWithError will be called and the
229+
/// completion handler will be called with the downloaded data.
230+
- (void)URLSession:(NSURLSession *)session
231+
dataTask:(NSURLSessionDataTask *)dataTask
232+
didReceiveData:(NSData *)data {
233+
@synchronized(self) {
234+
NSMutableData *mutableData = [[NSMutableData alloc] init];
235+
if (_downloadedData) {
236+
mutableData = _downloadedData.mutableCopy;
237+
}
238+
[mutableData appendData:data];
239+
_downloadedData = mutableData;
240+
}
241+
}
242+
224243
#pragma mark - NSURLSessionTaskDelegate
225244

226245
/// Called by the NSURLSession once the download task is completed. The file is saved in the

0 commit comments

Comments
 (0)