diff --git a/acceptance/acceptance_test.go b/acceptance/acceptance_test.go index 908cc098ce..57f3b10b9a 100644 --- a/acceptance/acceptance_test.go +++ b/acceptance/acceptance_test.go @@ -209,8 +209,11 @@ func runTest(t *testing.T, dir, coverDir string, repls testdiff.ReplacementsCont // Start a new server with a custom configuration if the acceptance test // specifies a custom server stubs. + var server *testserver.Server if len(config.Server) > 0 { - server := testserver.New(t) + server = testserver.New(t) + server.RecordRequests = true + for _, stub := range config.Server { require.NotEmpty(t, stub.Pattern) require.NotEmpty(t, stub.Response.Body) @@ -259,6 +262,14 @@ func runTest(t *testing.T, dir, coverDir string, repls testdiff.ReplacementsCont cmd.Dir = tmpDir err = cmd.Run() + // If a custom server was defined, record all requests made to an output file. + if server != nil { + requests := server.Requests() + requestsJSON, err := json.MarshalIndent(requests, "", " ") + require.NoError(t, err) + testutil.WriteFile(t, filepath.Join(tmpDir, "out.requests.json"), string(requestsJSON)) + } + // Include exit code in output (if non-zero) formatOutput(out, err) require.NoError(t, out.Close()) diff --git a/acceptance/workspace/jobs/create/out.requests.json b/acceptance/workspace/jobs/create/out.requests.json new file mode 100644 index 0000000000..4c945bf03e --- /dev/null +++ b/acceptance/workspace/jobs/create/out.requests.json @@ -0,0 +1,9 @@ +[ + { + "method": "POST", + "path": "/api/2.1/jobs/create", + "body": { + "name": "abc" + } + } +] \ No newline at end of file diff --git a/libs/testserver/server.go b/libs/testserver/server.go index 711d205613..e90c2f4860 100644 --- a/libs/testserver/server.go +++ b/libs/testserver/server.go @@ -2,9 +2,12 @@ package testserver import ( "encoding/json" + "io" "net/http" "net/http/httptest" + "github.com/stretchr/testify/assert" + "github.com/databricks/cli/internal/testutil" ) @@ -13,6 +16,16 @@ type Server struct { Mux *http.ServeMux t testutil.TestingT + + RecordRequests bool + + requests []Request +} + +type Request struct { + Method string `json:"method"` + Path string `json:"path"` + Body any `json:"body"` } type Stub struct { @@ -44,6 +57,10 @@ func (s *Server) Close() { s.Server.Close() } +func (s *Server) Requests() []Request { + return s.requests +} + func (s *Server) Handle(pattern string, handler HandlerFunc) { s.Mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { resp, err := handler(r) @@ -52,6 +69,22 @@ func (s *Server) Handle(pattern string, handler HandlerFunc) { return } + if s.RecordRequests { + body, err := io.ReadAll(r.Body) + assert.NoError(s.t, err) + + var reqBody map[string]any + err = json.Unmarshal(body, &reqBody) + assert.NoError(s.t, err) + + s.requests = append(s.requests, Request{ + Method: r.Method, + Path: r.URL.Path, + Body: reqBody, + }) + + } + w.Header().Set("Content-Type", "application/json") var respBytes []byte