From 13d9fa484da65b22d8db2864ad5a16829f444f63 Mon Sep 17 00:00:00 2001 From: Bharat Pasupula Date: Tue, 10 Oct 2023 11:03:24 +0200 Subject: [PATCH] Add validation for empty URL config --- x-pack/filebeat/input/http_endpoint/config.go | 4 ++ .../input/http_endpoint/config_test.go | 60 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 x-pack/filebeat/input/http_endpoint/config_test.go diff --git a/x-pack/filebeat/input/http_endpoint/config.go b/x-pack/filebeat/input/http_endpoint/config.go index 48fa51bd00b..b7fb61c2168 100644 --- a/x-pack/filebeat/input/http_endpoint/config.go +++ b/x-pack/filebeat/input/http_endpoint/config.go @@ -110,6 +110,10 @@ func (c *config) Validate() error { return errors.New("crc.provider is required when crc.secret is defined") } + if c.URL == "" { + return fmt.Errorf("webhook path URL can not be empty") + } + return nil } diff --git a/x-pack/filebeat/input/http_endpoint/config_test.go b/x-pack/filebeat/input/http_endpoint/config_test.go new file mode 100644 index 00000000000..f5318e214b4 --- /dev/null +++ b/x-pack/filebeat/input/http_endpoint/config_test.go @@ -0,0 +1,60 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package http_endpoint + +import ( + "fmt" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_validateConfig(t *testing.T) { + testCases := []struct { + name string // Sub-test name. + config config // Load config parameters. + wantError error // Expected error + }{ + { + name: "invalid URL", + config: config{ + URL: "", + ResponseBody: `{"message": "success"}`, + Method: http.MethodPost, + }, + wantError: fmt.Errorf("webhook path URL can not be empty"), + }, + { + name: "invalid method", + config: config{ + URL: "/", + ResponseBody: `{"message": "success"}`, + Method: "random", + }, + wantError: fmt.Errorf("method must be POST, PUT or PATCH: random"), + }, + { + name: "invalid URL", + config: config{ + URL: "/", + ResponseBody: "", + Method: http.MethodPost, + }, + wantError: fmt.Errorf("response_body must be valid JSON"), + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + tc.config.URL = "" + // Execute config validation + err := tc.config.Validate() + + // Validate responses + assert.Equal(t, tc.wantError, err) + }) + } +}