Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove trailing slash from http request if it exists in url #1754

Merged
merged 6 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion build/testing/integration/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
"time"

"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
Expand All @@ -13,11 +17,18 @@ import (
sdk "go.flipt.io/flipt/sdk/go"
)

func API(t *testing.T, ctx context.Context, client sdk.SDK, namespace string, authenticated bool) {
var (
httpClient = &http.Client{
Timeout: 5 * time.Second,
}
)

func API(t *testing.T, ctx context.Context, client sdk.SDK, fliptAddr string, namespace string, authenticated bool) {
t.Run("Namespaces", func(t *testing.T) {
if !namespaceIsDefault(namespace) {
t.Log(`Create namespace.`)

fmt.Println("The namespace is: ", namespace)
created, err := client.Flipt().CreateNamespace(ctx, &flipt.CreateNamespaceRequest{
Key: namespace,
Name: namespace,
Expand Down Expand Up @@ -55,6 +66,20 @@ func API(t *testing.T, ctx context.Context, client sdk.SDK, namespace string, au
require.NoError(t, err)

assert.Equal(t, "Some kind of description", updated.Description)

t.Log(`Namespace request with trailing slash should succeed.`)

if isHTTPProtocol(fliptAddr) && !authenticated {
reader := makeRequestWithTrailingSlash(t, ctx, http.MethodGet, fmt.Sprintf("%s/api/v1/namespaces", fliptAddr))

var fliptNamespaces *flipt.NamespaceList

err = json.NewDecoder(reader).Decode(&fliptNamespaces)
assert.NoError(t, err)

assert.Equal(t, "default", fliptNamespaces.Namespaces[0].Key)
assert.Equal(t, namespace, fliptNamespaces.Namespaces[1].Key)
}
} else {
t.Log(`Ensure default cannot be created.`)

Expand Down Expand Up @@ -641,3 +666,21 @@ func API(t *testing.T, ctx context.Context, client sdk.SDK, namespace string, au
func namespaceIsDefault(ns string) bool {
return ns == "" || ns == "default"
}

func isHTTPProtocol(fliptAddr string) bool {
protocol, _, _ := strings.Cut(fliptAddr, "://")

return protocol == "http" || protocol == "https"
}

func makeRequestWithTrailingSlash(t *testing.T, ctx context.Context, method string, fliptAddrWithPath string) io.Reader {
t.Helper()

req, err := http.NewRequestWithContext(ctx, method, fliptAddrWithPath+"/", nil)
assert.NoError(t, err)

res, err := httpClient.Do(req)
assert.NoError(t, err)

return res.Body
}
4 changes: 2 additions & 2 deletions build/testing/integration/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
)

func TestAPI(t *testing.T) {
integration.Harness(t, func(t *testing.T, sdk sdk.SDK, namespace string, authentication bool) {
integration.Harness(t, func(t *testing.T, sdk sdk.SDK, fliptAddr string, namespace string, authentication bool) {
ctx := context.Background()

api.API(t, ctx, sdk, namespace, authentication)
api.API(t, ctx, sdk, fliptAddr, namespace, authentication)

// run extra tests in authenticated context
if authentication {
Expand Down
4 changes: 2 additions & 2 deletions build/testing/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
fliptNamespace = flag.String("flipt-namespace", "", "Namespace used to scope API calls.")
)

func Harness(t *testing.T, fn func(t *testing.T, sdk sdk.SDK, ns string, authenticated bool)) {
func Harness(t *testing.T, fn func(t *testing.T, sdk sdk.SDK, fliptAddr string, ns string, authenticated bool)) {
var transport sdk.Transport

protocol, host, _ := strings.Cut(*fliptAddr, "://")
Expand Down Expand Up @@ -54,6 +54,6 @@ func Harness(t *testing.T, fn func(t *testing.T, sdk sdk.SDK, ns string, authent

name := fmt.Sprintf("[Protocol %q; Namespace %q; Authentication %v]", protocol, namespace, authentication)
t.Run(name, func(t *testing.T) {
fn(t, sdk.New(transport, opts...), namespace, authentication)
fn(t, sdk.New(transport, opts...), *fliptAddr, namespace, authentication)
})
}
2 changes: 1 addition & 1 deletion build/testing/integration/readonly/readonly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// folder has been loaded into the target instance being tested.
// It then exercises a bunch of read operations via the provided SDK in the target namespace.
func TestReadOnly(t *testing.T) {
integration.Harness(t, func(t *testing.T, sdk sdk.SDK, namespace string, authenticated bool) {
integration.Harness(t, func(t *testing.T, sdk sdk.SDK, _ string, namespace string, authenticated bool) {
ctx := context.Background()
ns, err := sdk.Flipt().GetNamespace(ctx, &flipt.GetNamespaceRequest{
Key: namespace,
Expand Down
16 changes: 16 additions & 0 deletions internal/cmd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"

"github.com/fatih/color"
Expand Down Expand Up @@ -99,6 +101,20 @@ func NewHTTPServer(
h.ServeHTTP(w, r)
})
})
r.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/") {
// Panic if URL can not be parsed if a trailing slash is trimmed.
nurl, err := url.Parse(strings.TrimSuffix(r.URL.String(), "/"))
if err != nil {
panic(err)
}

r.URL = nurl
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strings.TrimSuffix actually already calls strings.HasSuffix.
I think you can boil this all down to just the following (avoids panic too):

Suggested change
if strings.HasSuffix(r.URL.Path, "/") {
// Panic if URL can not be parsed if a trailing slash is trimmed.
nurl, err := url.Parse(strings.TrimSuffix(r.URL.String(), "/"))
if err != nil {
panic(err)
}
r.URL = nurl
}
r.URL.Path = strings.TrimSuffix(r.URL.Path)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GeorgeMac Thank you George!

h.ServeHTTP(w, r)
})
})
r.Use(middleware.Compress(gzip.DefaultCompression))
r.Use(middleware.Recoverer)
r.Mount("/debug", middleware.Profiler())
Expand Down