mirror of https://github.com/databricks/cli.git
Add request body assertions to acceptance tests (#2263)
## Changes With this PR, any acceptance tests that define custom server stubs in `test.toml` will automatically record all HTTP requests made and assert on them. Builds on top of https://github.com/databricks/cli/pull/2226 ## Tests Modifying existing acceptance test.
This commit is contained in:
parent
3c6eacb05b
commit
787dbe9099
|
@ -2,6 +2,7 @@ package acceptance_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -219,8 +220,21 @@ func runTest(t *testing.T, dir, coverDir string, repls testdiff.ReplacementsCont
|
||||||
|
|
||||||
// Start a new server with a custom configuration if the acceptance test
|
// Start a new server with a custom configuration if the acceptance test
|
||||||
// specifies a custom server stubs.
|
// specifies a custom server stubs.
|
||||||
if len(config.Server) > 0 {
|
var server *testserver.Server
|
||||||
server := testserver.New(t)
|
|
||||||
|
// Start a new server for this test if either:
|
||||||
|
// 1. A custom server spec is defined in the test configuration.
|
||||||
|
// 2. The test is configured to record requests and assert on them. We need
|
||||||
|
// a duplicate of the default server to record requests because the default
|
||||||
|
// server otherwise is a shared resource.
|
||||||
|
if len(config.Server) > 0 || config.RecordRequests {
|
||||||
|
server = testserver.New(t)
|
||||||
|
server.RecordRequests = config.RecordRequests
|
||||||
|
|
||||||
|
// If no custom server stubs are defined, add the default handlers.
|
||||||
|
if len(config.Server) == 0 {
|
||||||
|
AddHandlers(server)
|
||||||
|
}
|
||||||
|
|
||||||
for _, stub := range config.Server {
|
for _, stub := range config.Server {
|
||||||
require.NotEmpty(t, stub.Pattern)
|
require.NotEmpty(t, stub.Pattern)
|
||||||
|
@ -249,6 +263,25 @@ func runTest(t *testing.T, dir, coverDir string, repls testdiff.ReplacementsCont
|
||||||
cmd.Dir = tmpDir
|
cmd.Dir = tmpDir
|
||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
|
|
||||||
|
// Write the requests made to the server to a output file if the test is
|
||||||
|
// configured to record requests.
|
||||||
|
if config.RecordRequests {
|
||||||
|
f, err := os.OpenFile(filepath.Join(tmpDir, "out.requests.txt"), os.O_CREATE|os.O_WRONLY, 0o644)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
for _, req := range server.Requests {
|
||||||
|
reqJson, err := json.Marshal(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
line := fmt.Sprintf("%s\n", reqJson)
|
||||||
|
_, err = f.WriteString(line)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = f.Close()
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
// Include exit code in output (if non-zero)
|
// Include exit code in output (if non-zero)
|
||||||
formatOutput(out, err)
|
formatOutput(out, err)
|
||||||
require.NoError(t, out.Close())
|
require.NoError(t, out.Close())
|
||||||
|
|
|
@ -40,6 +40,10 @@ type TestConfig struct {
|
||||||
// }
|
// }
|
||||||
// '''
|
// '''
|
||||||
Server []ServerStub
|
Server []ServerStub
|
||||||
|
|
||||||
|
// Record the requests made to the server and write them as output to
|
||||||
|
// out.requests.txt
|
||||||
|
RecordRequests bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerStub struct {
|
type ServerStub struct {
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
{"method":"POST","path":"/api/2.1/jobs/create","body":{"name":"abc"}}
|
|
@ -1,3 +1,5 @@
|
||||||
|
RecordRequests = true
|
||||||
|
|
||||||
[[Server]]
|
[[Server]]
|
||||||
Pattern = "POST /api/2.1/jobs/create"
|
Pattern = "POST /api/2.1/jobs/create"
|
||||||
Response.Body = '''
|
Response.Body = '''
|
||||||
|
|
|
@ -2,9 +2,12 @@ package testserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/databricks/cli/internal/testutil"
|
"github.com/databricks/cli/internal/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,6 +16,16 @@ type Server struct {
|
||||||
Mux *http.ServeMux
|
Mux *http.ServeMux
|
||||||
|
|
||||||
t testutil.TestingT
|
t testutil.TestingT
|
||||||
|
|
||||||
|
RecordRequests bool
|
||||||
|
|
||||||
|
Requests []Request
|
||||||
|
}
|
||||||
|
|
||||||
|
type Request struct {
|
||||||
|
Method string `json:"method"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
Body any `json:"body"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(t testutil.TestingT) *Server {
|
func New(t testutil.TestingT) *Server {
|
||||||
|
@ -37,6 +50,18 @@ func (s *Server) Handle(pattern string, handler HandlerFunc) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.RecordRequests {
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
assert.NoError(s.t, err)
|
||||||
|
|
||||||
|
s.Requests = append(s.Requests, Request{
|
||||||
|
Method: r.Method,
|
||||||
|
Path: r.URL.Path,
|
||||||
|
Body: json.RawMessage(body),
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
var respBytes []byte
|
var respBytes []byte
|
||||||
|
|
Loading…
Reference in New Issue