From d50037cae20202d74e39c977c25e757e2ae7c98d Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 24 Mar 2021 15:33:52 -0400 Subject: [PATCH] http: add a test for transform changing value --- agent/uiserver/uiserver_test.go | 57 +++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/agent/uiserver/uiserver_test.go b/agent/uiserver/uiserver_test.go index 39d54eff09..3f56536088 100644 --- a/agent/uiserver/uiserver_test.go +++ b/agent/uiserver/uiserver_test.go @@ -11,9 +11,9 @@ import ( "strings" "testing" - "golang.org/x/net/html" - + "github.com/hashicorp/go-hclog" "github.com/stretchr/testify/require" + "golang.org/x/net/html" "github.com/hashicorp/consul/agent/config" "github.com/hashicorp/consul/sdk/testutil" @@ -360,3 +360,56 @@ func TestCompiledJS(t *testing.T) { } } + +func TestHandler_ServeHTTP_TransformIsEvaluatedOnEachRequest(t *testing.T) { + cfg := basicUIEnabledConfig() + + value := "seeds" + transform := func(data map[string]interface{}) error { + data["apple"] = value + return nil + } + h := NewHandler(cfg, hclog.New(nil), transform) + + t.Run("initial request", func(t *testing.T) { + req := httptest.NewRequest("GET", "/", nil) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + + require.Equal(t, http.StatusOK, rec.Code) + expected := `{ + "ACLsEnabled": false, + "LocalDatacenter": "dc1", + "ContentPath": "/ui/", + "UIConfig": { + "metrics_provider": "", + "metrics_proxy_enabled": false, + "dashboard_url_templates": null + }, + "apple": "seeds" + }` + require.JSONEq(t, expected, extractUIConfig(t, rec.Body.String())) + }) + + t.Run("transform value has changed", func(t *testing.T) { + + value = "plant" + req := httptest.NewRequest("GET", "/", nil) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + + require.Equal(t, http.StatusOK, rec.Code) + expected := `{ + "ACLsEnabled": false, + "LocalDatacenter": "dc1", + "ContentPath": "/ui/", + "UIConfig": { + "metrics_provider": "", + "metrics_proxy_enabled": false, + "dashboard_url_templates": null + }, + "apple": "plant" + }` + require.JSONEq(t, expected, extractUIConfig(t, rec.Body.String())) + }) +}