Skip to content

Commit

Permalink
fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NiniOak committed Nov 8, 2024
1 parent 10e528b commit 1961b33
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
10 changes: 6 additions & 4 deletions agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,12 @@ func ensureContentTypeHeader(next http.Handler, logger hclog.Logger) http.Handle
}

// validate response content-type header
respContentType := resp.Header().Get(api.ContentTypeHeader)
if respContentType == "" || respContentType != contentType {
logger.Debug("warning: response content-type header not explicitly set.", "request-path", req.URL)
resp.Header().Set(api.ContentTypeHeader, contentType)
if resp != nil {
respContentType := resp.Header().Get(api.ContentTypeHeader)
if respContentType == "" || respContentType != contentType {
logger.Debug("warning: response content-type header not explicitly set.", "request-path", req.URL)
resp.Header().Set(api.ContentTypeHeader, contentType)
}
}
})
}
Expand Down
3 changes: 1 addition & 2 deletions agent/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,6 @@ func TestHTTPAPI_DefaultACLPolicy(t *testing.T) {
})
}
}

func TestHTTPAPIResponseHeaders(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
Expand All @@ -643,7 +642,7 @@ func TestHTTPAPIResponseHeaders(t *testing.T) {

// Check the Index page that just renders a simple message with UI disabled
// also gets the right headers.
requireHasHeadersSet(t, a, http.MethodGet, "/", nil, api.JSONContentType)
requireHasHeadersSet(t, a, http.MethodGet, "/", nil, api.PlainContentType)
}

func TestHTTPAPISnapshotEndpointResponseHeaders(t *testing.T) {
Expand Down
8 changes: 5 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1099,9 +1099,11 @@ func (c *Client) doRequest(r *request) (time.Duration, *http.Response, error) {
start := time.Now()
resp, err := c.config.HttpClient.Do(req)
// validate response content-type header
respContentType := resp.Header.Get(ContentTypeHeader)
if respContentType == "" || respContentType != contentType {
resp.Header.Set(ContentTypeHeader, contentType)
if resp != nil {
respContentType := resp.Header.Get(ContentTypeHeader)
if respContentType == "" || respContentType != contentType {
resp.Header.Set(ContentTypeHeader, contentType)
}
}

diff := time.Since(start)
Expand Down
9 changes: 9 additions & 0 deletions api/content_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func DetermineContentType(req *http.Request) string {
return PlainContentType
}

if isIndexPage(req) {
return PlainContentType
}

if strings.HasPrefix(req.URL.Path, "/v1/internal") {
return req.Header.Get(ContentTypeHeader)
}
Expand All @@ -81,3 +85,8 @@ func matchesRule(req *http.Request, rule ContentTypeRule) bool {
return strings.HasPrefix(req.URL.Path, rule.Path) &&
(rule.Method == "" || req.Method == rule.Method)
}

// isIndexPage checks if the request is for the index page
func isIndexPage(req *http.Request) bool {
return req.URL.Path == "/"
}

0 comments on commit 1961b33

Please sign in to comment.