I see this happening in other threads but none of those solutions have worked for me. I'm new to Swift (my apologies) and attempting a simple test to get a string response from a service I have running on my machine. (This service show results from a browser and Postman so I'm sure it's working.)
This is my code:
let url = URL(string: "http://127.0.0.1:8080/stuff/ping")!
var request = URLRequest(url: url)
request.httpMethod = "GET"
print("creating ping task...")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
print("processing ping result...")
if let data = data {
let str = String(decoding: data, as: UTF8.self)
print(str)
} else if let error = error {
print("HTTP Request Failed \(error)")
}
}
print("resuming ping task...")
task.resume()
When I run that in a playground, it works! The console shows:
creating ping task....
resuming ping task...
processing ping result...
One ping only, Vasily
But from within a XCTest, it does not:
Test Suite 'NetworkTests' started at 2023-06-02 08:24:46.007
Test Case '-[StuffTests.NetworkTests testPing]' started.
creating ping task...
resuming ping task...
Test Case '-[StuffTests.NetworkTests testPing]' passed (0.002 seconds).
Test Suite 'NetworkTests' passed at 2023-06-02 08:24:46.010.
Executed 1 test, with 0 failures (0 unexpected) in 0.002 (0.002) seconds
I've set the info of the project according to what I've seen in other posts so that it can access a local host (?):
I appeal to you for help in understanding and fixing this. thank you!