From 07d125d8fafbe42a4db0519bcd6aa877b61230e0 Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Thu, 4 Aug 2022 17:15:00 +0200 Subject: [PATCH] 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. --- pkg/base1/test-http.js | 25 +++++++++++++++++++++++++ src/ws/test-server.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/pkg/base1/test-http.js b/pkg/base1/test-http.js index a1dffa677..62b3623a3 100644 --- a/pkg/base1/test-http.js +++ b/pkg/base1/test-http.js @@ -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("/"), diff --git a/src/ws/test-server.c b/src/ws/test-server.c index 5ceb33073..32820910b 100644 --- a/src/ws/test-server.c +++ b/src/ws/test-server.c @@ -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"))