@@ -1058,7 +1058,7 @@ test "cursor: match path" {
1058
1058
}
1059
1059
}
1060
1060
1061
- test "parse request" {
1061
+ test parseRequest {
1062
1062
const buffer : []const u8 = "OPTIONS /hey-this-is-kinda-long-path HTTP/1.1\r \n Host: localhost\r \n Connection: close\r \n \r \n " ;
1063
1063
1064
1064
var method : Method = .unknown ;
@@ -1081,54 +1081,25 @@ test "parse request" {
1081
1081
try testing .expectEqualStrings ("close" , headers [1 ].value );
1082
1082
}
1083
1083
1084
- //test parseRequest {
1085
- //const buffer: []const u8 = "TRACE /cookies HTTP/1.1\r\nHost: asdjqwdkwfj\r\nConnection: keep-alive\r\n\r\n";
1086
- //
1087
- //var method: Method = .unknown;
1088
- //var path: ?[]const u8 = null;
1089
- //var http_version: Version = .@"1.0";
1090
- //var headers: [3]Header = undefined;
1091
- //var header_count: usize = 0;
1092
- //
1093
- //const len = parseRequest(buffer[0..], &method, &path, &http_version, &headers, &header_count) catch |err| switch (err) {
1094
- // error.Incomplete => @panic("need more bytes"),
1095
- // error.Invalid => @panic("invalid!"),
1096
- //};
1097
- //
1098
- //std.debug.print("{}\t{}\n", .{ method, http_version });
1099
- //std.debug.print("path: {s}\n", .{path.?});
1100
- //
1101
- //for (headers[0..header_count]) |header| {
1102
- // std.debug.print("{s}\t{s}\n", .{ header.key, header.value });
1103
- //}
1104
- //
1105
- //std.debug.print("len: {any}\n", .{len});
1106
-
1107
- //var tokens: [256]u1 = std.mem.zeroes([256]u1);
1108
- //@memset(&tokens, 1);
1109
- //
1110
- //tokens[58] = 0;
1111
- //tokens[127] = 0;
1112
- //
1113
- //for (0..32) |i| {
1114
- // tokens[i] = 0;
1115
- //}
1116
- //
1117
- //std.debug.print("{any}\n", .{tokens});
1118
-
1119
- //const min: @Vector(8, u8) = @splat('A' - 1);
1120
- //const max: @Vector(8, u8) = @splat('Z');
1121
- //
1122
- //const chunk: @Vector(8, u8) = "tEsTINgG".*;
1123
- //
1124
- //const bits = @intFromBool(chunk <= max) & @intFromBool(chunk > min);
1125
- //var res: u8 = @bitCast(bits);
1126
- //
1127
- //while (res != 0) {
1128
- // const t = res & -%res;
1129
- // defer res ^= t;
1130
- //
1131
- // const idx = @ctz(t);
1132
- // std.debug.print("{c}\n", .{chunk[idx]});
1133
- //}
1134
- //}
1084
+ test parseResponse {
1085
+ const buffer = "HTTP/1.1 418 I'm a teapot\r \n Host: localhost\r \n Some-Number-Sequence: 123291429\r \n \r \n " ;
1086
+
1087
+ var version : Version = .@"1.0" ;
1088
+ var status_code : u16 = 0 ;
1089
+ var status_msg : ? []const u8 = null ;
1090
+ var headers : [2 ]Header = undefined ;
1091
+ var header_count : usize = 0 ;
1092
+
1093
+ const len = try parseResponse (buffer [0.. ], & version , & status_code , & status_msg , & headers , & header_count );
1094
+
1095
+ try testing .expect (buffer .len == len );
1096
+ try testing .expect (version == .@"1.1" );
1097
+ try testing .expect (status_code == 418 );
1098
+ try testing .expect (status_msg != null );
1099
+ try testing .expectEqualStrings ("I'm a teapot" , status_msg .? );
1100
+ try testing .expect (header_count == 2 );
1101
+ try testing .expectEqualStrings ("Host" , headers [0 ].key );
1102
+ try testing .expectEqualStrings ("localhost" , headers [0 ].value );
1103
+ try testing .expectEqualStrings ("Some-Number-Sequence" , headers [1 ].key );
1104
+ try testing .expectEqualStrings ("123291429" , headers [1 ].value );
1105
+ }
0 commit comments