prober: correct content-type response (#15989)

Content-type was responding as test/plain for probes
accepting application/json. Set content type header
before setting the response code to correct this.

Updates tailscale/corp#27370

Signed-off-by: Mike O'Driscoll <mikeo@tailscale.com>
This commit is contained in:
Mike O'Driscoll 2025-05-16 12:51:07 -04:00 committed by GitHub
parent 336b3b7df0
commit 9c52856af6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 1 deletions

View File

@ -559,8 +559,8 @@ func (p *Prober) RunHandler(w http.ResponseWriter, r *http.Request) error {
PreviousSuccessRatio: prevInfo.RecentSuccessRatio(),
PreviousMedianLatency: prevInfo.RecentMedianLatency(),
}
w.WriteHeader(respStatus)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(respStatus)
if err := json.NewEncoder(w).Encode(resp); err != nil {
return tsweb.Error(http.StatusInternalServerError, "error encoding JSON response", err)
}

View File

@ -598,6 +598,9 @@ func TestProberRunHandler(t *testing.T) {
}
if reqJSON {
if w.Header().Get("Content-Type") != "application/json" {
t.Errorf("unexpected content type: got %q, want application/json", w.Header().Get("Content-Type"))
}
var gotJSON RunHandlerResponse
if err := json.Unmarshal(w.Body.Bytes(), &gotJSON); err != nil {
t.Fatalf("failed to unmarshal JSON response: %v; body: %s", err, w.Body.String())