mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
fix(mcp): keep the target query when it does not parse
mergeQueryStrings now forwards a configured query that ParseQuery rejects, next to the client query, instead of dropping it.
This commit is contained in:
+3
-5
@@ -294,11 +294,9 @@ func mergeQueryStrings(targetQuery, requestQuery string) string {
|
||||
}
|
||||
merged, err := url.ParseQuery(targetQuery)
|
||||
if err != nil {
|
||||
// targetQuery came from an already-parsed url.URL's own RawQuery,
|
||||
// so this is unreachable in practice — falls back to the
|
||||
// request's own query alone rather than panicking or silently
|
||||
// dropping both.
|
||||
return requestQuery
|
||||
// url.Parse accepts a query that ParseQuery rejects (a bare
|
||||
// semicolon); the configured credential still has to go upstream.
|
||||
return targetQuery + "&" + requestQuery
|
||||
}
|
||||
reqValues, err := url.ParseQuery(requestQuery)
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package traefikllmgateway
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMergeQueryStrings_UnparseableTargetQueryIsKept(t *testing.T) {
|
||||
if got, want := mergeQueryStrings("token=s;v=1", "a=1"), "token=s;v=1&a=1"; got != want {
|
||||
t.Errorf("got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeQueryStrings_ParseableUnchanged(t *testing.T) {
|
||||
cases := []struct{ target, req, want string }{
|
||||
{"token=s", "", "token=s"},
|
||||
{"", "a=1", "a=1"},
|
||||
{"token=s", "a=1", "a=1&token=s"},
|
||||
{"a=1", "a=2", "a=1&a=2"},
|
||||
{"token=s", "a=1;b=2", "token=s"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := mergeQueryStrings(c.target, c.req); got != c.want {
|
||||
t.Errorf("merge(%q, %q) = %q, want %q", c.target, c.req, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildUpstreamTargetURL_SemicolonTargetQueryReachesUpstream(t *testing.T) {
|
||||
var gotRaw string
|
||||
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotRaw = r.URL.RawQuery
|
||||
}))
|
||||
defer up.Close()
|
||||
h := newTargetRedirectGateway(t, up.URL+"/mcp?token=s;v=1", up.URL+"/a2a")
|
||||
if rec := doTarget(h, "/mcp/alpha?a=1"); rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d", rec.Code)
|
||||
}
|
||||
if gotRaw != "token=s;v=1&a=1" {
|
||||
t.Errorf("upstream raw query = %q, want %q", gotRaw, "token=s;v=1&a=1")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user