From e2cd5851c095547143f3c59738567b2f1aeba83d Mon Sep 17 00:00:00 2001 From: Maha Hajja <82542081+maha-hajja@users.noreply.github.com> Date: Fri, 19 Apr 2024 11:36:28 -0700 Subject: [PATCH] http processor: replace space with + (#1499) * replace space with + * add test * address reviews --- pkg/plugin/processor/builtin/impl/webhook/http.go | 12 +++++++++++- .../processor/builtin/impl/webhook/http_test.go | 14 +++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/pkg/plugin/processor/builtin/impl/webhook/http.go b/pkg/plugin/processor/builtin/impl/webhook/http.go index bdc664d15..92c7e258b 100644 --- a/pkg/plugin/processor/builtin/impl/webhook/http.go +++ b/pkg/plugin/processor/builtin/impl/webhook/http.go @@ -21,6 +21,7 @@ import ( "context" "io" "net/http" + "net/url" "strconv" "strings" "text/template" @@ -164,7 +165,16 @@ func (p *httpProcessor) EvaluateURL(rec opencdc.Record) (string, error) { if err != nil { return "", cerrors.Errorf("error while evaluating URL template: %w", err) } - return b.String(), nil + u, err := url.Parse(b.String()) + if err != nil { + return "", cerrors.Errorf("error parsing URL: %w", err) + } + q, err := url.ParseQuery(u.RawQuery) + if err != nil { + return "", cerrors.Errorf("error parsing URL query: %w", err) + } + u.RawQuery = q.Encode() + return u.String(), nil } func (p *httpProcessor) Process(ctx context.Context, records []opencdc.Record) []sdk.ProcessedRecord { diff --git a/pkg/plugin/processor/builtin/impl/webhook/http_test.go b/pkg/plugin/processor/builtin/impl/webhook/http_test.go index 3a9c48f5f..7ca3d7e48 100644 --- a/pkg/plugin/processor/builtin/impl/webhook/http_test.go +++ b/pkg/plugin/processor/builtin/impl/webhook/http_test.go @@ -399,6 +399,18 @@ func TestHTTPProcessor_URLTemplate(t *testing.T) { }, }}, }, + { + name: "URL template, path and query have spaces", + pathTmpl: `/{{.Payload.Before.url}}`, + path: "/what%20is%20conduit?id=my+id", + args: []opencdc.Record{{ + Payload: opencdc.Change{ + Before: opencdc.StructuredData{ + "url": "what is conduit?id=my id", + }, + }, + }}, + }, } for _, tc := range tests { @@ -407,7 +419,7 @@ func TestHTTPProcessor_URLTemplate(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { // check the expected path with the evaluated URL - is.Equal(req.URL.Path, tc.path) + is.Equal(req.URL.String(), tc.path) })) defer srv.Close()