Skip to content

Instantly share code, notes, and snippets.

@ncreated
Last active June 10, 2020 11:16
Show Gist options
  • Save ncreated/c62b566368b1c05e1bbf117fe4ede68d to your computer and use it in GitHub Desktop.
Save ncreated/c62b566368b1c05e1bbf117fe4ede68d to your computer and use it in GitHub Desktop.
Replacing URL with URLRequest
import PlaygroundSupport
let url = URL(string: "https://picsum.photos/200/300")!
private extension URLSession {
/// Method under test
func urlRequest(with url: URL) -> URLRequest {
return URLRequest(
url: url,
cachePolicy: configuration.requestCachePolicy,
timeoutInterval: configuration.timeoutIntervalForRequest
)
}
}
func testAgainst(configuration: URLSessionConfiguration) {
let session = URLSession(configuration: configuration)
var taskFromURLRef: URLSessionTask?
var taskFromRequestRef: URLSessionTask?
let taskFromURL = session.dataTask(with: url) { data, response, error in
print("Inspecting request for `.dataTask(with: url)`")
inspectCurrentRequestFor(task: taskFromURLRef!)
}
let request = session.urlRequest(with: url)
let taskFromRequest = session.dataTask(with: request) { data, response, error in
print("Inspecting request for `.dataTask(with: request)`")
inspectCurrentRequestFor(task: taskFromRequestRef!)
}
taskFromURLRef = taskFromURL
taskFromRequestRef = taskFromRequest
taskFromURL.resume()
taskFromRequest.resume()
sleep(4) // wait for requests completion
}
func inspectCurrentRequestFor(task: URLSessionTask) {
print(" → allHTTPHeaderFields:", task.currentRequest!.allHTTPHeaderFields!.sorted(by: <).map { k, v in "\(k): \(v)" })
print(" → networkServiceType:", task.currentRequest!.networkServiceType.rawValue)
print(" → allowsCellularAccess:", task.currentRequest!.allowsCellularAccess)
print(" → timeoutInterval", task.currentRequest!.timeoutInterval)
print(" → httpShouldHandleCookies", task.currentRequest!.httpShouldHandleCookies)
}
testAgainst(configuration: {
let configuration = URLSessionConfiguration.ephemeral
configuration.httpAdditionalHeaders = ["foo": "bar"]
configuration.networkServiceType = .avStreaming
configuration.allowsCellularAccess = false
configuration.timeoutIntervalForRequest = 13
configuration.timeoutIntervalForResource = 11
configuration.httpCookieAcceptPolicy = .always
configuration.httpShouldSetCookies = true
return configuration
}())
PlaygroundPage.current.needsIndefiniteExecution = true
@ncreated
Copy link
Author

Output:

Inspecting request for `.dataTask(with: url)`
 → allHTTPHeaderFields: ["Accept: */*", "Accept-Encoding: gzip, deflate, br", "Accept-Language: en-us", "foo: bar"]
 → networkServiceType: 0
 → allowsCellularAccess: true
 → timeoutInterval 60.0
 → httpShouldHandleCookies true
Inspecting request for `.dataTask(with: request)`
 → allHTTPHeaderFields: ["Accept: */*", "Accept-Encoding: gzip, deflate, br", "Accept-Language: en-us", "foo: bar"]
 → networkServiceType: 0
 → allowsCellularAccess: true
 → timeoutInterval 13.0 ⚠️
 → httpShouldHandleCookies true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment