From a02d1e4dec7191212db2548ae57c1404d7a174a8 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Sun, 22 Jun 2025 22:38:04 -0400 Subject: [PATCH] Fixed unit tests. --- dub.json | 2 +- dub.selections.json | 4 ++-- source/handy_http_transport/helpers.d | 7 ++----- source/handy_http_transport/http1/transport.d | 19 +++++++++++++------ 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/dub.json b/dub.json index 3511c9d..fc43121 100644 --- a/dub.json +++ b/dub.json @@ -5,7 +5,7 @@ "copyright": "Copyright © 2024, Andrew Lalis", "dependencies": { "handy-http-primitives": "~>1.6", - "photon": "~>0.10", + "photon": "~>0.11", "streams": "~>3.6", "slf4d": "~>4.0" }, diff --git a/dub.selections.json b/dub.selections.json index ba24159..f3f1369 100644 --- a/dub.selections.json +++ b/dub.selections.json @@ -2,9 +2,9 @@ "fileVersion": 1, "versions": { "handy-http-primitives": "1.6.0", - "photon": "0.10.2", + "photon": "0.11.0", "sharded-map": "2.7.0", - "slf4d": "4.0.0", + "slf4d": "4.0.1", "streams": "3.6.0" } } diff --git a/source/handy_http_transport/helpers.d b/source/handy_http_transport/helpers.d index 5203a33..1702745 100644 --- a/source/handy_http_transport/helpers.d +++ b/source/handy_http_transport/helpers.d @@ -14,8 +14,6 @@ Either!(string, "value", StreamError, "error") consumeUntil(S)( S inputStream, string target ) if (isByteInputStream!S) { - import slf4d; - // info("consumeUntil called."); ubyte[1024] buffer; size_t idx; while (true) { @@ -29,8 +27,7 @@ Either!(string, "value", StreamError, "error") consumeUntil(S)( return Either!(string, "value", StreamError, "error")( cast(string) buffer[0 .. idx - target.length].idup ); - } - if (idx >= buffer.length) { + } else if (idx >= buffer.length) { return Either!(string, "value", StreamError, "error")( StreamError("Couldn't find target \"" ~ target ~ "\" after reading 1024 bytes.", 1) ); @@ -66,7 +63,7 @@ string stripSpaces(string s) { while (startIdx < s.length && s[startIdx] == ' ') startIdx++; s = s[startIdx .. $]; if (s.length == 0) return ""; - ptrdiff_t endIdx = s.length - 1; + ptrdiff_t endIdx = cast(ptrdiff_t) s.length - 1; while (s[endIdx] == ' ' && endIdx >= 0) endIdx--; return s[0 .. endIdx + 1]; } diff --git a/source/handy_http_transport/http1/transport.d b/source/handy_http_transport/http1/transport.d index 8dbadfd..176cea2 100644 --- a/source/handy_http_transport/http1/transport.d +++ b/source/handy_http_transport/http1/transport.d @@ -291,7 +291,9 @@ unittest { * Parses HTTP headers from an input stream, and returns them as an associative * array mapping header names to their list of values. * Params: - * inputStream = The byte input stream to read from. + * inputStream = The byte input stream to read from. Note that this stream + * should be passed as a pointer / reference, values will be + * consumed from the stream. * Returns: Either the headers, or a stream error. */ Either!(string[][string], "headers", StreamError, "error") parseHeaders(S)(S inputStream) if (isByteInputStream!S) { @@ -325,28 +327,33 @@ unittest { } // Basic valid headers. - auto r1 = parseHeaders(makeStream("Content-Type: application/json\r\n\r\n")); + auto s1 = makeStream("Content-Type: application/json\r\n\r\n"); + auto r1 = parseHeaders(&s1); assert(r1.hasHeaders); assert("Content-Type" in r1.headers); assert(r1.headers["Content-Type"] == ["application/json"]); // Multiple headers. - auto r2 = parseHeaders(makeStream("Accept: text, json, image\r\nContent-Length: 1234\r\n\r\n")); + auto s2 = makeStream("Accept: text, json, image\r\nContent-Length: 1234\r\n\r\n"); + auto r2 = parseHeaders(&s2); assert(r2.hasHeaders); assert("Accept" in r2.headers); assert(r2.headers["Accept"] == ["text, json, image"]); assert(r2.headers["Content-Length"] == ["1234"]); // Basic invalid header string. - auto r3 = parseHeaders(makeStream("Invalid headers")); + auto s3 = makeStream("Invalid headers"); + auto r3 = parseHeaders(&s3); assert(r3.hasError); // No trailing \r\n - auto r4 = parseHeaders(makeStream("Content-Type: application/json")); + auto s4 = makeStream("Content-Type: application/json"); + auto r4 = parseHeaders(&s4); assert(r4.hasError); // Empty headers. - auto r5 = parseHeaders(makeStream("\r\n")); + auto s5 = makeStream("\r\n"); + auto r5 = parseHeaders(&s5); assert(r5.hasHeaders); assert(r5.headers.length == 0); }