From 1753fa353005724afd893c2b873d4aeeb418f1db Mon Sep 17 00:00:00 2001 From: Rob Whitby Date: Mon, 8 Mar 2021 23:46:27 +0000 Subject: [PATCH] handle GET requests in webhook hra (#378) --- controllers/horizontal_runner_autoscaler_webhook.go | 6 ++++++ .../horizontal_runner_autoscaler_webhook_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/controllers/horizontal_runner_autoscaler_webhook.go b/controllers/horizontal_runner_autoscaler_webhook.go index 083b4e92..44eb2139 100644 --- a/controllers/horizontal_runner_autoscaler_webhook.go +++ b/controllers/horizontal_runner_autoscaler_webhook.go @@ -95,6 +95,12 @@ func (autoscaler *HorizontalRunnerAutoscalerGitHubWebhook) Handle(w http.Respons } }() + // respond ok to GET / e.g. for health check + if r.Method == http.MethodGet { + fmt.Fprintln(w, "webhook server is running") + return + } + var payload []byte if len(autoscaler.SecretKeyBytes) > 0 { diff --git a/controllers/horizontal_runner_autoscaler_webhook_test.go b/controllers/horizontal_runner_autoscaler_webhook_test.go index f57fadb5..fb55b045 100644 --- a/controllers/horizontal_runner_autoscaler_webhook_test.go +++ b/controllers/horizontal_runner_autoscaler_webhook_test.go @@ -113,6 +113,19 @@ func TestWebhookPing(t *testing.T) { ) } +func TestGetRequest(t *testing.T) { + hra := HorizontalRunnerAutoscalerGitHubWebhook{} + request, _ := http.NewRequest(http.MethodGet, "/", nil) + recorder := httptest.ResponseRecorder{} + + hra.Handle(&recorder, request) + response := recorder.Result() + + if response.StatusCode != http.StatusOK { + t.Errorf("want %d, got %d", http.StatusOK, response.StatusCode) + } +} + func TestGetValidCapacityReservations(t *testing.T) { now := time.Now()