diff --git a/transmission/transmission.go b/transmission/transmission.go index aec5ded..243b193 100644 --- a/transmission/transmission.go +++ b/transmission/transmission.go @@ -19,7 +19,6 @@ import ( "io/ioutil" "net/http" "net/url" - "path" "runtime" "strings" "sync" @@ -399,7 +398,7 @@ func (b *batchAgg) fireBatch(events []*Event) { } } - url, err := url.Parse(apiHost) + url, err := buildRequestURL(apiHost, dataset) if err != nil { end := time.Now().UTC() if b.testNower != nil { @@ -421,9 +420,6 @@ func (b *batchAgg) fireBatch(events []*Event) { return } - // build the HTTP request - url.Path = buildReqestPath(url.Path, dataset) - // One retry allowed for connection timeouts. var resp *http.Response for try := 0; try < 2; try++ { @@ -437,9 +433,9 @@ func (b *batchAgg) fireBatch(events []*Event) { // Pass the underlying bytes.Reader to http.Request so that // GetBody and ContentLength fields are populated on Request. // See https://cs.opensource.google/go/go/+/refs/tags/go1.17.5:src/net/http/request.go;l=898 - req, err = http.NewRequest("POST", url.String(), &reader.Reader) + req, err = http.NewRequest("POST", url, &reader.Reader) } else { - req, err = http.NewRequest("POST", url.String(), reqBody) + req, err = http.NewRequest("POST", url, reqBody) } req.Header.Set("Content-Type", contentType) if zipped { @@ -743,6 +739,6 @@ type nower interface { Now() time.Time } -func buildReqestPath(existingPath, dataset string) string { - return path.Join(existingPath, "/1/batch", url.PathEscape(dataset)) +func buildRequestURL(apiHost, dataset string) (string, error) { + return url.JoinPath(apiHost, "/1/batch", url.PathEscape(dataset)) } diff --git a/transmission/transmission_test.go b/transmission/transmission_test.go index b1ae3bd..3b025b5 100644 --- a/transmission/transmission_test.go +++ b/transmission/transmission_test.go @@ -1395,7 +1395,8 @@ func TestBuildRequestPath(t *testing.T) { } for _, tc := range testCases { - path := buildReqestPath("", tc.datasetName) - assert.Equal(t, tc.expectedPath, path) + url, err := buildRequestURL("http://fakeHost:8080", tc.datasetName) + assert.NoError(t, err) + assert.Equal(t, "http://fakeHost:8080"+tc.expectedPath, url) } }