base1: Add http-stream2 unit test for non-default method

Our own HTTP server only accepts HEAD and GET, so let's test `HEAD`. Add
a /mock/headonly path which refuses GET, and check the `method: HEAD`
channel option in test-http.
This commit is contained in:
Martin Pitt 2022-08-04 17:15:00 +02:00 committed by Martin Pitt
parent 47d5389039
commit 07d125d8fa
2 changed files with 53 additions and 0 deletions

View File

@ -306,6 +306,31 @@ QUnit.test("address with params", assert => {
.finally(done);
});
QUnit.test("HEAD method", assert => {
const done = assert.async();
assert.expect(4);
assert.rejects(
cockpit.http(test_server).get("/mock/headonly"),
ex => ex.status == 400 && ex.reason == "Only HEAD allowed on this path",
"rejects GET request on /headonly path");
const InputData = "some chars";
cockpit.http(test_server).request({
path: "/mock/headonly",
method: "HEAD",
headers: { InputData },
body: "",
})
.response((status, headers) => {
assert.equal(status, 200);
assert.equal(headers.InputDataLength, InputData.length);
})
.then(data => assert.equal(data, ""))
.finally(done);
});
QUnit.test("wrong options", assert => {
assert.rejects(
cockpit.http({}).get("/"),

View File

@ -234,6 +234,32 @@ mock_http_connection (CockpitWebResponse *response)
return TRUE;
}
static gboolean
mock_http_headonly (CockpitWebRequest *request,
CockpitWebResponse *response)
{
if (!g_str_equal (cockpit_web_request_get_method (request), "HEAD"))
{
cockpit_web_response_error (response, 400, NULL, "Only HEAD allowed on this path");
}
else
{
const char *input_data = cockpit_web_request_lookup_header (request, "InputData");
if (!input_data)
{
cockpit_web_response_error (response, 400, NULL, "Requires InputData header");
return TRUE;
}
g_autoptr(GHashTable) headers = cockpit_web_server_new_table();
g_hash_table_insert (headers, g_strdup ("InputDataLength"), g_strdup_printf ("%zu", strlen (input_data)));
cockpit_web_response_headers_full (response, 200, "OK", -1, headers);
cockpit_web_response_complete (response);
}
return TRUE;
}
static gboolean
mock_http_expect_warnings (CockpitWebResponse *response,
GLogLevelFlags warnings)
@ -267,6 +293,8 @@ on_handle_mock (CockpitWebServer *server,
return mock_http_host (response, headers);
if (g_str_equal (path, "/connection"))
return mock_http_connection (response);
if (g_str_equal (path, "/headonly"))
return mock_http_headonly (request, response);
if (g_str_equal (path, "/expect-warnings"))
return mock_http_expect_warnings (response, 0);
if (g_str_equal (path, "/dont-expect-warnings"))