From 523cd077c4e4b37d95ed1d1d1e15f8156819cbcb Mon Sep 17 00:00:00 2001 From: "mayur.popli" Date: Thu, 27 Mar 2025 08:30:27 +0530 Subject: [PATCH] fix: plugin test case --- .../requestPreProcessor/cmd/plugin_test.go | 84 +++++++++++++++++++ .../requestPreProcessor/reqpreprocessor.go | 7 ++ 2 files changed, 91 insertions(+) create mode 100644 pkg/plugin/implementation/requestPreProcessor/cmd/plugin_test.go diff --git a/pkg/plugin/implementation/requestPreProcessor/cmd/plugin_test.go b/pkg/plugin/implementation/requestPreProcessor/cmd/plugin_test.go new file mode 100644 index 0000000..956be52 --- /dev/null +++ b/pkg/plugin/implementation/requestPreProcessor/cmd/plugin_test.go @@ -0,0 +1,84 @@ +package main + +import ( + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestProviderNew(t *testing.T) { + testCases := []struct { + name string + config map[string]string + expectedError bool + expectedStatus int + prepareRequest func(req *http.Request) + }{ + { + name: "No Config", + config: map[string]string{}, + expectedError: true, + expectedStatus: http.StatusOK, + prepareRequest: func(req *http.Request) { + // Add minimal required headers + req.Header.Set("context", "test-context") + req.Header.Set("transaction_id", "test-transaction") + }, + }, + { + name: "With Check Keys", + config: map[string]string{ + "CheckKeys": "message_id,transaction_id", + }, + expectedError: false, + expectedStatus: http.StatusOK, + prepareRequest: func(req *http.Request) { + // Add headers matching the check keys + req.Header.Set("context", "test-context") + req.Header.Set("transaction_id", "test-transaction") + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + requestBody := `{ + "context": { + "transaction_id": "abc" + } + }` + + p := provider{} + middleware, err := p.New(context.Background(), tc.config) + if tc.expectedError { + assert.Error(t, err) + return + } + require.NoError(t, err) + assert.NotNil(t, middleware) + + testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + req := httptest.NewRequest("POST", "/", strings.NewReader(requestBody)) + req.Header.Set("Content-Type", "application/json") + if tc.prepareRequest != nil { + tc.prepareRequest(req) + } + + w := httptest.NewRecorder() + middlewaredHandler := middleware(testHandler) + middlewaredHandler.ServeHTTP(w, req) + assert.Equal(t, tc.expectedStatus, w.Code, "Unexpected response status") + responseBody := w.Body.String() + t.Logf("Response Body: %s", responseBody) + + }) + } +} diff --git a/pkg/plugin/implementation/requestPreProcessor/reqpreprocessor.go b/pkg/plugin/implementation/requestPreProcessor/reqpreprocessor.go index 36389dc..031d4ce 100644 --- a/pkg/plugin/implementation/requestPreProcessor/reqpreprocessor.go +++ b/pkg/plugin/implementation/requestPreProcessor/reqpreprocessor.go @@ -88,6 +88,13 @@ func validateConfig(cfg *Config) error { if cfg == nil { return errors.New("config cannot be nil") } + + // Check if CheckKeys is empty + if len(cfg.CheckKeys) == 0 { + return errors.New("checkKeys cannot be empty") + } + + // Validate that CheckKeys does not contain empty strings for _, key := range cfg.CheckKeys { if key == "" { return errors.New("checkKeys cannot contain empty strings")