// The OpenAPI checker is what lets a fake daemon stand in for the real one: a // fake that drifted from docs/openapi.yaml, or a client request the daemon // would refuse, must fail the test that used it. Each case plants one drift // and expects the checker to record it, and the faithful exchange beside it to // pass. package testwire import ( "io" "net/http" "net/http/httptest" "strings" "testing" ) type exchange struct { name string method string target string body string media string status int reply string replyAs string } func run(t *testing.T, case_ exchange) []error { t.Helper() checker, err := NewChecker() if err != nil { t.Fatal(err) } server := httptest.NewServer(checker.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = io.ReadAll(r.Body) if case_.replyAs != "" { w.Header().Set("Content-Type", case_.replyAs) } w.WriteHeader(case_.status) for line := range strings.SplitAfterSeq(case_.reply, "\n") { _, _ = io.WriteString(w, line) w.(http.Flusher).Flush() } }))) request, err := http.NewRequestWithContext(t.Context(), case_.method, server.URL+case_.target, strings.NewReader(case_.body)) if err != nil { t.Fatal(err) } request.Header.Set("If-Match", `"revision-a"`) if case_.media != "" { request.Header.Set("Content-Type", case_.media) } // The client side checks the same exchange through Transport. client := &http.Client{Transport: checker.Transport(http.DefaultTransport)} response, err := client.Do(request) if err != nil { t.Fatal(err) } _, _ = io.ReadAll(response.Body) _ = response.Body.Close() server.Close() return checker.Failures() } const problem = `{"type":"about:blank","title":"Unauthorized","status":401,"code":"unauthorized","detail":"no"}` func TestCheckerRejectsEachPlantedDrift(t *testing.T) { historyPage := `{"items":[],"older":null,"newer":null,"high_water":0}` batch := "data: {\"generation\":\"" + GenerationA + "\",\"cursor\":0,\"events\":[]}\n\n" cases := []struct { faithful exchange drifted exchange }{ { exchange{name: "server", method: "GET", target: "/server", status: 200, reply: Server, replyAs: "application/json"}, exchange{name: "server missing protocol", method: "GET", target: "/server", status: 200, reply: strings.Replace(Server, `"protocol":4,`, "", 1), replyAs: "application/json"}, }, { exchange{name: "problem", method: "GET", target: "/server", status: 401, reply: problem, replyAs: "application/problem+json"}, exchange{name: "problem without code", method: "GET", target: "/server", status: 401, reply: strings.Replace(problem, `"code":"unauthorized",`, "", 1), replyAs: "application/problem+json"}, }, { exchange{name: "documented status", method: "GET", target: "/server", status: 401, reply: problem, replyAs: "application/problem+json"}, exchange{name: "undocumented status", method: "GET", target: "/server", status: 418, reply: problem, replyAs: "application/problem+json"}, }, { exchange{name: "documented route", method: "GET", target: "/sessions/s1/history?limit=5", status: 200, reply: historyPage, replyAs: "application/json"}, exchange{name: "undocumented route", method: "GET", target: "/sessions/s1/histories", status: 200, reply: historyPage, replyAs: "application/json"}, }, { exchange{name: "declared query", method: "GET", target: "/sessions/s1/history?limit=5", status: 200, reply: historyPage, replyAs: "application/json"}, exchange{name: "undeclared query", method: "GET", target: "/sessions/s1/history?count=5", status: 200, reply: historyPage, replyAs: "application/json"}, }, { exchange{name: "integer query", method: "GET", target: "/sessions/s1/history?limit=5", status: 200, reply: historyPage, replyAs: "application/json"}, exchange{name: "mistyped query", method: "GET", target: "/sessions/s1/history?limit=five", status: 200, reply: historyPage, replyAs: "application/json"}, }, { exchange{name: "patch with a field", method: "PATCH", target: "/sessions/s1/configuration", body: `{"name":"x"}`, media: "application/merge-patch+json", status: 412, reply: problem, replyAs: "application/problem+json"}, exchange{name: "patch with an unknown field", method: "PATCH", target: "/sessions/s1/configuration", body: `{"nickname":"x"}`, media: "application/merge-patch+json", status: 412, reply: problem, replyAs: "application/problem+json"}, }, { exchange{name: "get without body", method: "GET", target: "/server", status: 200, reply: Server, replyAs: "application/json"}, exchange{name: "get with body", method: "GET", target: "/server", body: `{}`, media: "application/json", status: 200, reply: Server, replyAs: "application/json"}, }, { exchange{name: "stream batch", method: "GET", target: "/sessions/s1/events", status: 200, reply: batch, replyAs: "text/event-stream"}, exchange{name: "stream batch without cursor", method: "GET", target: "/sessions/s1/events", status: 200, reply: strings.Replace(batch, `"cursor":0,`, "", 1), replyAs: "text/event-stream"}, }, } for _, pair := range cases { if failures := run(t, pair.faithful); len(failures) != 0 { t.Errorf("%s: faithful exchange rejected: %v", pair.faithful.name, failures) } if failures := run(t, pair.drifted); len(failures) == 0 { t.Errorf("%s: drift accepted", pair.drifted.name) } } }