Skip to content

Commit 7d8bbf0

Browse files
authored
Merge pull request #1486 from nethraravindran/http-redirection
2 parents a539033 + 72f4ec5 commit 7d8bbf0

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

Foundation/URLSession/http/HTTPURLProtocol.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,13 @@ internal extension _HTTPURLProtocol {
402402
var components = URLComponents()
403403
components.scheme = scheme
404404
components.host = host
405-
components.path = targetURL.relativeString
405+
//The path must either begin with "/" or be an empty string.
406+
if targetURL.relativeString.first != "/" {
407+
components.path = "/" + targetURL.relativeString
408+
} else {
409+
components.path = targetURL.relativeString
410+
}
411+
406412
guard let urlString = components.string else { fatalError("Invalid URL") }
407413
request.url = URL(string: urlString)
408414
let timeSpent = easyHandle.getTimeoutIntervalSpent()

TestFoundation/HTTPServer.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ public class TestURLSessionServer {
337337
"Italy": "Rome",
338338
"USA": "Washington, D.C.",
339339
"UnitedStates": "USA",
340+
"UnitedKingdom": "UK",
341+
"UK": "London",
340342
"country.txt": "A country is a region that is identified as a distinct national entity in political geography"]
341343
let httpServer: _HTTPServer
342344
let startDelay: TimeInterval?
@@ -432,6 +434,13 @@ public class TestURLSessionServer {
432434
return _HTTPResponse(response: .OK, body: dtd)
433435
}
434436

437+
if uri == "/UnitedKingdom" {
438+
let value = capitals[String(uri.dropFirst())]!
439+
let text = request.getCommaSeparatedHeaders()
440+
//Response header with only path to the location to redirect.
441+
let httpResponse = _HTTPResponse(response: .REDIRECT, headers: "Location: \(value)", body: text)
442+
return httpResponse
443+
}
435444
return _HTTPResponse(response: .OK, body: capitals[String(uri.dropFirst())]!)
436445
}
437446

TestFoundation/TestURLSession.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class TestURLSession : LoopbackServerTest {
3535
("test_verifyRequestHeaders", test_verifyRequestHeaders),
3636
("test_verifyHttpAdditionalHeaders", test_verifyHttpAdditionalHeaders),
3737
("test_timeoutInterval", test_timeoutInterval),
38-
("test_httpRedirection", test_httpRedirection),
38+
("test_httpRedirectionWithCompleteRelativePath", test_httpRedirectionWithCompleteRelativePath),
39+
//("test_httpRedirectionWithInCompleteRelativePath", test_httpRedirectionWithInCompleteRelativePath), /* temporarily disabled. Needs HTTPServer rework */
3940
//("test_httpRedirectionTimeout", test_httpRedirectionTimeout), /* temporarily disabled (https://bugs.swift.org/browse/SR-5751) */
4041
("test_http0_9SimpleResponses", test_http0_9SimpleResponses),
4142
("test_outOfRangeButCorrectlyFormattedHTTPCode", test_outOfRangeButCorrectlyFormattedHTTPCode),
@@ -325,14 +326,22 @@ class TestURLSession : LoopbackServerTest {
325326
waitForExpectations(timeout: 30)
326327
}
327328

328-
func test_httpRedirection() {
329+
func test_httpRedirectionWithCompleteRelativePath() {
329330
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedStates"
330331
let url = URL(string: urlString)!
331332
let d = HTTPRedirectionDataTask(with: expectation(description: "GET \(urlString): with HTTP redirection"))
332333
d.run(with: url)
333334
waitForExpectations(timeout: 12)
334335
}
335336

337+
func test_httpRedirectionWithInCompleteRelativePath() {
338+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedKingdom"
339+
let url = URL(string: urlString)!
340+
let d = HTTPRedirectionDataTask(with: expectation(description: "GET \(urlString): with HTTP redirection"))
341+
d.run(with: url)
342+
waitForExpectations(timeout: 12)
343+
}
344+
336345
/*
337346
// temporarily disabled (https://bugs.swift.org/browse/SR-5751)
338347
func test_httpRedirectionTimeout() {

0 commit comments

Comments
 (0)