Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ if let environmentPath = Context.environment["CURL_INCLUDE_PATH"] {

var curlLinkFlags: [LinkerSetting] = [
.linkedLibrary("libcurl.lib", .when(platforms: [.windows])),
.linkedLibrary("zlibstatic.lib", .when(platforms: [.windows]))
.linkedLibrary("zlibstatic.lib", .when(platforms: [.windows])),
.linkedLibrary("brotlicommon.lib", .when(platforms: [.windows])),
.linkedLibrary("brotlidec.lib", .when(platforms: [.windows]))
]
if let environmentPath = Context.environment["CURL_LIBRARY_PATH"] {
curlLinkFlags.append(.unsafeFlags([
Expand All @@ -70,6 +72,11 @@ if let environmentPath = Context.environment["ZLIB_LIBRARY_PATH"] {
"-L\(environmentPath)"
]))
}
if let environmentPath = Context.environment["BROTLI_LIBRARY_PATH"] {
curlLinkFlags.append(.unsafeFlags([
"-L\(environmentPath)"
]))
}

var libxmlLinkFlags: [LinkerSetting] = [
.linkedLibrary("libxml2s.lib", .when(platforms: [.windows]))
Expand Down
24 changes: 24 additions & 0 deletions Tests/Foundation/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,18 @@ class _HTTPServer: CustomStringConvertible {
"\r\n").data(using: .utf8)!
try tcpSocket.writeRawData(responseData)
}

func respondWithAcceptEncoding(request: _HTTPRequest) throws {
var responseData: Data
if let acceptEncoding = request.getHeader(for: "Accept-Encoding") {
let content = acceptEncoding.data(using: .utf8)!
responseData = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=ISO-8859-1\r\nContent-Length: \(content.count)\r\n\r\n".data(using: .utf8)!
responseData.append(content)
} else {
responseData = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=ISO-8859-1\r\nContent-Length: 0\r\n\r\n".data(using: .utf8)!
}
try tcpSocket.writeRawData(responseData)
}
}

struct _HTTPRequest: CustomStringConvertible {
Expand Down Expand Up @@ -690,6 +702,8 @@ public class TestURLSessionServer: CustomStringConvertible {
try httpServer.respondWithUnauthorizedHeader()
} else if req.uri.hasPrefix("/web-socket") {
try handleWebSocketRequest(req)
} else if req.uri.hasPrefix("/accept-encoding") {
try httpServer.respondWithAcceptEncoding(request: req)
} else {
let response = try getResponse(request: req)
try httpServer.respond(with: response)
Expand Down Expand Up @@ -852,6 +866,16 @@ public class TestURLSessionServer: CustomStringConvertible {
"Content-Encoding: gzip"].joined(separator: _HTTPUtils.CRLF),
bodyData: helloWorld)
}

if uri == "/brotli-response" {
// This is "Hello World!" brotli encoded.
let helloWorld = Data([0x8B, 0x05, 0x80, 0x48, 0x65, 0x6C, 0x6C, 0x6F,
0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21, 0x03])
return _HTTPResponse(response: .OK,
headers: ["Content-Length: \(helloWorld.count)",
"Content-Encoding: br"].joined(separator: _HTTPUtils.CRLF),
bodyData: helloWorld)
}

if uri == "/echo-query" {
let body = request.parameters.map { "\($0.key)=\($0.value)" }.joined(separator: "&")
Expand Down
23 changes: 23 additions & 0 deletions Tests/Foundation/TestURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ final class TestURLSession: LoopbackServerTest, @unchecked Sendable {
}
}

func test_dataTaskWithAcceptEncoding() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/accept-encoding"
let url = URL(string: urlString)!
let d = DataTask(with: expectation(description: "GET \(urlString): with a delegate"))
d.run(with: url)
waitForExpectations(timeout: 12)
if !d.error {
let supportedEncodings = d.capital.split(separator: ",").map { $0.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines ) }
XCTAssert(supportedEncodings.contains("br"), "test_dataTaskWithURLRequest returned an unexpected result")
}
}

func test_dataTaskWithURLCompletionHandler() async {
//shared session
await dataTaskWithURLCompletionHandler(with: URLSession.shared)
Expand Down Expand Up @@ -256,6 +268,17 @@ final class TestURLSession: LoopbackServerTest, @unchecked Sendable {
}
}

func test_brotliDataTask() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/brotli-response"
let url = URL(string: urlString)!
let d = DataTask(with: expectation(description: "GET \(urlString): brotli response"))
d.run(with: url)
waitForExpectations(timeout: 12)
if !d.error {
XCTAssertEqual(d.capital, "Hello World!")
}
}

func test_downloadTaskWithURL() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
let url = URL(string: urlString)!
Expand Down