From eaaf68c07d8e7e764b8ef364f53cac42bd1c3a5c Mon Sep 17 00:00:00 2001 From: zymxxxs Date: Sat, 23 May 2020 18:44:52 +0800 Subject: [PATCH 1/5] build: only add synchronized in setter method --- YMHTTP/YMURLSessionTask.m | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/YMHTTP/YMURLSessionTask.m b/YMHTTP/YMURLSessionTask.m index e36a0b2..36640ce 100644 --- a/YMHTTP/YMURLSessionTask.m +++ b/YMHTTP/YMURLSessionTask.m @@ -372,27 +372,29 @@ - (BOOL)isCanResumeFromState { } - (void)setInternalState:(YMURLSessionTaskInternalState)internalState { - YMURLSessionTaskInternalState newValue = internalState; - if (![self isEasyHandlePausedForState:_internalState] && [self isEasyHandlePausedForState:newValue]) { - [self.easyHandle pauseReceive]; - } + @synchronized(self) { + YMURLSessionTaskInternalState newValue = internalState; + if (![self isEasyHandlePausedForState:_internalState] && [self isEasyHandlePausedForState:newValue]) { + [self.easyHandle pauseReceive]; + } - if ([self isEasyHandleAddedToMultiHandleForState:_internalState] && - ![self isEasyHandleAddedToMultiHandleForState:newValue]) { - [self.session removeHandle:self.easyHandle]; - } + if ([self isEasyHandleAddedToMultiHandleForState:_internalState] && + ![self isEasyHandleAddedToMultiHandleForState:newValue]) { + [self.session removeHandle:self.easyHandle]; + } - // set - YMURLSessionTaskInternalState oldValue = _internalState; - _internalState = internalState; + // set + YMURLSessionTaskInternalState oldValue = _internalState; + _internalState = internalState; - if (![self isEasyHandleAddedToMultiHandleForState:oldValue] && - [self isEasyHandleAddedToMultiHandleForState:_internalState]) { - [self.session addHandle:self.easyHandle]; - } + if (![self isEasyHandleAddedToMultiHandleForState:oldValue] && + [self isEasyHandleAddedToMultiHandleForState:_internalState]) { + [self.session addHandle:self.easyHandle]; + } - if ([self isEasyHandlePausedForState:oldValue] && ![self isEasyHandlePausedForState:_internalState]) { - [self.easyHandle unpauseReceive]; + if ([self isEasyHandlePausedForState:oldValue] && ![self isEasyHandlePausedForState:_internalState]) { + [self.easyHandle unpauseReceive]; + } } } From c1a36a5bc478e86faa5220fe91a0c0053b54ca58 Mon Sep 17 00:00:00 2001 From: zymxxxs Date: Sat, 23 May 2020 19:15:13 +0800 Subject: [PATCH 2/5] build: modify yml file to run ut success --- .github/workflows/build.yml | 10 +++- Example/Tests/TestURLSession.m | 79 +++++++++++++-------------- Example/Tests/TestURLSessionCache.m | 14 ++--- Example/Tests/TestURLSesssionDirect.m | 41 ++++++-------- 4 files changed, 73 insertions(+), 71 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 08d67b0..509c110 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,7 +7,15 @@ on: branches: [master] jobs: - test: + httpbin: + runs-on: macOS-latest + steps: + - name: install httpbin + run: | + docker pull kennethreitz/httpbin + docker run -p 80:80 kennethreitz/httpbin + + ut: runs-on: macOS-latest steps: - name: print env diff --git a/Example/Tests/TestURLSession.m b/Example/Tests/TestURLSession.m index bde361b..a91767d 100644 --- a/Example/Tests/TestURLSession.m +++ b/Example/Tests/TestURLSession.m @@ -21,7 +21,7 @@ @interface TestURLSession : XCTestCase @implementation TestURLSession - (void)testDataTaskWithURL { - NSString *urlString = @"http://httpbin.org/get?capital=testDataTaskWithURL"; + NSString *urlString = @"http://0.0.0.0/get?capital=testDataTaskWithURL"; NSURL *url = [NSURL URLWithString:urlString]; XCTestExpectation *te = [self expectationWithDescription:@"GET testDataTaskWithURL: with a delegate"]; YMDataTask *d = [[YMDataTask alloc] initWithExpectation:te]; @@ -42,7 +42,7 @@ - (void)testDataTaskWithURLCompletionHandler { } - (void)dataTaskWithURLCompletionHandlerWithSession:(YMURLSession *)session { - NSString *urlString = @"http://httpbin.org/get?capital=China"; + NSString *urlString = @"http://0.0.0.0/get?capital=China"; NSURL *url = [NSURL URLWithString:urlString]; XCTestExpectation *te = [self expectationWithDescription:@"GET dataTaskWithURLCompletionHandlerWithSession: with a completion handler"]; @@ -68,7 +68,7 @@ - (void)dataTaskWithURLCompletionHandlerWithSession:(YMURLSession *)session { } - (void)testDataTaskWithRequest { - NSString *urlString = @"http://httpbin.org/get?capital=testDataTaskWithRequest"; + NSString *urlString = @"http://0.0.0.0/get?capital=testDataTaskWithRequest"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; XCTestExpectation *te = [self expectationWithDescription:@"GET testDataTaskWithRequest: with a delegate"]; @@ -83,7 +83,7 @@ - (void)testDataTaskWithRequest { } - (void)testDataTaskWithURLRequestCompletionHandler { - NSString *urlString = @"http://httpbin.org/get?capital=testDataTaskWithURLRequestCompletionHandler"; + NSString *urlString = @"http://0.0.0.0/get?capital=testDataTaskWithURLRequestCompletionHandler"; NSURL *url = [NSURL URLWithString:urlString]; XCTestExpectation *te = [self expectationWithDescription:@"GET testDataTaskWithURLRequestCompletionHandler: with a completion handler"]; @@ -123,7 +123,7 @@ - (void)testDataTaskWithHttpInputStream { @"pretium, augue non elementum imperdiet, diam ex vestibulum tortor, non ultrices ante enim iaculis ex."; NSData *bodyData = [dataString dataUsingEncoding:NSUTF8StringEncoding]; for (NSString *method in httpMethods) { - NSString *urlString = [NSString stringWithFormat:@"https://httpbin.org/%@", [method lowercaseString]]; + NSString *urlString = [NSString stringWithFormat:@"http://0.0.0.0/%@", [method lowercaseString]]; for (id contentType in @[ @"text/plain; charset=utf-8", [NSNull null] ]) { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; request.HTTPMethod = method; @@ -189,7 +189,7 @@ - (void)testDataTaskWithHttpInputStream { } - (void)testGzippedDataTask { - NSString *urlString = @"http://httpbin.org/gzip"; + NSString *urlString = @"http://0.0.0.0/gzip"; NSURL *url = [NSURL URLWithString:urlString]; XCTestExpectation *te = [self expectationWithDescription:@"GET testGzippedDataTask: with a delegate"]; YMDataTask *d = [[YMDataTask alloc] initWithExpectation:te]; @@ -201,7 +201,7 @@ - (void)testGzippedDataTask { } - (void)testDownloadTaskWithURL { - NSString *urlString = @"http://httpbin.org/image/png"; + NSString *urlString = @"http://0.0.0.0/image/png"; NSURL *url = [NSURL URLWithString:urlString]; YMDownloadTask *d = [[YMDownloadTask alloc] initWithTestCase:self @@ -211,7 +211,7 @@ - (void)testDownloadTaskWithURL { } - (void)testDownloadTaskWithRequest { - NSString *urlString = @"http://httpbin.org/image/jepg"; + NSString *urlString = @"http://0.0.0.0/image/jepg"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; YMDownloadTask *d = @@ -233,7 +233,7 @@ - (void)downloadTaskWithRequestAndHandlerWithSession:(YMURLSession *)session { XCTestExpectation *te = [self expectationWithDescription: @"Download GET downloadTaskWithRequestAndHandlerWithSession: with a completion handler"]; - NSString *urlString = @"http://httpbin.org/image/svg"; + NSString *urlString = @"http://0.0.0.0/image/svg"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; YMURLSessionTask *task = @@ -254,7 +254,7 @@ - (void)downloadTaskWithRequestAndHandlerWithSession:(YMURLSession *)session { - (void)testDownloadTaskWithURLAndHandler { XCTestExpectation *te = [self expectationWithDescription:@"Download GET testDownloadTaskWithURLAndHandler: with a completion handler"]; - NSString *urlString = @"http://httpbin.org/image/webp"; + NSString *urlString = @"http://0.0.0.0/image/webp"; NSURL *url = [NSURL URLWithString:urlString]; YMURLSessionConfiguration *config = [YMURLSessionConfiguration defaultSessionConfiguration]; @@ -276,7 +276,7 @@ - (void)testDownloadTaskWithURLAndHandler { } - (void)testGzippedDownloadTask { - NSString *urlString = @"http://httpbin.org/image/gzip"; + NSString *urlString = @"http://0.0.0.0/image/gzip"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; YMDownloadTask *d = @@ -291,7 +291,7 @@ - (void)testFinishTasksAndInvalidate { XCTestExpectation *completionExpectation = [self expectationWithDescription:@"GET testFinishTasksAndInvalidate: task completion before session invalidation"]; - NSString *urlString = @"http://httpbin.org/get"; + NSString *urlString = @"http://0.0.0.0/get"; NSURL *url = [NSURL URLWithString:urlString]; YMSessionDelegate *delegate = [YMSessionDelegate new]; delegate.invalidateExpectation = invalidateExpectation; @@ -332,7 +332,7 @@ - (void)testTaskError { } - (void)testTaskCopy { - NSString *urlString = @"http://httpbin.org/get"; + NSString *urlString = @"http://0.0.0.0/get"; NSURL *url = [NSURL URLWithString:urlString]; YMURLSessionConfiguration *config = [YMURLSessionConfiguration defaultSessionConfiguration]; YMURLSession *session = [YMURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; @@ -342,7 +342,7 @@ - (void)testTaskCopy { } - (void)testCancelTask { - NSString *urlString = @"http://httpbin.org/get"; + NSString *urlString = @"http://0.0.0.0/get"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; @@ -365,7 +365,7 @@ - (void)testVerifyRequestHeaders { YMURLSessionConfiguration *config = [YMURLSessionConfiguration defaultSessionConfiguration]; YMURLSession *session = [YMURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; - NSString *urlString = @"http://httpbin.org/post"; + NSString *urlString = @"http://0.0.0.0/post"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; @@ -400,7 +400,7 @@ - (void)testVerifyHttpAdditionalHeaders { }; YMURLSession *session = [YMURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; - NSString *urlString = @"http://httpbin.org/post"; + NSString *urlString = @"http://0.0.0.0/post"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; @@ -437,7 +437,7 @@ - (void)testTaskTimeOut { config.timeoutIntervalForRequest = 5; YMURLSession *session = [YMURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; - NSString *urlString = @"http://httpbin.org/delay/10"; + NSString *urlString = @"http://0.0.0.0/delay/10"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.timeoutInterval = 5; @@ -459,7 +459,7 @@ - (void)testConcurrentRequests { dispatch_group_t gourp = dispatch_group_create(); for (int i = 0; i < 10; i++) { dispatch_group_enter(gourp); - NSString *urlString = [NSString stringWithFormat:@"http://httpbin.org/get?capital=testDataTaskWithURL%@", @(i)]; + NSString *urlString = [NSString stringWithFormat:@"http://0.0.0.0/get?capital=testDataTaskWithURL%@", @(i)]; NSURL *url = [NSURL URLWithString:urlString]; XCTestExpectation *te = [self expectationWithDescription:[NSString @@ -483,7 +483,7 @@ - (void)testOutOfRangeButCorrectlyFormattedHTTPCode { config.timeoutIntervalForRequest = 8; YMURLSession *session = [YMURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; - NSString *urlString = @"http://httpbin.org/status/999"; + NSString *urlString = @"http://0.0.0.0/status/999"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *req = [NSURLRequest requestWithURL:url]; @@ -508,7 +508,7 @@ - (void)testMissingContentLengthButStillABody { config.timeoutIntervalForRequest = 8; YMURLSession *session = [YMURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; - NSString *urlString = @"https://httpbin.org/stream-bytes/10"; + NSString *urlString = @"http://0.0.0.0/stream-bytes/10"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *req = [NSURLRequest requestWithURL:url]; @@ -534,7 +534,7 @@ - (void)testSimpleUploadWithDelegate { delegate.uploadCompletedExpectation = [self expectationWithDescription:@"PUT testSimpleUploadWithDelegate"]; - NSString *urlString = @"https://httpbin.org/put"; + NSString *urlString = @"http://0.0.0.0/put"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; req.HTTPMethod = @"PUT"; @@ -549,7 +549,7 @@ - (void)testSimpleUploadWithDelegate { - (void)testRequestWithEmptyBody { NSArray *httpMethods = @[ @"GET", @"PUT", @"POST", @"DELETE" ]; for (NSString *method in httpMethods) { - NSString *urlString = [NSString stringWithFormat:@"https://httpbin.org/%@", [method lowercaseString]]; + NSString *urlString = [NSString stringWithFormat:@"http://0.0.0.0/%@", [method lowercaseString]]; for (id body in @[ [NSNull null], [NSData data] ]) { for (id contentType in @[ @"text/plain; charset=utf-8", [NSNull null] ]) { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; @@ -606,7 +606,7 @@ - (void)testRequestWithNonEmptyBody { NSArray *httpMethods = @[ @"GET", @"PUT", @"POST", @"DELETE" ]; for (NSString *method in httpMethods) { NSData *bodyData = [@"This is a request body" dataUsingEncoding:NSUTF8StringEncoding]; - NSString *urlString = [NSString stringWithFormat:@"https://httpbin.org/%@", [method lowercaseString]]; + NSString *urlString = [NSString stringWithFormat:@"http://0.0.0.0/%@", [method lowercaseString]]; for (id contentType in @[ @"text/plain; charset=utf-8", [NSNull null] ]) { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; request.HTTPMethod = method; @@ -683,7 +683,7 @@ - (void)testSimpleUploadWithDelegateProvidingInputStream { completionHandler([[NSInputStream alloc] initWithData:data]); }; - NSString *urlString = [NSString stringWithFormat:@"http://httpbin.org/%@", [method lowercaseString]]; + NSString *urlString = [NSString stringWithFormat:@"http://0.0.0.0/%@", [method lowercaseString]]; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; req.HTTPMethod = method; @@ -769,8 +769,7 @@ - (void)testDisableCookiesStorage { XCTAssertEqual(config.HTTPCookieStorage.cookies.count, 0); YMURLSession *session = [YMURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; - NSString *urlString = - @"https://httpbin.org/response-headers?Set-Cookie=a=bbbb&Set-Cookie=a=bbbb1&Set-Cookie=b=bbbb2"; + NSString *urlString = @"http://0.0.0.0/response-headers?Set-Cookie=a=bbbb&Set-Cookie=a=bbbb1&Set-Cookie=b=bbbb2"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; req.HTTPMethod = @"POST"; @@ -800,7 +799,7 @@ - (void)testCookiesStorage { YMURLSession *session = [YMURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; NSString *urlString = - @"https://httpbin.org/" + @"http://0.0.0.0/" @"response-headers?Set-Cookie=a=bbbb&Set-Cookie=a=bbbb1&Set-Cookie=b=bbbb2&Set-Cookie=b=bbbb2"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; @@ -834,7 +833,7 @@ - (void)testPreviouslySetCookiesAreSentInLaterRequests { YMURLSession *session = [YMURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; NSString *urlString = - @"https://httpbin.org/" + @"http://0.0.0.0/" @"response-headers?Set-Cookie=a=bbbb&Set-Cookie=a=bbbb1&Set-Cookie=b=bbbb2&Set-Cookie=b=bbbb2"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; @@ -850,7 +849,7 @@ - (void)testPreviouslySetCookiesAreSentInLaterRequests { XCTAssertNotNil(httpresponse.allHeaderFields[@"Set-Cookie"]); XCTAssertEqual([NSHTTPCookieStorage sharedHTTPCookieStorage].cookies.count, 2); - task2 = [session taskWithURL:[NSURL URLWithString:@"https://httpbin.org/cookies"] + task2 = [session taskWithURL:[NSURL URLWithString:@"http://0.0.0.0/cookies"] completionHandler:^( NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) { XCTAssertNotNil(data); @@ -872,7 +871,7 @@ - (void)testPreviouslySetCookiesAreSentInLaterRequests { } //- (void)testBasicAuthRequest { -// NSString *urlString = @"https://httpbin.org/basic-auth/zymxxxs/zymxxxs"; +// NSString *urlString = @"http://0.0.0.0/basic-auth/zymxxxs/zymxxxs"; // NSURL *url = [NSURL URLWithString:urlString]; // XCTestExpectation *te = [self expectationWithDescription:@"GET testBasicAuthRequest: with a delegate"]; // YMDataTask *d = [[YMDataTask alloc] initWithExpectation:te]; @@ -887,7 +886,7 @@ - (void)testPostWithEmptyBody { config.timeoutIntervalForRequest = 5; YMURLSession *session = [YMURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; - NSString *urlString = @"https://httpbin.org/post"; + NSString *urlString = @"http://0.0.0.0/post"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; req.HTTPMethod = @"POST"; @@ -909,7 +908,7 @@ - (void)testCheckErrorTypeAfterInvalidateAndCancel { XCTestExpectation *expectation = [self expectationWithDescription:@"Check error code of tasks after invalidateAndCancel"]; - NSString *urlString = @"http://httpbin.org/delay/5"; + NSString *urlString = @"http://0.0.0.0/delay/5"; NSURL *url = [NSURL URLWithString:urlString]; YMSessionDelegate *delegate = [YMSessionDelegate new]; YMURLSessionConfiguration *config = [YMURLSessionConfiguration defaultSessionConfiguration]; @@ -931,9 +930,9 @@ - (void)testTaskCountAfterInvalidateAndCancel { YMURLSessionConfiguration *config = [YMURLSessionConfiguration defaultSessionConfiguration]; YMURLSession *session = [YMURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; - NSURL *url1 = [NSURL URLWithString:@"http://httpbin.org/delay/5"]; - NSURL *url2 = [NSURL URLWithString:@"http://httpbin.org/delay/15"]; - NSURL *url3 = [NSURL URLWithString:@"http://httpbin.org/delay/25"]; + NSURL *url1 = [NSURL URLWithString:@"http://0.0.0.0/delay/5"]; + NSURL *url2 = [NSURL URLWithString:@"http://0.0.0.0/delay/15"]; + NSURL *url3 = [NSURL URLWithString:@"http://0.0.0.0/delay/25"]; YMURLSessionTask *task1 = [session taskWithURL:url1]; YMURLSessionTask *task2 = [session taskWithURL:url2]; @@ -971,9 +970,9 @@ - (void)testGetAllTasks { YMURLSessionConfiguration *config = [YMURLSessionConfiguration defaultSessionConfiguration]; YMURLSession *session = [YMURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; - NSURL *url1 = [NSURL URLWithString:@"https://httpbin.org/delay/5"]; - NSURL *url2 = [NSURL URLWithString:@"https://httpbin.org/delay/10"]; - NSURL *url3 = [NSURL URLWithString:@"https://httpbin.org/delay/15"]; + NSURL *url1 = [NSURL URLWithString:@"http://0.0.0.0/delay/5"]; + NSURL *url2 = [NSURL URLWithString:@"http://0.0.0.0/delay/10"]; + NSURL *url3 = [NSURL URLWithString:@"http://0.0.0.0/delay/15"]; YMURLSessionTask *task1 = [session taskWithURL:url1]; YMURLSessionTask *task2 = [session taskWithURL:url2]; @@ -1040,7 +1039,7 @@ - (void)testNoDoubleCallbackWhenCancellingAndProtocolFailsFast { } - (void)testCancelledTasksCannotBeResumed { - NSString *urlString = @"http://httpbin.org/delay/5"; + NSString *urlString = @"http://0.0.0.0/delay/5"; NSURL *url = [NSURL URLWithString:urlString]; YMSessionDelegate *delegate = [YMSessionDelegate new]; YMURLSessionConfiguration *config = [YMURLSessionConfiguration defaultSessionConfiguration]; @@ -1060,7 +1059,7 @@ - (void)testCancelledTasksCannotBeResumed { - (void)testSuspendResumeTask { XCTestExpectation *expectation = [self expectationWithDescription:@"GET testSuspendResumeTask: suspend task"]; YMURLSessionTask *task = [[YMURLSession sharedSession] - taskWithURL:[NSURL URLWithString:@"https://httpbin.org/get"] + taskWithURL:[NSURL URLWithString:@"http://0.0.0.0/get"] completionHandler:^(NSData *_Nullable data, NSHTTPURLResponse *_Nullable response, NSError *_Nullable error) { if (response.statusCode == 200) { [expectation fulfill]; diff --git a/Example/Tests/TestURLSessionCache.m b/Example/Tests/TestURLSessionCache.m index feca7d8..6a76160 100644 --- a/Example/Tests/TestURLSessionCache.m +++ b/Example/Tests/TestURLSessionCache.m @@ -19,12 +19,13 @@ @implementation TestURLSessionCache - (void)resetURLCache { NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:0 diskPath:nil]; [NSURLCache setSharedURLCache:URLCache]; + sleep(1); } - (void)testCacheUseProtocolCachePolicy { [self resetURLCache]; - NSString *urlString = @"http://httpbin.org/cache/200"; + NSString *urlString = @"http://0.0.0.0/cache/200"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; XCTestExpectation *te = [self expectationWithDescription:@"GET testCacheUseProtocolCachePolicy: with a delegate"]; @@ -65,7 +66,7 @@ - (void)testCacheUseProtocolCachePolicy { - (void)testCacheUseIgnoringLocalCacheData { [self resetURLCache]; - NSString *urlString = @"http://httpbin.org/cache/200"; + NSString *urlString = @"http://0.0.0.0/cache/200"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; XCTestExpectation *te = @@ -87,7 +88,7 @@ - (void)testCacheUseIgnoringLocalCacheData { NSCachedURLResponse *cachedURLResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request]; XCTAssertNotNil(cachedURLResponse); - sleep(1); + sleep(2); XCTestExpectation *te1 = [self expectationWithDescription:@"GET testCacheUseProtocolCachePolicy1: with a delegate"]; YMCacheDataTask *d1 = [[YMCacheDataTask alloc] initWithExpectation:te1]; @@ -109,7 +110,7 @@ - (void)testCacheUseIgnoringLocalCacheData { - (void)testCacheUseReturnCacheDataElseLoad { [self resetURLCache]; - NSString *urlString = @"http://httpbin.org/cache/200"; + NSString *urlString = @"http://0.0.0.0/cache/200"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; XCTestExpectation *te = @@ -149,7 +150,6 @@ - (void)testCacheUseReturnCacheDataElseLoad { [self resetURLCache]; XCTAssertNil([[NSURLCache sharedURLCache] cachedResponseForRequest:request]); - sleep(2); XCTestExpectation *te2 = [self expectationWithDescription:@"GET testCacheUseReturnCacheDataElseLoad 2: with a delegate"]; @@ -172,7 +172,7 @@ - (void)testCacheUseReturnCacheDataElseLoad { - (void)testCacheUseReturnCacheDataDontLoad { [[NSURLCache sharedURLCache] removeAllCachedResponses]; - NSString *urlString = @"http://httpbin.org/cache/200"; + NSString *urlString = @"http://0.0.0.0/cache/200"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; XCTestExpectation *te = @@ -226,7 +226,7 @@ - (void)testCacheUseReturnCacheDataDontLoad { - (void)testCacheUsingResponseAllow { [self resetURLCache]; - NSString *urlString = @"http://httpbin.org/get"; + NSString *urlString = @"http://0.0.0.0/get"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; XCTestExpectation *te = [self expectationWithDescription:@"GET testCacheUsingResponseAllow: with a delegate"]; diff --git a/Example/Tests/TestURLSesssionDirect.m b/Example/Tests/TestURLSesssionDirect.m index 729a7ed..6268517 100644 --- a/Example/Tests/TestURLSesssionDirect.m +++ b/Example/Tests/TestURLSesssionDirect.m @@ -20,7 +20,7 @@ @implementation TestURLSesssionDirect - (void)testHttpRedirectionWithCode300 { NSArray *httpMethods = @[ @"HEAD", @"GET", @"PUT", @"POST", @"DELETE" ]; for (NSString *method in httpMethods) { - NSString *urlString = @"https://httpbin.org/redirect-to?url=%2Fget&status_code=300"; + NSString *urlString = @"http://0.0.0.0/redirect-to?url=%2Fget&status_code=300"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = method; @@ -47,9 +47,8 @@ - (void)testHttpRedirectionWithCode301_302 { NSArray *httpMethods = @[ @"POST", @"HEAD", @"GET", @"PUT", @"DELETE" ]; for (NSNumber *statusCode in @[ @(301), @(302) ]) { for (NSString *method in httpMethods) { - NSString *urlString = - [NSString stringWithFormat:@"https://httpbin.org/redirect-to?url=/anything&status_code=%@", - statusCode.stringValue]; + NSString *urlString = [NSString + stringWithFormat:@"http://0.0.0.0/redirect-to?url=/anything&status_code=%@", statusCode.stringValue]; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = method; @@ -100,7 +99,7 @@ - (void)testHttpRedirectionWithCode301_302 { - (void)testHttpRedirectionWithCode303 { NSArray *httpMethods = @[ @"POST", @"HEAD", @"GET", @"PUT", @"DELETE" ]; for (NSString *method in httpMethods) { - NSString *urlString = @"https://httpbin.org/redirect-to?url=/anything&status_code=303"; + NSString *urlString = @"http://0.0.0.0/redirect-to?url=/anything&status_code=303"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = method; @@ -130,7 +129,7 @@ - (void)testHttpRedirectionWithCode303 { - (void)testHttpRedirectionWithCode304 { NSArray *httpMethods = @[ @"HEAD", @"GET", @"PUT", @"POST", @"DELETE" ]; for (NSString *method in httpMethods) { - NSString *urlString = @"https://httpbin.org/redirect-to?url=%2Fget&status_code=304"; + NSString *urlString = @"http://0.0.0.0/redirect-to?url=%2Fget&status_code=304"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = method; @@ -158,9 +157,8 @@ - (void)testHttpRedirectionWithCode305_308 { NSArray *httpMethods = @[ @"POST", @"HEAD", @"GET", @"PUT", @"DELETE" ]; for (NSNumber *statusCode in @[ @(305), @(306), @(307), @(308) ]) { for (NSString *method in httpMethods) { - NSString *urlString = - [NSString stringWithFormat:@"https://httpbin.org/redirect-to?url=/anything&status_code=%@", - statusCode.stringValue]; + NSString *urlString = [NSString + stringWithFormat:@"http://0.0.0.0/redirect-to?url=/anything&status_code=%@", statusCode.stringValue]; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = method; @@ -208,7 +206,7 @@ - (void)testHttpRedirectionWithCode305_308 { } - (void)testHttpRedirectionWithCompleteRelativePath { - NSString *urlString = @"https://httpbin.org/redirect-to?url=https%3A%2F%2Fhttpbin.org%2Fget"; + NSString *urlString = @"http://0.0.0.0/redirect-to?url=http%3A%2F%2F0.0.0.0%2Fget"; NSURL *url = [NSURL URLWithString:urlString]; XCTestExpectation *te = [self expectationWithDescription:@"GET testHttpRedirectionWithCompleteRelativePath: with HTTP redirection"]; @@ -217,13 +215,13 @@ - (void)testHttpRedirectionWithCompleteRelativePath { [self waitForExpectationsWithTimeout:12 handler:nil]; if (!d.error) { XCTAssertEqualObjects(d.result[@"url"], - @"https://httpbin.org/get", + @"http://0.0.0.0/get", @"testHttpRedirectionWithCompleteRelativePath returned an unexpected result"); } } - (void)testHttpRedirectionWithInCompleteRelativePath { - NSString *urlString = @"https://httpbin.org/redirect-to?url=%2Fget"; + NSString *urlString = @"http://0.0.0.0/redirect-to?url=%2Fget"; NSURL *url = [NSURL URLWithString:urlString]; XCTestExpectation *te = [self expectationWithDescription:@"GET testHttpRedirectionWithInCompleteRelativePath: with HTTP redirection"]; @@ -232,7 +230,7 @@ - (void)testHttpRedirectionWithInCompleteRelativePath { [self waitForExpectationsWithTimeout:12 handler:nil]; if (!d.error) { XCTAssertEqualObjects(d.result[@"url"], - @"https://httpbin.org/get", + @"http://0.0.0.0/get", @"testHttpRedirectionWithCompleteRelativePath returned an unexpected result"); } } @@ -245,7 +243,7 @@ - (void)testHttpRedirectionTimeout { config.timeoutIntervalForRequest = 5; YMURLSession *session = [YMURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; - NSString *urlString = @"https://httpbin.org/redirect-to?url=%2Fdelay%2F10"; + NSString *urlString = @"http://0.0.0.0/redirect-to?url=%2Fdelay%2F10"; NSURL *url = [NSURL URLWithString:urlString]; YMURLSessionTask *task = @@ -265,8 +263,7 @@ - (void)testHttpRedirectionTimeout { - (void)testHttpRedirectDontFollowUsingNil { NSArray *httpMethods = @[ @"HEAD", @"GET", @"PUT", @"POST", @"DELETE" ]; for (NSString *method in httpMethods) { - NSString *urlString = - [NSString stringWithFormat:@"https://httpbin.org/redirect-to?url=/anything&status_code=302"]; + NSString *urlString = [NSString stringWithFormat:@"http://0.0.0.0/redirect-to?url=/anything&status_code=302"]; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = method; @@ -302,12 +299,11 @@ - (void)testHttpRedirectDontFollowUsingNil { - (void)testHttpRedirectDontFollowIgnoringHandler { NSArray *httpMethods = @[ @"HEAD", @"GET", @"PUT", @"POST", @"DELETE" ]; for (NSString *method in httpMethods) { - NSString *urlString = - [NSString stringWithFormat:@"https://httpbin.org/redirect-to?url=/anything&status_code=302"]; + NSString *urlString = [NSString stringWithFormat:@"http://0.0.0.0/redirect-to?url=/anything&status_code=302"]; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = method; - request.timeoutInterval = 5.f; + request.timeoutInterval = 2.f; XCTestExpectation *te = [self expectationWithDescription: [NSString stringWithFormat:@"%@ testHttpRedirectDontFollowIgnoringHandler: with redirection", method]]; @@ -320,7 +316,7 @@ - (void)testHttpRedirectDontFollowIgnoringHandler { }; [d runWithRequest:request]; - [self waitForExpectationsWithTimeout:10.f handler:nil]; + [self waitForExpectationsWithTimeout:3.f handler:nil]; XCTAssertNil(d.error); XCTAssertNil(d.receivedData); XCTAssertNil(d.response); @@ -336,8 +332,7 @@ - (void)testHttpRedirectDontFollowIgnoringHandler { - (void)testHttpRedirectionChainInheritsTimeoutInterval { NSArray *httpMethods = @[ @"HEAD", @"GET", @"PUT", @"POST", @"DELETE" ]; for (NSString *method in httpMethods) { - NSString *urlString = - [NSString stringWithFormat:@"https://httpbin.org/redirect-to?url=/anything&status_code=302"]; + NSString *urlString = [NSString stringWithFormat:@"http://0.0.0.0/redirect-to?url=/anything&status_code=302"]; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = method; @@ -364,7 +359,7 @@ - (void)testHttpRedirectionChainInheritsTimeoutInterval { } - (void)testHttpRedirectionExceededMaxRedirects { - NSString *urlString = [NSString stringWithFormat:@"https://httpbin.org/redirect/18"]; + NSString *urlString = [NSString stringWithFormat:@"http://0.0.0.0/redirect/18"]; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.timeoutInterval = 3.f; From 75cfd5b9b54a84ee06dc175ea6b4bedfaa0391b9 Mon Sep 17 00:00:00 2001 From: zymxxxs Date: Sat, 23 May 2020 19:54:08 +0800 Subject: [PATCH 3/5] build: modify the build.yml --- .github/workflows/build.yml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 509c110..c366e74 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,28 +7,27 @@ on: branches: [master] jobs: - httpbin: + ut: runs-on: macOS-latest steps: + - uses: actions-hub/docker/cli@master + - name: install httpbin run: | docker pull kennethreitz/httpbin docker run -p 80:80 kennethreitz/httpbin - ut: - runs-on: macOS-latest - steps: - - name: print env - run: | - xcodebuild -version - uses: actions/checkout@master + - name: install cocoapods run: gem install cocoapods + - name: install dependencies run: | cd Example pod install shell: bash + - name: run test run: xcodebuild test -workspace ./Example/YMHTTP.xcworkspace -scheme 'YMHTTP-Example' -destination 'platform=iOS Simulator,name=iPhone 11' shell: bash From dd6856d7930b64f678547a7ec1b219d883b1baee Mon Sep 17 00:00:00 2001 From: zymxxxs Date: Sat, 23 May 2020 20:04:29 +0800 Subject: [PATCH 4/5] build: modify build.yml --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c366e74..e9c86b3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,12 +10,12 @@ jobs: ut: runs-on: macOS-latest steps: - - uses: actions-hub/docker/cli@master - name: install httpbin run: | docker pull kennethreitz/httpbin docker run -p 80:80 kennethreitz/httpbin + shell: bash - uses: actions/checkout@master @@ -27,7 +27,7 @@ jobs: cd Example pod install shell: bash - + - name: run test run: xcodebuild test -workspace ./Example/YMHTTP.xcworkspace -scheme 'YMHTTP-Example' -destination 'platform=iOS Simulator,name=iPhone 11' shell: bash From 7c1c7c76e0cff9608a84501a0a7daf537e8f333d Mon Sep 17 00:00:00 2001 From: zymxxxs Date: Sat, 23 May 2020 20:13:32 +0800 Subject: [PATCH 5/5] build: modfy the build.yml --- .github/workflows/build.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e9c86b3..9f7ab27 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,15 +7,19 @@ on: branches: [master] jobs: - ut: - runs-on: macOS-latest + httpbin: + runs-on: ubuntu-latest steps: + - uses: actions-hub/docker/cli@master - name: install httpbin run: | docker pull kennethreitz/httpbin docker run -p 80:80 kennethreitz/httpbin shell: bash + ut: + runs-on: macOS-latest + steps: - uses: actions/checkout@master