From 6011cdbf17c33d8ac4fbdf404730e3d985ad8dca Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Mon, 29 Jan 2024 16:17:51 +0100 Subject: [PATCH] refactor: test arg in path alone --- handler_test.go | 2 +- handlers.go | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/handler_test.go b/handler_test.go index fc5df68..959bc31 100644 --- a/handler_test.go +++ b/handler_test.go @@ -54,7 +54,7 @@ func TestRPCRedirectsToKubo(t *testing.T) { tests := []rpcRedirectTest{ {"/api/v0/name/resolve?arg=some-arg", "http://example.com/api/v0/name/resolve?arg=some-arg", http.StatusTemporaryRedirect}, - {"/api/v0/name/resolve/ipfs.io?arg=some-arg", "http://example.com/api/v0/name/resolve/ipfs.io?arg=some-arg", http.StatusTemporaryRedirect}, + {"/api/v0/name/resolve/some-arg", "http://example.com/api/v0/name/resolve/some-arg", http.StatusTemporaryRedirect}, {"/api/v0/resolve?arg=some-arg", "http://example.com/api/v0/resolve?arg=some-arg", http.StatusTemporaryRedirect}, {"/api/v0/dag/resolve?arg=some-arg", "http://example.com/api/v0/dag/resolve?arg=some-arg", http.StatusTemporaryRedirect}, {"/api/v0/dns?arg=some-arg", "http://example.com/api/v0/dns?arg=some-arg", http.StatusTemporaryRedirect}, diff --git a/handlers.go b/handlers.go index e2f0d3d..5834aa1 100644 --- a/handlers.go +++ b/handlers.go @@ -253,7 +253,10 @@ func newKuboRPCHandler(endpoints []string) http.Handler { redirectToKubo := func(w http.ResponseWriter, r *http.Request) { // Naively choose one of the Kubo RPC clients. endpoint := endpoints[rand.Intn(len(endpoints))] - url := endpoint + r.URL.Path + "?" + r.URL.RawQuery + url := endpoint + r.URL.Path + if r.URL.RawQuery != "" { + url += "?" + r.URL.RawQuery + } goLog.Debugw("api request redirected to kubo", "url", r.URL, "redirect", url) http.Redirect(w, r, url, http.StatusTemporaryRedirect) }