Skip to content

Commit

Permalink
refs #4646 allow REST handlers to set custom headers in error respons…
Browse files Browse the repository at this point in the history
…es (#4648)

refs #4647 ensure that deserialization errors in requests result in a 400 Bad Request response
  • Loading branch information
davidnich committed Nov 24, 2022
1 parent 324049d commit 31cea35
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 96 deletions.
16 changes: 13 additions & 3 deletions doxygen/lang/900_release_notes.dox.tmpl
Expand Up @@ -8,13 +8,23 @@
- <a href="../../modules/DebugUtil/html/index.html">DebugUtil</a> module
- fixed bugs in log handling
(<a href="https://github.com/qorelanguage/qore/issues/4635">issue 4635</a>)
- <a href="../../modules/Schema/html/index.html">Schema</a> module
- allow the user to determine if a schema alignment operation was a first-time creation or an update
(<a href="https://github.com/qorelanguage/qore/issues/4637">issue 4637</a>)
- <a href="../../modules/RestHandler/html/index.html">RestHandler</a> module
- implemented the @ref RestHandler::RestHandler::errorResponseHeaders() to allow subclasses to return custom
headers in error responses, such as when a request cannot be deserialized
(<a href="https://github.com/qorelanguage/qore/issues/4646">issue 4646</a>)
- <a href="../../modules/RestSchemaValidator/html/index.html">RestSchemaValidator</a> module
- REST handlers must throw a \c DESERIALIZATION-ERROR when deserialization errors occur, so that a
<tt>400 Bad Request</tt> response is returned
(<a href="https://github.com/qorelanguage/qore/issues/4647">issue 4647</a>)
- allow for the client's time zone locale to be set for data serialization / deserialization
(<a href="https://github.com/qorelanguage/qore/issues/4639">issue 4639</a>)
- <a href="../../modules/Schema/html/index.html">Schema</a> module
- allow the user to determine if a schema alignment operation was a first-time creation or an update
(<a href="https://github.com/qorelanguage/qore/issues/4637">issue 4637</a>)
- <a href="../../modules/Swagger/html/index.html">Swagger</a> module
- REST handlers must throw a \c DESERIALIZATION-ERROR when deserialization errors occur, so that a
<tt>400 Bad Request</tt> response is returned
(<a href="https://github.com/qorelanguage/qore/issues/4647">issue 4647</a>)
- do not reserialize already deserialized data when processing responses
(<a href="https://github.com/qorelanguage/qore/issues/4640">issue 4640</a>)
- allow for the client's time zone locale to be set for data serialization / deserialization
Expand Down
31 changes: 31 additions & 0 deletions examples/test/qlib/RestHandler/RestHandler.qtest
Expand Up @@ -146,6 +146,12 @@ class MyRestHandler inherits RestHandler {
hash<HttpHandlerResponseInfo> getAction(hash<auto> cx, *hash<auto> ah) {
return RestHandler::makeResponse(200, exists ah.action);
}

private *hash<auto> errorResponseHeaders() {
return {
"Error": "True",
};
}
}

class SwaggerRestHandler inherits RestHandler {
Expand Down Expand Up @@ -318,6 +324,7 @@ public class RestHandlerTest inherits QUnit::Test {
}

constructor() : Test("RestHandlerTest", "1.0") {
addTestCase("test bad request", \badRequestTest());
addTestCase("Test direct interface", \directTest());
addTestCase("Test external serialization", \serializationTest());
addTestCase("Test xml", \xmlTest());
Expand Down Expand Up @@ -364,6 +371,30 @@ public class RestHandlerTest inherits QUnit::Test {
delete mServer;
}

badRequestTest() {
HTTPClient client({"url": mClient.getURL()});

hash<auto> info;
on_error printf("info: %N\n", info);

string body = make_yaml(LargerHashValue);
hash<auto> hdr = {
"Content-Type": MimeTypeYaml,
"Accept": MimeTypeYaml,
};
hash<auto> h = client.send(body, "PUT", "test?action=echo", hdr, False, \info);
assertEq(body, h.body);

hdr = {
"Content-Type": MimeTypeJson,
"Accept": MimeTypeJson,
};
assertThrows("HTTP-CLIENT-RECEIVE-ERROR", \client.send(),
("[x x]", "PUT", "test?action=echo", hdr, False, \info));
assertEq(400, info."response-headers".status_code);
assertEq("True", info."response-headers".error);
}

directTest() {
# test direct interface
auto val = mHandler.handleExternalRequest("GET", "test?action=echo", LargerHashValue);
Expand Down

0 comments on commit 31cea35

Please sign in to comment.