Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when unknown API endpoint is used in testserver #2292

Merged
merged 9 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion acceptance/cmd_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import (
func StartCmdServer(t *testing.T) *testserver.Server {
server := testserver.New(t)

server.Handle("/", func(w *testserver.FakeWorkspace, r *http.Request) (any, int) {
// {$} is a wildcard that only matches the end of the URL. We explicitly use
// /{$} to disambiguate it from the generic handler for '/' which is used to
// identify unhandled API endpoints in the test server.
server.Handle("/{$}", func(w *testserver.FakeWorkspace, r *http.Request) (any, int) {
q := r.URL.Query()
args := strings.Split(q.Get("args"), " ")

Expand Down
34 changes: 33 additions & 1 deletion libs/testserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/databricks/cli/internal/testutil"
"github.com/databricks/databricks-sdk-go/apierr"
)

type Server struct {
Expand Down Expand Up @@ -41,13 +42,44 @@ func New(t testutil.TestingT) *Server {
server := httptest.NewServer(mux)
t.Cleanup(server.Close)

return &Server{
s := &Server{
Server: server,
Mux: mux,
t: t,
mu: &sync.Mutex{},
fakeWorkspaces: map[string]*FakeWorkspace{},
}

// The server resolves conflicting handlers by using the one with higher
// specificity. This handler is the least specific, so it will be used as a
// fallback when no other handlers match.
s.Handle("/", func(fakeWorkspace *FakeWorkspace, r *http.Request) (any, int) {
pattern := r.Method + " " + r.URL.Path

t.Errorf(`
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved

----------------------------------------
No stub found for pattern: %s

To stub a response for this request, you can add
the following to test.toml:
[[Server]]
Pattern = %q
Response.Body = '''
<response body here>
'''
Response.StatusCode = <response status-code here>
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved
----------------------------------------


`, pattern, pattern)

return apierr.APIError{
Message: "No stub found for pattern: " + pattern,
}, http.StatusNotFound
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved
})

return s
}

type HandlerFunc func(fakeWorkspace *FakeWorkspace, req *http.Request) (resp any, statusCode int)
Expand Down