|
| 1 | +package proxy_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/zalando/skipper/eskip" |
| 12 | + "github.com/zalando/skipper/filters" |
| 13 | + "github.com/zalando/skipper/filters/diag" |
| 14 | + "github.com/zalando/skipper/proxy" |
| 15 | + "github.com/zalando/skipper/proxy/proxytest" |
| 16 | + xtrace "golang.org/x/exp/trace" |
| 17 | +) |
| 18 | + |
| 19 | +func TestFlightRecorder(t *testing.T) { |
| 20 | + service := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 21 | + if r.Method != "PUT" { |
| 22 | + w.WriteHeader(http.StatusMethodNotAllowed) |
| 23 | + w.Write([]byte(http.StatusText(http.StatusMethodNotAllowed))) |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + var buf bytes.Buffer |
| 28 | + n, err := io.Copy(&buf, r.Body) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("Failed to copy data: %v", err) |
| 31 | + } |
| 32 | + if n < 100 { |
| 33 | + t.Fatalf("Failed to write enough data: %d bytes", n) |
| 34 | + } |
| 35 | + w.WriteHeader(http.StatusCreated) |
| 36 | + w.Write([]byte(http.StatusText(http.StatusCreated))) |
| 37 | + |
| 38 | + })) |
| 39 | + defer service.Close() |
| 40 | + |
| 41 | + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) |
| 42 | + defer backend.Close() |
| 43 | + |
| 44 | + flightRecorder := xtrace.NewFlightRecorder() |
| 45 | + flightRecorder.Start() |
| 46 | + |
| 47 | + spec := diag.NewTrace() |
| 48 | + fr := make(filters.Registry) |
| 49 | + fr.Register(spec) |
| 50 | + |
| 51 | + doc := fmt.Sprintf(`r: * -> trace("20µs") -> "%s"`, backend.URL) |
| 52 | + rr := eskip.MustParse(doc) |
| 53 | + |
| 54 | + pr := proxytest.WithParams(fr, proxy.Params{ |
| 55 | + FlightRecorder: flightRecorder, |
| 56 | + }, rr...) |
| 57 | + _ = pr |
| 58 | + pr.Client().Get(pr.URL) |
| 59 | +} |
0 commit comments