Skip to content

Commit f0fbde1

Browse files
committed
Add basic tests for brotli support
1 parent 6799142 commit f0fbde1

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

Tests/Foundation/HTTPServer.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,18 @@ class _HTTPServer: CustomStringConvertible {
481481
"\r\n").data(using: .utf8)!
482482
try tcpSocket.writeRawData(responseData)
483483
}
484+
485+
func respondWithAcceptEncoding(request: _HTTPRequest) throws {
486+
var responseData: Data
487+
if let acceptEncoding = request.getHeader(for: "Accept-Encoding") {
488+
let content = acceptEncoding.data(using: .utf8)!
489+
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)!
490+
responseData.append(content)
491+
} else {
492+
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)!
493+
}
494+
try tcpSocket.writeRawData(responseData)
495+
}
484496
}
485497

486498
struct _HTTPRequest: CustomStringConvertible {
@@ -690,6 +702,8 @@ public class TestURLSessionServer: CustomStringConvertible {
690702
try httpServer.respondWithUnauthorizedHeader()
691703
} else if req.uri.hasPrefix("/web-socket") {
692704
try handleWebSocketRequest(req)
705+
} else if req.uri.hasPrefix("/accept-encoding") {
706+
try httpServer.respondWithAcceptEncoding(request: req)
693707
} else {
694708
let response = try getResponse(request: req)
695709
try httpServer.respond(with: response)
@@ -852,6 +866,16 @@ public class TestURLSessionServer: CustomStringConvertible {
852866
"Content-Encoding: gzip"].joined(separator: _HTTPUtils.CRLF),
853867
bodyData: helloWorld)
854868
}
869+
870+
if uri == "/brotli-response" {
871+
// This is "Hello World!" brotli encoded.
872+
let helloWorld = Data([0x8B, 0x05, 0x80, 0x48, 0x65, 0x6C, 0x6C, 0x6F,
873+
0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21, 0x03])
874+
return _HTTPResponse(response: .OK,
875+
headers: ["Content-Length: \(helloWorld.count)",
876+
"Content-Encoding: br"].joined(separator: _HTTPUtils.CRLF),
877+
bodyData: helloWorld)
878+
}
855879

856880
if uri == "/echo-query" {
857881
let body = request.parameters.map { "\($0.key)=\($0.value)" }.joined(separator: "&")

Tests/Foundation/TestURLSession.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ final class TestURLSession: LoopbackServerTest, @unchecked Sendable {
3131
}
3232
}
3333

34+
func test_dataTaskWithAcceptEncoding() async {
35+
#if !os(Windows)
36+
throw XCTSkip("This test is currently only enabled on Windows")
37+
#else
38+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/accept-encoding"
39+
let url = URL(string: urlString)!
40+
let d = DataTask(with: expectation(description: "GET \(urlString): with a delegate"))
41+
d.run(with: url)
42+
waitForExpectations(timeout: 12)
43+
if !d.error {
44+
XCTAssertEqual(d.capital, "deflate, gzip, br", "test_dataTaskWithURLRequest returned an unexpected result")
45+
}
46+
#endif
47+
}
48+
3449
func test_dataTaskWithURLCompletionHandler() async {
3550
//shared session
3651
await dataTaskWithURLCompletionHandler(with: URLSession.shared)
@@ -256,6 +271,21 @@ final class TestURLSession: LoopbackServerTest, @unchecked Sendable {
256271
}
257272
}
258273

274+
func test_brotliDataTask() async {
275+
#if !os(Windows)
276+
throw XCTSkip("This test is currently only enabled on Windows")
277+
#else
278+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/brotli-response"
279+
let url = URL(string: urlString)!
280+
let d = DataTask(with: expectation(description: "GET \(urlString): brotli response"))
281+
d.run(with: url)
282+
waitForExpectations(timeout: 12)
283+
if !d.error {
284+
XCTAssertEqual(d.capital, "Hello World!")
285+
}
286+
#endif
287+
}
288+
259289
func test_downloadTaskWithURL() async {
260290
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
261291
let url = URL(string: urlString)!

0 commit comments

Comments
 (0)