Enhance API path normalization and configuration validation
- Introduced a new function to normalize request URL paths, collapsing duplicate slashes and clearing raw paths to ensure correct routing in the reverse proxy. - Updated the gateway to utilize the normalization function for API requests, improving routing consistency. - Trimmed whitespace from server configuration fields in the validation process to prevent potential issues with malformed URLs. - Enhanced documentation in GATEWAY_RUN.md to clarify the importance of proper URL formatting and configuration.
This commit is contained in:
@@ -25,6 +25,32 @@ func (c *captureTransport) RoundTrip(req *http.Request) (*http.Response, error)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestDirectorDoubleSlashPathMatchesStripPrefix(t *testing.T) {
|
||||
target, err := url.Parse("http://127.0.0.1:9")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cap := &captureTransport{}
|
||||
rp := NewReverseProxy(target, "/api/mtg", "/v1", "")
|
||||
rp.Transport = cap
|
||||
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://127.0.0.1:9/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.URL.Path = "/api//mtg/health"
|
||||
rp.ServeHTTP(httptest.NewRecorder(), req)
|
||||
|
||||
if cap.got == nil {
|
||||
t.Fatal("no outgoing request captured")
|
||||
}
|
||||
want, err := url.Parse("http://127.0.0.1:9/v1/health")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertSameURL(t, cap.got.URL, want)
|
||||
}
|
||||
|
||||
func TestDirectorRewritesPath(t *testing.T) {
|
||||
target, err := url.Parse("http://127.0.0.1:9")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user