Skip to content

Commit 1961b33

Browse files
committed
fix failing tests
1 parent 10e528b commit 1961b33

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

agent/http.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,12 @@ func ensureContentTypeHeader(next http.Handler, logger hclog.Logger) http.Handle
360360
}
361361

362362
// validate response content-type header
363-
respContentType := resp.Header().Get(api.ContentTypeHeader)
364-
if respContentType == "" || respContentType != contentType {
365-
logger.Debug("warning: response content-type header not explicitly set.", "request-path", req.URL)
366-
resp.Header().Set(api.ContentTypeHeader, contentType)
363+
if resp != nil {
364+
respContentType := resp.Header().Get(api.ContentTypeHeader)
365+
if respContentType == "" || respContentType != contentType {
366+
logger.Debug("warning: response content-type header not explicitly set.", "request-path", req.URL)
367+
resp.Header().Set(api.ContentTypeHeader, contentType)
368+
}
367369
}
368370
})
369371
}

agent/http_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,6 @@ func TestHTTPAPI_DefaultACLPolicy(t *testing.T) {
617617
})
618618
}
619619
}
620-
621620
func TestHTTPAPIResponseHeaders(t *testing.T) {
622621
if testing.Short() {
623622
t.Skip("too slow for testing.Short")
@@ -643,7 +642,7 @@ func TestHTTPAPIResponseHeaders(t *testing.T) {
643642

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

649648
func TestHTTPAPISnapshotEndpointResponseHeaders(t *testing.T) {

api/api.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,9 +1099,11 @@ func (c *Client) doRequest(r *request) (time.Duration, *http.Response, error) {
10991099
start := time.Now()
11001100
resp, err := c.config.HttpClient.Do(req)
11011101
// validate response content-type header
1102-
respContentType := resp.Header.Get(ContentTypeHeader)
1103-
if respContentType == "" || respContentType != contentType {
1104-
resp.Header.Set(ContentTypeHeader, contentType)
1102+
if resp != nil {
1103+
respContentType := resp.Header.Get(ContentTypeHeader)
1104+
if respContentType == "" || respContentType != contentType {
1105+
resp.Header.Set(ContentTypeHeader, contentType)
1106+
}
11051107
}
11061108

11071109
diff := time.Since(start)

api/content_type.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ func DetermineContentType(req *http.Request) string {
6161
return PlainContentType
6262
}
6363

64+
if isIndexPage(req) {
65+
return PlainContentType
66+
}
67+
6468
if strings.HasPrefix(req.URL.Path, "/v1/internal") {
6569
return req.Header.Get(ContentTypeHeader)
6670
}
@@ -81,3 +85,8 @@ func matchesRule(req *http.Request, rule ContentTypeRule) bool {
8185
return strings.HasPrefix(req.URL.Path, rule.Path) &&
8286
(rule.Method == "" || req.Method == rule.Method)
8387
}
88+
89+
// isIndexPage checks if the request is for the index page
90+
func isIndexPage(req *http.Request) bool {
91+
return req.URL.Path == "/"
92+
}

0 commit comments

Comments
 (0)