You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
a path with or without a root element is appended to a NewRequest using req.URL.JoinPath()
in both cases the resulting req.URL.Path is equal to the string given to req.URL.JoinPath() without the leading /.
What this means is that when req.Write() is called the path is added incorrectly to the HTTP request line here as something which looks like.
GET test/path HTTP/1.1
This very same line also causes the server to throw a non-descriptive 400 Bad Request.
The interesting thing here is that req.URL.String() will print correctly.
Here we append the existing path to the beginnging of our elements array, and if / doesn't exist we add it. Then we strip that / back off after processing with path.Join().
Test:
package main_test
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
)
// AppendPath appends a specified path to the request's URL.
func AppendPath(req *http.Request, s string) {
req.URL = req.URL.JoinPath(s)
}
// Handler function for the test server
func handler(w http.ResponseWriter, r *http.Request) {
// Respond with a simple message
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello, this is the test server at " + r.URL.Path))
}
// Test function to test the HTTP request
func TestHTTPRequest(t *testing.T) {
// Create a new httptest server
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
// Create a new request
req, err := http.NewRequest(http.MethodGet, server.URL, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
// Append the path to the request
AppendPath(req, "/test/path")
// Send the request to the test server
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("Failed to send request: %v", err)
}
defer resp.Body.Close()
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %v", err)
}
// Verify the response status code
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected status OK; got %v", resp.Status)
}
// Verify the response body
expected := "Hello, this is the test server at /test/path"
if string(body) != expected {
t.Fatalf("Expected body %q; got %q", expected, body)
}
fmt.Println("Test passed!")
}
What did you see happen?
I think, I mentioned everything in the first block, but basically 400 Bad Request
What did you expect to see?
request should ensure a valid path, and then succeed or an error should be created when we attempt to create the request.
The text was updated successfully, but these errors were encountered:
Go version
go version go1.23.4 linux/amd64
Output of
go env
in your module/workspace:What did you do?
If the Request is built without explicitly adding the root path it is neither identified as an error or automatically fixed.
e.g. Given:
req.URL.JoinPath()
req.URL.Path
is equal to the string given toreq.URL.JoinPath()
without the leading/
.What this means is that when
req.Write()
is called the path is added incorrectly to the HTTP request line here as something which looks like.This very same line also causes the server to throw a non-descriptive
400 Bad Request
.The interesting thing here is that req.URL.String() will print correctly.
CAUSED_BY:
https://cs.opensource.google/go/go/+/master:src/net/url/url.go;l=1239-1247
Here we append the existing path to the beginnging of our elements array, and if
/
doesn't exist we add it. Then we strip that/
back off after processing with path.Join().Test:
What did you see happen?
I think, I mentioned everything in the first block, but basically
400 Bad Request
What did you expect to see?
request should ensure a valid path, and then succeed or an error should be created when we attempt to create the request.
The text was updated successfully, but these errors were encountered: