Skip to content

Commit

Permalink
resolved path prefix duplication and added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aryan-p03 committed Dec 31, 2024
1 parent fd3c9d4 commit 6463bbc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion links/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package links
import (
"net/http"
"net/url"
"strings"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -43,7 +44,10 @@ func FromHeadersOrDefault(h *http.Header, defaultURL *url.URL) *Builder {
}

func (b *Builder) BuildURL(oldURL *url.URL) *url.URL {
apiURL := b.URL.JoinPath(oldURL.Path)
newPath := oldURL.Path
newPath = strings.TrimPrefix(newPath, "/v1")

apiURL := b.URL.JoinPath(newPath)
apiURL.RawQuery = oldURL.RawQuery
return apiURL
}
Expand Down
12 changes: 12 additions & 0 deletions links/links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,18 @@ func TestBuilder_BuildLink(t *testing.T) {
"http://localhost:8080/some/path?param1=value1&param2=value2",
"http://localhost:8080/some/path?param1=value1&param2=value2",
},
// Old external link to new internal url
{
"http://localhost:8080/",
"https://some.api.host/v1/some/path",
"http://localhost:8080/some/path",
},
// Old external link to new external url
{
"https://some.api.host/v1",
"https://some.api.host/v1/some/path",
"https://some.api.host/v1/some/path",
},
// Old internal link to new external url
{
"https://some.api.host/v1",
Expand Down

0 comments on commit 6463bbc

Please sign in to comment.