-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: DownloadTaskManager handle redirect authentication #8947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vsarunas
wants to merge
3
commits into
swiftlang:main
Choose a base branch
from
vsarunas:remove-auth-header-on-redirect
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -805,6 +805,78 @@ final class URLSessionHTTPClientTest: XCTestCase { | |||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
func testAsyncDownloadAuthenticateWithRedirectedSuccess() async throws { | ||||||||||||
#if !os(macOS) | ||||||||||||
// URLSession Download tests can only run on macOS | ||||||||||||
// as re-libs-foundation's URLSessionTask implementation which expects the temporaryFileURL property to be on the request. | ||||||||||||
// and there is no way to set it in a mock | ||||||||||||
// https://github.com/apple/swift-corelibs-foundation/pull/2593 tries to address the latter part | ||||||||||||
try XCTSkipIf(true, "test is only supported on macOS") | ||||||||||||
#endif | ||||||||||||
let netrcContent = "machine async-protected.downloader-tests.com login anonymous password qwerty" | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the GitHub case we need to make sure the authentication header is actually removed, so I have adjusted the test case to check for empty header. |
||||||||||||
let netrc = try NetrcAuthorizationWrapper(underlying: NetrcParser.parse(netrcContent)) | ||||||||||||
let authData = Data("anonymous:qwerty".utf8) | ||||||||||||
let testAuthHeader = "Basic \(authData.base64EncodedString())" | ||||||||||||
|
||||||||||||
let configuration = URLSessionConfiguration.default | ||||||||||||
configuration.protocolClasses = [MockURLProtocol.self] | ||||||||||||
let urlSession = URLSessionHTTPClient(configuration: configuration) | ||||||||||||
let httpClient = HTTPClient(implementation: urlSession.execute) | ||||||||||||
|
||||||||||||
try await testWithTemporaryDirectory { temporaryDirectory in | ||||||||||||
let url = URL("https://async-protected.downloader-tests.com/testBasics.zip") | ||||||||||||
let redirectURL = URL("https://cdn-async.downloader-tests.com/testBasics.zip") | ||||||||||||
let destination = temporaryDirectory.appending("download") | ||||||||||||
var options = HTTPClientRequest.Options() | ||||||||||||
options.authorizationProvider = netrc.httpAuthorizationHeader(for:) | ||||||||||||
let request = HTTPClient.Request.download( | ||||||||||||
url: url, | ||||||||||||
options: options, | ||||||||||||
fileSystem: localFileSystem, | ||||||||||||
destination: destination | ||||||||||||
) | ||||||||||||
let redirectRequest = HTTPClient.Request.download( | ||||||||||||
url: redirectURL, | ||||||||||||
options: options, | ||||||||||||
fileSystem: localFileSystem, | ||||||||||||
destination: destination | ||||||||||||
) | ||||||||||||
|
||||||||||||
MockURLProtocol.onRequest(request) { request in | ||||||||||||
XCTAssertEqual(request.allHTTPHeaderFields?["Authorization"], testAuthHeader) | ||||||||||||
MockURLProtocol.sendResponse(statusCode: 302, headers: ["Location": redirectURL.absoluteString], for: request) | ||||||||||||
MockURLProtocol.sendRedirect(for: request, to: URLRequest(url: redirectURL)) | ||||||||||||
} | ||||||||||||
MockURLProtocol.onRequest(redirectRequest) { request in | ||||||||||||
XCTAssertEqual(request.allHTTPHeaderFields?["Authorization"], nil) | ||||||||||||
MockURLProtocol.sendResponse(statusCode: 200, headers: ["Content-Length": "1024"], for: request) | ||||||||||||
MockURLProtocol.sendData(Data(repeating: 0xBE, count: 512), for: request) | ||||||||||||
MockURLProtocol.sendData(Data(repeating: 0xEF, count: 512), for: request) | ||||||||||||
MockURLProtocol.sendCompletion(for: request) | ||||||||||||
} | ||||||||||||
|
||||||||||||
let response = try await httpClient.execute( | ||||||||||||
request, | ||||||||||||
progress: { bytesDownloaded, totalBytesToDownload in | ||||||||||||
switch (bytesDownloaded, totalBytesToDownload) { | ||||||||||||
case (512, 1024): | ||||||||||||
break | ||||||||||||
case (1024, 1024): | ||||||||||||
break | ||||||||||||
default: | ||||||||||||
XCTFail("unexpected progress") | ||||||||||||
} | ||||||||||||
} | ||||||||||||
) | ||||||||||||
|
||||||||||||
XCTAssertEqual(response.statusCode, 200) | ||||||||||||
XCTAssertFileExists(destination) | ||||||||||||
|
||||||||||||
let bytes = ByteString(Array(repeating: 0xBE, count: 512) + Array(repeating: 0xEF, count: 512)) | ||||||||||||
XCTAssertEqual(try! localFileSystem.readFileContents(destination), bytes) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
func testAsyncDownloadDefaultAuthenticationSuccess() async throws { | ||||||||||||
#if !os(macOS) | ||||||||||||
// URLSession Download tests can only run on macOS | ||||||||||||
|
@@ -1055,6 +1127,19 @@ private class MockURLProtocol: URLProtocol { | |||||||||||
request.client?.urlProtocol(request, didFailWithError: error) | ||||||||||||
} | ||||||||||||
|
||||||||||||
static func sendRedirect(for request: URLRequest, to newRequest: URLRequest) { | ||||||||||||
let key = Key(request.httpMethod!, request.url!) | ||||||||||||
self.sendRedirect(newRequest: newRequest, for: key) | ||||||||||||
} | ||||||||||||
|
||||||||||||
private static func sendRedirect(newRequest: URLRequest, for key: Key) { | ||||||||||||
guard let request = self.requests[key] else { | ||||||||||||
return XCTFail("url did not start loading") | ||||||||||||
} | ||||||||||||
let response = HTTPURLResponse(url: key.url, statusCode: 302, httpVersion: "1.1", headerFields: nil)! | ||||||||||||
request.client?.urlProtocol(request, wasRedirectedTo: newRequest, redirectResponse: response) | ||||||||||||
} | ||||||||||||
|
||||||||||||
private struct Key: Hashable { | ||||||||||||
let method: String | ||||||||||||
let url: URL | ||||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Can you add a test case to
Tests/BasicsTests/URLSessionHTTPClientTests.swift
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a draft based on testAsyncDownloadDefaultAuthenticationSuccess(), the 302 request is sent out but the URLSessionHTTPClient is not being executed for me under Xcode.
I don't see how to add an assert or to view the intermediate headers in the test.