From 98d379925e8f537016b56c06c0a86e55bea06009 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Thu, 22 May 2025 18:59:38 -0400 Subject: [PATCH] Added tests, increased primitives version. --- README.md | 4 ++-- dub.json | 2 +- dub.selections.json | 4 ++-- source/handy_http_data/json.d | 31 ++++++++++++++++++++++++++++++- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5817702..c8a32f9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# data +# Handy-Http Data -Support for common data formats and database operations. \ No newline at end of file +Support for common data formats and database operations. diff --git a/dub.json b/dub.json index d0a4428..e985380 100644 --- a/dub.json +++ b/dub.json @@ -6,7 +6,7 @@ "dependencies": { "asdf": "~>0.7", "dxml": "~>0.4", - "handy-http-primitives": "~>1.4" + "handy-http-primitives": "~>1.6" }, "description": "Support for common data formats and database operations.", "license": "CC0", diff --git a/dub.selections.json b/dub.selections.json index 8b8e8a5..14bdc49 100644 --- a/dub.selections.json +++ b/dub.selections.json @@ -3,10 +3,10 @@ "versions": { "asdf": "0.7.17", "dxml": "0.4.4", - "handy-http-primitives": "1.4.0", + "handy-http-primitives": "1.6.0", "mir-algorithm": "3.22.3", "mir-core": "1.7.1", "silly": "1.1.1", - "streams": "3.5.0" + "streams": "3.6.0" } } diff --git a/source/handy_http_data/json.d b/source/handy_http_data/json.d index 336ea9c..4e172c0 100644 --- a/source/handy_http_data/json.d +++ b/source/handy_http_data/json.d @@ -26,6 +26,19 @@ T readJsonBodyAs(T)(ref ServerHttpRequest request) { } } +unittest { + import handy_http_primitives.builder; + ServerHttpRequest request = ServerHttpRequestBuilder() + .withBody("{\"key\": \"value\"}") + .withHeader("Content-Length", "16") + .build(); + struct TestStruct { + string key; + } + TestStruct testStruct = readJsonBodyAs!TestStruct(request); + assert(testStruct.key == "value"); +} + /** * Writes a JSON value to the body of the given HTTP response, serializing it * using the ASDF library. Will also set Content-Type and Content-Length @@ -57,9 +70,25 @@ void writeJsonBody(T)(ref ServerHttpResponse response, in T bodyContent) { StreamResult result = response.outputStream.writeToStream(cast(ubyte[]) responseBody); if (result.hasError) { StreamError err = result.error; - throw new HttpStatusException(HttpStatus.INTERNAL_SERVER_ERROR, err.message); + throw new HttpStatusException(HttpStatus.INTERNAL_SERVER_ERROR, cast(string) err.message); } } catch (SerdeException e) { throw new HttpStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.msg, e); } } + +unittest { + import handy_http_primitives.builder; + ArrayOutputStream!ubyte outputStream = byteArrayOutputStream(); + ServerHttpResponse response = ServerHttpResponseBuilder() + .withOutputStream(&outputStream) + .build(); + struct TestStruct { + string key; + } + TestStruct testStruct = TestStruct("value"); + writeJsonBody(response, testStruct); + assert(response.headers["Content-Type"] == "application/json"); + const writtenBody = cast(string) outputStream.toArrayRaw(); + assert(writtenBody == "{\"key\":\"value\"}"); +}