Skip to content

Commit

Permalink
Add request body assertions to acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-goenka committed Jan 29, 2025
1 parent 3e7acdb commit 59f28d6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
13 changes: 12 additions & 1 deletion acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
9 changes: 9 additions & 0 deletions acceptance/workspace/jobs/create/out.requests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"method": "POST",
"path": "/api/2.1/jobs/create",
"body": {
"name": "abc"
}
}
]
33 changes: 33 additions & 0 deletions libs/testserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 59f28d6

Please sign in to comment.