diff --git a/GoogleUtilities/Tests/Unit/Environment/NSURLSession+GULPromisesTests.m b/GoogleUtilities/Tests/Unit/Environment/NSURLSession+GULPromisesTests.m index deec84af..eacf6499 100644 --- a/GoogleUtilities/Tests/Unit/Environment/NSURLSession+GULPromisesTests.m +++ b/GoogleUtilities/Tests/Unit/Environment/NSURLSession+GULPromisesTests.m @@ -16,16 +16,13 @@ #import -#import #import "FBLPromise+Testing.h" -#import "GoogleUtilities/Tests/Unit/Shared/URLSession/FIRURLSessionOCMockStub.h" #import "GoogleUtilities/Environment/Public/GoogleUtilities/GULURLSessionDataResponse.h" #import "GoogleUtilities/Environment/Public/GoogleUtilities/NSURLSession+GULPromises.h" @interface NSURLSession_GULPromisesTests : XCTestCase @property(nonatomic) NSURLSession *URLSession; -@property(nonatomic) id URLSessionMock; @end @implementation NSURLSession_GULPromisesTests @@ -33,68 +30,48 @@ @implementation NSURLSession_GULPromisesTests - (void)setUp { self.URLSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]]; - self.URLSessionMock = OCMPartialMock(self.URLSession); -} - -- (void)tearDown { - [self.URLSessionMock stopMocking]; - self.URLSessionMock = nil; - self.URLSession = nil; } - (void)testDataTaskPromiseWithRequestSuccess { - NSURL *url = [NSURL URLWithString:@"https://localhost"]; - NSURLRequest *request = [NSURLRequest requestWithURL:url]; - - NSHTTPURLResponse *expectedResponse = [[NSHTTPURLResponse alloc] initWithURL:url - statusCode:200 - HTTPVersion:@"1.1" - headerFields:nil]; - NSData *expectedBody = [@"body" dataUsingEncoding:NSUTF8StringEncoding]; - - [FIRURLSessionOCMockStub - stubURLSessionDataTaskWithResponse:expectedResponse - body:expectedBody - error:nil - URLSessionMock:self.URLSessionMock - requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) { - return [sentRequest isEqual:request]; - }]; - - __auto_type taskPromise = [self.URLSessionMock gul_dataTaskPromiseWithRequest:request]; - - XCTAssert(FBLWaitForPromisesWithTimeout(0.5)); - - XCTAssertTrue(taskPromise.isFulfilled); + // Given + NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"success.txt"]; + [[NSFileManager defaultManager] removeItemAtPath:tempPath error:nil]; + NSData *expectedData = [@"Hello, world!" dataUsingEncoding:NSUTF8StringEncoding]; + BOOL success = [[NSFileManager defaultManager] createFileAtPath:tempPath + contents:expectedData + attributes:nil]; + XCTAssert(success); + + // When + NSURL *tempURL = [NSURL fileURLWithPath:tempPath]; + NSURLRequest *request = [NSURLRequest requestWithURL:tempURL]; + __auto_type taskPromise = [self.URLSession gul_dataTaskPromiseWithRequest:request]; + + // Then + XCTAssert(FBLWaitForPromisesWithTimeout(1.0)); + XCTAssert(taskPromise.isFulfilled); XCTAssertNil(taskPromise.error); - XCTAssertEqualObjects(taskPromise.value.HTTPResponse, expectedResponse); - XCTAssertEqualObjects(taskPromise.value.HTTPBody, expectedBody); + XCTAssertEqualObjects(expectedData, taskPromise.value.HTTPBody); + XCTAssertEqual(taskPromise.value.HTTPResponse.statusCode, 200); } - (void)testDataTaskPromiseWithRequestError { - NSURL *url = [NSURL URLWithString:@"https://localhost"]; - NSURLRequest *request = [NSURLRequest requestWithURL:url]; - - NSError *expectedError = [NSError errorWithDomain:@"testDataTaskPromiseWithRequestError" - code:-1 - userInfo:nil]; - - [FIRURLSessionOCMockStub - stubURLSessionDataTaskWithResponse:nil - body:nil - error:expectedError - URLSessionMock:self.URLSessionMock - requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) { - return [sentRequest isEqual:request]; - }]; - - __auto_type taskPromise = [self.URLSessionMock gul_dataTaskPromiseWithRequest:request]; - - XCTAssert(FBLWaitForPromisesWithTimeout(0.5)); - - XCTAssertTrue(taskPromise.isRejected); - XCTAssertEqualObjects(taskPromise.error, expectedError); - XCTAssertNil(taskPromise.value); + // Given + NSString *tempPath = + [NSTemporaryDirectory() stringByAppendingPathComponent:@"does_not_exist.txt"]; + XCTAssertFalse([[NSFileManager defaultManager] fileExistsAtPath:tempPath]); + + // When + NSURL *tempURL = [NSURL fileURLWithPath:tempPath]; + NSURLRequest *request = [NSURLRequest requestWithURL:tempURL]; + __auto_type taskPromise = [self.URLSession gul_dataTaskPromiseWithRequest:request]; + + // Then + XCTAssert(FBLWaitForPromisesWithTimeout(1.0)); + XCTAssert(taskPromise.isRejected); + XCTAssertNotNil(taskPromise.error); + XCTAssertNil(taskPromise.value.HTTPBody); + XCTAssertEqual(taskPromise.value.HTTPResponse.statusCode, 0); } @end diff --git a/GoogleUtilities/Tests/Unit/Shared/URLSession/FIRURLSessionOCMockStub.h b/GoogleUtilities/Tests/Unit/Shared/URLSession/FIRURLSessionOCMockStub.h deleted file mode 100644 index 2895b03c..00000000 --- a/GoogleUtilities/Tests/Unit/Shared/URLSession/FIRURLSessionOCMockStub.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#import - -NS_ASSUME_NONNULL_BEGIN - -typedef BOOL (^FIRRequestValidationBlock)(NSURLRequest *request); - -@interface FIRURLSessionOCMockStub : NSObject - -+ (id)stubURLSessionDataTaskWithResponse:(nullable NSHTTPURLResponse *)response - body:(nullable NSData *)body - error:(nullable NSError *)error - URLSessionMock:(id)URLSessionMock - requestValidationBlock:(nullable FIRRequestValidationBlock)requestValidationBlock; - -+ (NSHTTPURLResponse *)HTTPResponseWithCode:(NSInteger)statusCode; - -@end - -NS_ASSUME_NONNULL_END diff --git a/GoogleUtilities/Tests/Unit/Shared/URLSession/FIRURLSessionOCMockStub.m b/GoogleUtilities/Tests/Unit/Shared/URLSession/FIRURLSessionOCMockStub.m deleted file mode 100644 index f88b8116..00000000 --- a/GoogleUtilities/Tests/Unit/Shared/URLSession/FIRURLSessionOCMockStub.m +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#import "GoogleUtilities/Tests/Unit/Shared/URLSession/FIRURLSessionOCMockStub.h" - -#import - -@implementation FIRURLSessionOCMockStub - -+ (id)stubURLSessionDataTaskWithResponse:(NSHTTPURLResponse *)response - body:(NSData *)body - error:(NSError *)error - URLSessionMock:(id)URLSessionMock - requestValidationBlock:(FIRRequestValidationBlock)requestValidationBlock { - id mockDataTask = OCMStrictClassMock([NSURLSessionDataTask class]); - - // Validate request content. - FIRRequestValidationBlock nonOptionalRequestValidationBlock = - requestValidationBlock ?: ^BOOL(id request) { - return YES; - }; - - id URLRequestValidationArg = [OCMArg checkWithBlock:nonOptionalRequestValidationBlock]; - - // Save task completion to be called on the `[NSURLSessionDataTask resume]` - __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *); - id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) { - taskCompletion = obj; - return YES; - }]; - - // Expect `dataTaskWithRequest` to be called. - OCMExpect([URLSessionMock dataTaskWithRequest:URLRequestValidationArg - completionHandler:completionArg]) - .andReturn(mockDataTask); - - // Expect the task to be resumed and call the task completion. - OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]).andDo(^(NSInvocation *invocation) { - taskCompletion(body, response, error); - }); - - return mockDataTask; -} - -+ (NSHTTPURLResponse *)HTTPResponseWithCode:(NSInteger)statusCode { - return [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"http://localhost"] - statusCode:statusCode - HTTPVersion:@"HTTP/1.1" - headerFields:nil]; -} - -@end