Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c5fb684

Browse files
ms-jihuaRaj Seshasankaran
authored andcommitted
Check if NSURLProtocolClient has private callback before calling it (#2765)
* Check if NSURLProtocolClient has private callback before calling it Fixes #2764 * - Coalesce headers - Add test for a POST NSURLSessionTask
1 parent 2d430f1 commit c5fb684

File tree

5 files changed

+60
-26
lines changed

5 files changed

+60
-26
lines changed

Frameworks/Foundation/NSURLProtocol.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
#import <NSRaise.h>
2525
#import <LoggingNative.h>
2626

27-
#import <NSURLProtocolInternal.h>
2827
#import <NSURLRequestInternal.h>
28+
#import "NSURLProtocolInternal.h"
2929
#import "NSURLProtocol_file.h"
3030

3131
static const wchar_t* TAG = L"NSURLProtocol";

Frameworks/Foundation/NSURLProtocolInternal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
#pragma once
1717

1818
#import <Foundation/NSURLProtocol.h>
19+
#import <Starboard/SmartTypes.h>
20+
21+
@interface NSURLProtocol ()
22+
+ (id)_URLProtocolClassForRequest:(id)request;
23+
@end
1924

2025
// Internal extension for NSURLProtocolClient, to support POST
2126
@protocol _NSURLProtocolClientInternal <NSURLProtocolClient>

Frameworks/Foundation/NSURLProtocol_WinHTTP.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ - (void)startLoading {
288288
// TODO #2721: If this properly supports streaming bodies instead of flattening the data up front,
289289
// more accurate callbacks can be done
290290
NSInteger contentSize = self.request.HTTPBody ? self.request.HTTPBody.length : _flattenedBodyStream.size();
291-
if (contentSize > 0) {
291+
if (([self.client respondsToSelector:@selector(URLProtocol:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:)]) &&
292+
(contentSize > 0)) {
292293
__dispatchClientCallback(self, ^void() {
293294
[self.client URLProtocol:self
294295
didWriteData:contentSize

Frameworks/include/NSURLProtocolInternal.h

Lines changed: 0 additions & 24 deletions
This file was deleted.

tests/functionaltests/Tests/NSURLSession.mm

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,58 @@ - (void)URLSession:(NSURLSession*)session
614614
ASSERT_EQ_MSG(nil, taskError, "FAILED: Connection returned error!");
615615
}
616616

617+
/**
618+
* Test to verify a data task post request can be successfully made and a valid data is received with a completion handler
619+
*/
620+
TEST_METHOD(DataTaskWithPostRequest) {
621+
__block THBooleanCondition* condition = [[THBooleanCondition alloc] init];
622+
__block NSURLResponse* taskResponse;
623+
__block NSData* taskData;
624+
__block NSError* taskError;
625+
626+
NSURLSessionDataTaskTestHelper* dataTaskTestHelper = [[NSURLSessionDataTaskTestHelper alloc] init];
627+
NSURLSession* session = [dataTaskTestHelper createSession];
628+
NSURL* url = [NSURL URLWithString:@"https://httpbin.org/post"];
629+
LOG_INFO("Establishing data task with url %@", url);
630+
631+
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
632+
633+
static const std::string alphanumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
634+
request.HTTPBody = [NSData dataWithBytes:reinterpret_cast<const void*>(alphanumeric.data()) length:alphanumeric.size()];
635+
request.HTTPMethod = @"POST";
636+
[request setValue:[NSString stringWithFormat:@"%lu", alphanumeric.size()] forHTTPHeaderField:@"Content-Length"];
637+
638+
NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request
639+
completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) {
640+
taskResponse = response;
641+
taskData = data;
642+
taskError = error;
643+
[condition signal];
644+
}];
645+
[dataTask resume];
646+
647+
// Wait for data.
648+
ASSERT_TRUE([condition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:c_testTimeoutInSec]]);
649+
ASSERT_TRUE(taskResponse || taskData || taskError);
650+
651+
// Make sure we received a response.
652+
ASSERT_TRUE_MSG((taskResponse != nil), "FAILED: Response cannot be empty!");
653+
if (![taskResponse isKindOfClass:[NSHTTPURLResponse class]]) {
654+
ASSERT_FALSE_MSG(true, "FAILED: Response should be of kind NSHTTPURLResponse class!");
655+
}
656+
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)taskResponse;
657+
LOG_INFO("Received HTTP response status: %ld", [httpResponse statusCode]);
658+
ASSERT_EQ_MSG(200, [httpResponse statusCode], "FAILED: HTTP status 200 expected!");
659+
LOG_INFO("Received HTTP response headers: %@", [httpResponse allHeaderFields]);
660+
661+
// Make sure we received data.
662+
ASSERT_TRUE_MSG((taskData != nil), "FAILED: We should have received some data!");
663+
LOG_INFO("Received data: %@", [[NSString alloc] initWithData:taskData encoding:NSUTF8StringEncoding]);
664+
665+
// Make sure there was no error.
666+
ASSERT_EQ_MSG(nil, taskError, "FAILED: Task returned error!");
667+
}
668+
617669
//
618670
// NSURLSessionDownloadTask tests
619671
//

0 commit comments

Comments
 (0)