Created
April 1, 2019 16:37
-
-
Save Andrew-Lees11/a23d37c3ab81aa8ad17125b3766ad601 to your computer and use it in GitHub Desktop.
Example of dataTask producing a double callback on Swift 5 in linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
import Kitura | |
import Foundation | |
final class dataTaskTests: XCTestCase { | |
let router = Router() | |
func testExample() throws { | |
router.get("/", handler: getHandler) | |
Kitura.addHTTPServer(onPort: 8080, with: router) | |
Kitura.start() | |
let expectation1 = expectation(description: "A unauthorized response is received from the server -> .unauthorized") | |
let request = URLRequest(url: URL(string: "http://localhost:8080/")!) | |
let session = URLSession(configuration: URLSessionConfiguration.default) | |
let task = session.dataTask(with: request) { (data, response, error) in | |
print("session.dataTask: \(String(describing: response?.description))") | |
guard error == nil, let response = response as? HTTPURLResponse else { | |
print("Error returned: \(String(describing: error?.localizedDescription))") | |
return XCTFail("Error connecting returned") | |
} | |
let code = response.statusCode | |
if code >= 200 && code < 300 { | |
print("Success code") | |
return XCTFail() | |
} else { | |
print("Failure code") | |
expectation1.fulfill() | |
} | |
} | |
task.resume() | |
waitForExpectations(timeout: 3.0, handler: nil) | |
Kitura.stop() | |
} | |
func getHandler(completion: ([String]?, RequestError?) -> Void) { | |
completion(nil, .unauthorized) | |
} | |
static var allTests = [ | |
("testExample", testExample), | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment