From 89b91ab0bf4c9ac09e1ca405c29dfff1a647f147 Mon Sep 17 00:00:00 2001 From: "Berk D. Demir" Date: Tue, 22 Nov 2022 15:19:35 -0800 Subject: [PATCH] Fix URL construction problems when no trailing slash path is present Fixes #75 --- internal/api.go | 4 +--- internal/api_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/internal/api.go b/internal/api.go index dd01c62..c5ee48d 100644 --- a/internal/api.go +++ b/internal/api.go @@ -251,13 +251,11 @@ func (c *APIClient) ping(handle, rid, typePath string, body io.Reader) (*Instanc return nil, err } - path, err := url.JoinPath(handle, typePath) + u.Path, err = url.JoinPath(u.Path, handle, typePath) if err != nil { return nil, err } - u.Path += path - if len(rid) > 0 { q := url.Values{} q.Add("rid", rid) diff --git a/internal/api_test.go b/internal/api_test.go index 4cba724..a15c89a 100644 --- a/internal/api_test.go +++ b/internal/api_test.go @@ -5,6 +5,7 @@ package internal_test import ( "net/http" "net/http/httptest" + "net/url" "strings" "sync/atomic" "testing" @@ -144,6 +145,47 @@ func TestPostNonRetriable(t *testing.T) { } } +// Tests if POST URI is constructed correctly +func TestPostURIConstruction(t *testing.T) { + t.Parallel() + + type ping func() (*InstanceConfig, error) + + c := &APIClient{} + + testCases := map[string]string{ + "suffix=": "/", + "suffix=/": "/", + "suffix=//": "/", + "suffix=/foo": "/foo", + } + + ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + testCase := r.Header.Get("test-case") + expectedPathPrefix, ok := testCases[testCase] + if !ok { + t.Fatalf("Unexpected test case %s", testCase) + } + + expectedPath, _ := url.JoinPath(expectedPathPrefix, TestHandle, "start") + if r.URL.Path != expectedPath { + t.Errorf("For test case %s expected to get path %s, got %s\n", testCase, expectedPath, r.URL.Path) + } + })) + + defer ts.Close() + + c.Client = ts.Client() + for testCase := range testCases { + reqPath := strings.TrimPrefix(testCase, "suffix=") + c.BaseURL = ts.URL + reqPath + c.ReqHeaders = map[string]string{"test-case": testCase} + if _, err := c.PingStart(TestHandle, TestRunId); err != nil { + t.Fatalf("Request for test case %s failed: %v", testCase, err) + } + } +} + // Tests if Ping{Start,Log,Status} functions hit the correct URI paths. func TestPostURIs(t *testing.T) { t.Parallel()