@@ -331,6 +331,54 @@ class TestURLSession: LoopbackServerTest {
331331 waitForExpectations ( timeout: 12 )
332332 }
333333
334+ func test_unhandledURLProtocol( ) {
335+ let urlString = " foobar://127.0.0.1: \( TestURLSession . serverPort) /Nepal "
336+ let url = URL ( string: urlString) !
337+ let session = URLSession ( configuration: URLSessionConfiguration . default,
338+ delegate: nil ,
339+ delegateQueue: nil )
340+ let completionExpectation = expectation ( description: " GET \( urlString) : Unsupported URL error " )
341+ let task = session. dataTask ( with: url) { ( data, response, _error) in
342+ XCTAssertNil ( data)
343+ XCTAssertNil ( response)
344+ let error = _error as? URLError
345+ XCTAssertNotNil ( error)
346+ XCTAssertEqual ( error? . code, . unsupportedURL)
347+ completionExpectation. fulfill ( )
348+ }
349+ task. resume ( )
350+
351+ waitForExpectations ( timeout: 5 ) { error in
352+ XCTAssertNil ( error)
353+ XCTAssertEqual ( ( task. error as? URLError ) ? . code, . unsupportedURL)
354+ }
355+ }
356+
357+ func test_requestToNilURL( ) {
358+ let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /Nepal "
359+ let url = URL ( string: urlString) !
360+ let session = URLSession ( configuration: URLSessionConfiguration . default,
361+ delegate: nil ,
362+ delegateQueue: nil )
363+ let completionExpectation = expectation ( description: " DataTask with nil URL: Unsupported URL error " )
364+ var request = URLRequest ( url: url)
365+ request. url = nil
366+ let task = session. dataTask ( with: request) { ( data, response, _error) in
367+ XCTAssertNil ( data)
368+ XCTAssertNil ( response)
369+ let error = _error as? URLError
370+ XCTAssertNotNil ( error)
371+ XCTAssertEqual ( error? . code, . unsupportedURL)
372+ completionExpectation. fulfill ( )
373+ }
374+ task. resume ( )
375+
376+ waitForExpectations ( timeout: 5 ) { error in
377+ XCTAssertNil ( error)
378+ XCTAssertEqual ( ( task. error as? URLError ) ? . code, . unsupportedURL)
379+ }
380+ }
381+
334382 func test_suspendResumeTask( ) throws {
335383 let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /get "
336384 let url = try XCTUnwrap ( URL ( string: urlString) )
@@ -1819,6 +1867,8 @@ class TestURLSession: LoopbackServerTest {
18191867 ( " test_taskError " , test_taskError) ,
18201868 ( " test_taskCopy " , test_taskCopy) ,
18211869 ( " test_cancelTask " , test_cancelTask) ,
1870+ ( " test_unhandledURLProtocol " , test_unhandledURLProtocol) ,
1871+ ( " test_requestToNilURL " , test_requestToNilURL) ,
18221872 /* ⚠️ */ ( " test_suspendResumeTask " , testExpectedToFail ( test_suspendResumeTask, " Occasionally breaks " ) ) ,
18231873 ( " test_taskTimeout " , test_taskTimeout) ,
18241874 ( " test_verifyRequestHeaders " , test_verifyRequestHeaders) ,
0 commit comments