mirror of
https://github.com/tailscale/tailscale.git
synced 2025-07-29 15:23:45 +00:00
prober: update header check test (#15993)
Use of the httptest client doesn't render header ordering as expected. Use http.DefaultClient for the test to ensure that the header ordering test is valid. Updates tailscale/corp#27370 Signed-off-by: Mike O'Driscoll <mikeo@tailscale.com>
This commit is contained in:
parent
c4fb380f3c
commit
118206ab79
@ -9,6 +9,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -586,30 +587,48 @@ func TestProberRunHandler(t *testing.T) {
|
|||||||
defer probe.Close()
|
defer probe.Close()
|
||||||
<-probe.stopped // wait for the first run.
|
<-probe.stopped // wait for the first run.
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
mux := http.NewServeMux()
|
||||||
|
server := httptest.NewServer(mux)
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
mux.Handle("/prober/run/", tsweb.StdHandler(tsweb.ReturnHandlerFunc(p.RunHandler), tsweb.HandlerOptions{}))
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", server.URL+"/prober/run/?name="+tt.name, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
req := httptest.NewRequest("GET", "/prober/run/?name="+tt.name, nil)
|
|
||||||
if reqJSON {
|
if reqJSON {
|
||||||
req.Header.Set("Accept", "application/json")
|
req.Header.Set("Accept", "application/json")
|
||||||
}
|
}
|
||||||
tsweb.StdHandler(tsweb.ReturnHandlerFunc(p.RunHandler), tsweb.HandlerOptions{}).ServeHTTP(w, req)
|
|
||||||
if w.Result().StatusCode != tt.wantResponseCode {
|
resp, err := http.DefaultClient.Do(req)
|
||||||
t.Errorf("unexpected response code: got %d, want %d", w.Code, tt.wantResponseCode)
|
if err != nil {
|
||||||
|
t.Fatalf("failed to make request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != tt.wantResponseCode {
|
||||||
|
t.Errorf("unexpected response code: got %d, want %d", resp.StatusCode, tt.wantResponseCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
if reqJSON {
|
if reqJSON {
|
||||||
if w.Header().Get("Content-Type") != "application/json" {
|
if resp.Header.Get("Content-Type") != "application/json" {
|
||||||
t.Errorf("unexpected content type: got %q, want application/json", w.Header().Get("Content-Type"))
|
t.Errorf("unexpected content type: got %q, want application/json", resp.Header.Get("Content-Type"))
|
||||||
}
|
}
|
||||||
var gotJSON RunHandlerResponse
|
var gotJSON RunHandlerResponse
|
||||||
if err := json.Unmarshal(w.Body.Bytes(), &gotJSON); err != nil {
|
body, err := io.ReadAll(resp.Body)
|
||||||
t.Fatalf("failed to unmarshal JSON response: %v; body: %s", err, w.Body.String())
|
if err != nil {
|
||||||
|
t.Fatalf("failed to read response body: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(body, &gotJSON); err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal JSON response: %v; body: %s", err, body)
|
||||||
}
|
}
|
||||||
if diff := cmp.Diff(tt.wantJSONResponse, gotJSON, cmpopts.IgnoreFields(ProbeInfo{}, "Start", "End", "Labels", "RecentLatencies")); diff != "" {
|
if diff := cmp.Diff(tt.wantJSONResponse, gotJSON, cmpopts.IgnoreFields(ProbeInfo{}, "Start", "End", "Labels", "RecentLatencies")); diff != "" {
|
||||||
t.Errorf("unexpected JSON response (-want +got):\n%s", diff)
|
t.Errorf("unexpected JSON response (-want +got):\n%s", diff)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
body, _ := io.ReadAll(w.Result().Body)
|
body, _ := io.ReadAll(resp.Body)
|
||||||
if !strings.Contains(string(body), tt.wantPlaintextResponse) {
|
if !strings.Contains(string(body), tt.wantPlaintextResponse) {
|
||||||
t.Errorf("unexpected response body: got %q, want to contain %q", body, tt.wantPlaintextResponse)
|
t.Errorf("unexpected response body: got %q, want to contain %q", body, tt.wantPlaintextResponse)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user