Skip to content

Commit

Permalink
Fix URL construction problems when no trailing slash path is present
Browse files Browse the repository at this point in the history
Fixes #75
  • Loading branch information
bdd committed Nov 23, 2022
1 parent bac4814 commit 89b91ab
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
4 changes: 1 addition & 3 deletions internal/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions internal/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package internal_test
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 89b91ab

Please sign in to comment.