This commit is contained in:
Karl Seguin
2026-03-22 12:47:33 +08:00
parent 4e2e895cd9
commit e3085cb0f1

View File

@@ -514,17 +514,16 @@ fn buildJSONVersionResponse(
pub const timestamp = @import("datetime.zig").timestamp; pub const timestamp = @import("datetime.zig").timestamp;
pub const milliTimestamp = @import("datetime.zig").milliTimestamp; pub const milliTimestamp = @import("datetime.zig").milliTimestamp;
const testing = std.testing; const testing = @import("testing.zig");
test "server: buildJSONVersionResponse" { test "server: buildJSONVersionResponse" {
const address = try net.Address.parseIp4("127.0.0.1", 9001); const res = try buildJSONVersionResponse(testing.test_app);
const res = try buildJSONVersionResponse(testing.allocator, address); defer testing.test_app.allocator.free(res);
defer testing.allocator.free(res);
try testing.expectEqualStrings("HTTP/1.1 200 OK\r\n" ++ try testing.expectEqual("HTTP/1.1 200 OK\r\n" ++
"Content-Length: 48\r\n" ++ "Content-Length: 48\r\n" ++
"Connection: Close\r\n" ++ "Connection: Close\r\n" ++
"Content-Type: application/json; charset=UTF-8\r\n\r\n" ++ "Content-Type: application/json; charset=UTF-8\r\n\r\n" ++
"{\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9001/\"}", res); "{\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9222/\"}", res);
} }
test "Client: http invalid request" { test "Client: http invalid request" {
@@ -532,7 +531,7 @@ test "Client: http invalid request" {
defer c.deinit(); defer c.deinit();
const res = try c.httpRequest("GET /over/9000 HTTP/1.1\r\n" ++ "Header: " ++ ("a" ** 4100) ++ "\r\n\r\n"); const res = try c.httpRequest("GET /over/9000 HTTP/1.1\r\n" ++ "Header: " ++ ("a" ** 4100) ++ "\r\n\r\n");
try testing.expectEqualStrings("HTTP/1.1 413 \r\n" ++ try testing.expectEqual("HTTP/1.1 413 \r\n" ++
"Connection: Close\r\n" ++ "Connection: Close\r\n" ++
"Content-Length: 17\r\n\r\n" ++ "Content-Length: 17\r\n\r\n" ++
"Request too large", res); "Request too large", res);
@@ -601,7 +600,7 @@ test "Client: http valid handshake" {
"Custom: Header-Value\r\n\r\n"; "Custom: Header-Value\r\n\r\n";
const res = try c.httpRequest(request); const res = try c.httpRequest(request);
try testing.expectEqualStrings("HTTP/1.1 101 Switching Protocols\r\n" ++ try testing.expectEqual("HTTP/1.1 101 Switching Protocols\r\n" ++
"Upgrade: websocket\r\n" ++ "Upgrade: websocket\r\n" ++
"Connection: upgrade\r\n" ++ "Connection: upgrade\r\n" ++
"Sec-Websocket-Accept: flzHu2DevQ2dSCSVqKSii5e9C2o=\r\n\r\n", res); "Sec-Websocket-Accept: flzHu2DevQ2dSCSVqKSii5e9C2o=\r\n\r\n", res);
@@ -729,7 +728,7 @@ test "server: 404" {
defer c.deinit(); defer c.deinit();
const res = try c.httpRequest("GET /unknown HTTP/1.1\r\n\r\n"); const res = try c.httpRequest("GET /unknown HTTP/1.1\r\n\r\n");
try testing.expectEqualStrings("HTTP/1.1 404 \r\n" ++ try testing.expectEqual("HTTP/1.1 404 \r\n" ++
"Connection: Close\r\n" ++ "Connection: Close\r\n" ++
"Content-Length: 9\r\n\r\n" ++ "Content-Length: 9\r\n\r\n" ++
"Not found", res); "Not found", res);
@@ -741,7 +740,7 @@ test "server: get /json/version" {
"Content-Length: 48\r\n" ++ "Content-Length: 48\r\n" ++
"Connection: Close\r\n" ++ "Connection: Close\r\n" ++
"Content-Type: application/json; charset=UTF-8\r\n\r\n" ++ "Content-Type: application/json; charset=UTF-8\r\n\r\n" ++
"{\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9583/\"}"; "{\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9222/\"}";
{ {
// twice on the same connection // twice on the same connection
@@ -749,7 +748,7 @@ test "server: get /json/version" {
defer c.deinit(); defer c.deinit();
const res1 = try c.httpRequest("GET /json/version HTTP/1.1\r\n\r\n"); const res1 = try c.httpRequest("GET /json/version HTTP/1.1\r\n\r\n");
try testing.expectEqualStrings(expected_response, res1); try testing.expectEqual(expected_response, res1);
} }
{ {
@@ -758,7 +757,7 @@ test "server: get /json/version" {
defer c.deinit(); defer c.deinit();
const res1 = try c.httpRequest("GET /json/version HTTP/1.1\r\n\r\n"); const res1 = try c.httpRequest("GET /json/version HTTP/1.1\r\n\r\n");
try testing.expectEqualStrings(expected_response, res1); try testing.expectEqual(expected_response, res1);
} }
} }
@@ -776,7 +775,7 @@ fn assertHTTPError(
.{ expected_status, expected_body.len, expected_body }, .{ expected_status, expected_body.len, expected_body },
); );
try testing.expectEqualStrings(expected_response, res); try testing.expectEqual(expected_response, res);
} }
fn assertWebSocketError(close_code: u16, input: []const u8) !void { fn assertWebSocketError(close_code: u16, input: []const u8) !void {
@@ -920,7 +919,7 @@ const TestClient = struct {
"Custom: Header-Value\r\n\r\n"; "Custom: Header-Value\r\n\r\n";
const res = try self.httpRequest(request); const res = try self.httpRequest(request);
try testing.expectEqualStrings("HTTP/1.1 101 Switching Protocols\r\n" ++ try testing.expectEqual("HTTP/1.1 101 Switching Protocols\r\n" ++
"Upgrade: websocket\r\n" ++ "Upgrade: websocket\r\n" ++
"Connection: upgrade\r\n" ++ "Connection: upgrade\r\n" ++
"Sec-Websocket-Accept: flzHu2DevQ2dSCSVqKSii5e9C2o=\r\n\r\n", res); "Sec-Websocket-Accept: flzHu2DevQ2dSCSVqKSii5e9C2o=\r\n\r\n", res);