Fixed unit tests.
This commit is contained in:
parent
49d0ebfed0
commit
a02d1e4dec
2
dub.json
2
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"
|
||||
},
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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];
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue