From 99ad56a0c2ac6b7cf7375480e6593058d3bf6f0e Mon Sep 17 00:00:00 2001 From: Mike Goldsmth Date: Wed, 15 May 2024 13:57:50 +0100 Subject: [PATCH] feat: URL encode dataset --- transmission/transmission.go | 6 ++++- transmission/transmission_test.go | 41 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/transmission/transmission.go b/transmission/transmission.go index 2ecc597..29bf346 100644 --- a/transmission/transmission.go +++ b/transmission/transmission.go @@ -422,7 +422,7 @@ func (b *batchAgg) fireBatch(events []*Event) { } // build the HTTP request - url.Path = path.Join(url.Path, "/1/batch", dataset) + url.Path = path.Join(url.Path, buildReqestPath(dataset)) // One retry allowed for connection timeouts. var resp *http.Response @@ -742,3 +742,7 @@ func buildReqReader(jsonEncoded []byte, compress bool) (io.Reader, bool) { type nower interface { Now() time.Time } + +func buildReqestPath(dataset string) string { + return path.Join("/1/batch", url.PathEscape(dataset)) +} diff --git a/transmission/transmission_test.go b/transmission/transmission_test.go index 6731df5..a395b0b 100644 --- a/transmission/transmission_test.go +++ b/transmission/transmission_test.go @@ -1358,3 +1358,44 @@ func TestFireBatchWithUnauthorizedResponse(t *testing.T) { testEquals(t, l.logs[0], "APIKey 'written' was rejected. Please verify APIKey is correct.") }) } + +func TestBuildRequestPath(t *testing.T) { + testCases := []struct { + datasetName string + expectedUrl string + }{ + { + datasetName: "foobar", + expectedUrl: "/1/batch/foobar", + }, + { + datasetName: "foo.bar", + expectedUrl: "/1/batch/foo.bar", + }, + { + datasetName: "foo-bar", + expectedUrl: "/1/batch/foo-bar", + }, + { + datasetName: "foo/bar", + expectedUrl: "/1/batch/foo%2Fbar", + }, + { + datasetName: "foo(bar)", + expectedUrl: "/1/batch/foo%28bar%29", + }, + { + datasetName: "foo[bar]", + expectedUrl: "/1/batch/foo%5Bbar%5D", + }, + { + datasetName: "foo{bar}", + expectedUrl: "/1/batch/foo%7Bbar%7D", + }, + } + + for _, tc := range testCases { + path := buildReqestPath(tc.datasetName) + assert.Equal(t, tc.expectedUrl, path) + } +}