From 94e70d358df997ea7434b0856520dcfc14d3cf74 Mon Sep 17 00:00:00 2001 From: Sergey Kacheev Date: Wed, 6 Mar 2024 08:17:22 +0300 Subject: [PATCH] fix: encode path params with BaseURL and the first param at index zero (#781) (#782) --- middleware.go | 2 +- middleware_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/middleware.go b/middleware.go index 1fccbb7..603448d 100644 --- a/middleware.go +++ b/middleware.go @@ -57,7 +57,7 @@ func parseRequestURL(c *Client, r *Request) error { buf := acquireBuffer() defer releaseBuffer(buf) // search for the next or first opened curly bracket - for curr := strings.Index(r.URL, "{"); curr > prev; curr = prev + strings.Index(r.URL[prev:], "{") { + for curr := strings.Index(r.URL, "{"); curr == 0 || curr > prev; curr = prev + strings.Index(r.URL[prev:], "{") { // write everything from the previous position up to the current if curr > prev { buf.WriteString(r.URL[prev:curr]) diff --git a/middleware_test.go b/middleware_test.go index f5924cb..49733e9 100644 --- a/middleware_test.go +++ b/middleware_test.go @@ -145,6 +145,16 @@ func Test_parseRequestURL(t *testing.T) { }, expectedURL: "https://example.com/1/2", }, + { + name: "using base url with path param at index 0", + init: func(c *Client, r *Request) { + c.SetBaseURL("https://example.com/prefix") + r.SetPathParam("first", "1"). + SetPathParam("second", "2") + r.URL = "{first}/{second}" + }, + expectedURL: "https://example.com/prefix/1/2", + }, { name: "using BaseURL with absolute URL in request", init: func(c *Client, r *Request) {