Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(sdk-go): keep a raw path's query ahead of its fragment
Yudhi Armyndharis15992af27b82
diff --git a/sdk/go/client.go b/sdk/go/client.goindex d9bf5465e..4464d9b05 100644--- a/sdk/go/client.go+++ b/sdk/go/client.go@@ -296,19 +296,17 @@ func (c *Client) doRaw(ctx context.Context, method, path string, query url.Valuereader = bytes.NewReader(b)}- rawURL := c.baseURL + path- if len(query) > 0 {- sep := "?"- if strings.Contains(path, "?") {- sep = "&"- }- rawURL += sep + query.Encode()- }-- req, err := http.NewRequestWithContext(ctx, method, rawURL, reader)+ req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, reader)if err != nil {return nil, "", fmt.Errorf("openwa: building request: %w", err)}+ // Merged into the parsed URL: appended to the string, the values would land inside a fragment.+ if len(query) > 0 {+ if req.URL.RawQuery != "" {+ req.URL.RawQuery += "&"+ }+ req.URL.RawQuery += query.Encode()+ }// Read through the request: NewRequestWithContext has just refused a nil ctx, which ctx.Value would panic on.if key, hasKey := req.Context().Value(idempotencyContextKey{}).(string); hasKey {if len(key) < 1 || len(key) > 255 || strings.IndexFunc(key, func(r rune) bool { return r < 0x21 || r > 0x7e }) >= 0 {diff --git a/sdk/go/client_test.go b/sdk/go/client_test.goindex 5aa6eb155..5b90c971d 100644--- a/sdk/go/client_test.go+++ b/sdk/go/client_test.go@@ -1778,13 +1778,29 @@ func TestRequestLogRedactsBaseURLPassword(t *testing.T) {// A Do path that already carries a query string keeps it; the query values are appended with "&"// rather than a second "?", which the server would read as part of the first value.func TestDoPathWithQueryAppendsQueryValues(t *testing.T) {- rt := &recordTransport{status: 200, body: `[]`}- c := newTestClient(t, rt)- if err := c.Do(context.Background(), "GET", "/api/sessions?limit=5", url.Values{"name": {"x"}}, nil, nil); err != nil {- t.Fatal(err)- }- if got := rt.lastReq.URL.RawQuery; got != "limit=5&name=x" {- t.Errorf("query = %q, want %q", got, "limit=5&name=x")+ for _, tc := range []struct{ path, want string }{+ {"/api/sessions", "name=x&tag=a&tag=b"},+ {"/api/sessions?limit=5", "limit=5&name=x&tag=a&tag=b"},+ {"/api/sessions#client", "name=x&tag=a&tag=b"},+ {"/api/sessions?limit=5#client", "limit=5&name=x&tag=a&tag=b"},+ {"/api/sessions#client?x=1", "name=x&tag=a&tag=b"},+ } {+ t.Run(tc.path, func(t *testing.T) {+ rt := &recordTransport{status: 200, body: `[]`}+ c, err := New("https://api.example.com/prefix", "k", WithTransport(rt))+ if err != nil {+ t.Fatal(err)+ }+ if err := c.Do(context.Background(), "GET", tc.path, url.Values{"name": {"x"}, "tag": {"a", "b"}}, nil, nil); err != nil {+ t.Fatal(err)+ }+ if got := rt.lastReq.URL.RawQuery; got != tc.want {+ t.Errorf("query = %q, want %q", got, tc.want)+ }+ if got := rt.lastReq.URL.EscapedPath(); got != "/prefix/api/sessions" {+ t.Errorf("path = %q, want the base URL prefix preserved", got)+ }+ })}}@@ -2003,3 +2019,17 @@ func TestStatusFontConstants(t *testing.T) {t.Errorf("font did not marshal as a bare number: %s", body)}}++func TestReservedCharactersStayInTypedPathSegment(t *testing.T) {+ rt := &recordTransport{status: 200, body: `{}`}+ c := newTestClient(t, rt)+ if _, err := c.Contacts.Get(context.Background(), "s", "a/b?c#d"); err != nil {+ t.Fatal(err)+ }+ if got := rt.lastReq.URL.EscapedPath(); got != "/api/sessions/s/contacts/a%2Fb%3Fc%23d" {+ t.Errorf("escaped path = %q", got)+ }+ if rt.lastReq.URL.RawQuery != "" || rt.lastReq.URL.Fragment != "" {+ t.Errorf("the ID moved into query or fragment: %s", rt.lastReq.URL)+ }+}