Skip to content

Commit 61f5a6f

Browse files
authored
Add basic scheme to url building path to prevent mishaps in parsing (#411)
1 parent 8a95a4f commit 61f5a6f

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

esapi/esapi_request_internal_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
//go:build !integration
1819
// +build !integration
1920

2021
package esapi
@@ -74,5 +75,16 @@ func TestAPIRequest(t *testing.T) {
7475
if _, ok := req.Body.(io.ReadCloser); !ok {
7576
t.Errorf("Unexpected type for req.Body: %T", req.Body)
7677
}
78+
79+
req, err = newRequest("GET", "http:////_aliases/test", nil)
80+
if err != nil {
81+
t.Fatalf("Unexpected error: %s", err)
82+
}
83+
if req.URL.String() != "http:////_aliases/test" {
84+
t.Errorf("Unexpected url for request: %s", req.URL.String())
85+
}
86+
if req.URL.Host != "" {
87+
t.Errorf("Unexpected host, should be empty, got: %s", req.URL.Host)
88+
}
7789
})
7890
}

internal/build/cmd/generate/commands/gensource/generator.go

+7
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,12 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
576576

577577
pathGrow.WriteString(` path.Grow(`)
578578

579+
// Adds a scheme to the initial empty request.
580+
// This allows the http.NewRequest to build a proper url
581+
// even if some arguments are missing.
582+
pathGrow.WriteString(`7 + `)
583+
pathContent.WriteString(`path.WriteString("http://")` + "\n")
584+
579585
// FIXME: Select longest path based on number of template entries, not string length
580586
longestPath := g.Endpoint.URL.Paths[0]
581587
for _, p := range g.Endpoint.URL.Paths {
@@ -806,6 +812,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
806812
}
807813

808814
g.w(`req, err := newRequest(method, path.String(), ` + httpBody + `)` + "\n")
815+
809816
g.w(`if err != nil {
810817
return nil, err
811818
}` + "\n\n")

0 commit comments

Comments
 (0)