@@ -614,6 +614,58 @@ - (void)URLSession:(NSURLSession*)session
614
614
ASSERT_EQ_MSG (nil , taskError, " FAILED: Connection returned error!" );
615
615
}
616
616
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
+
617
669
//
618
670
// NSURLSessionDownloadTask tests
619
671
//
0 commit comments