Documentation
¶
Overview ¶
Package testserver provides an in-process mock RPC server for testing applications that use Connect RPC.
The mock server is designed to be reusable across multiple projects and provides a realistic testing environment by implementing actual HTTP/RPC handlers rather than using interface mocks.
Basic Usage ¶
Create a mock server and configure responses:
func TestMyFeature(t *testing.T) {
server := testserver.NewMockBuilderServer(t)
defer server.Close()
// Configure mock response
server.OnGetClusterTimescaleDB().Return(&serverv1.GetClusterTimescaleDBResponse{
Id: "test-db-id",
Name: "test-db",
})
// Use server.URL to create a real client
client := serverv1connect.NewBuilderServiceClient(http.DefaultClient, server.URL)
resp, err := client.GetClusterTimescaleDB(ctx, connect.NewRequest(&serverv1.GetClusterTimescaleDBRequest{
Id: "test-db-id",
}))
require.NoError(t, err)
assert.Equal(t, "test-db-id", resp.Msg.Id)
}
Error Testing ¶
Configure methods to return errors:
server.OnUpdateClusterTimescaleDB().ReturnError(errors.New("update failed"))
client := serverv1connect.NewBuilderServiceClient(http.DefaultClient, server.URL)
_, err := client.UpdateClusterTimescaleDB(ctx, connect.NewRequest(...))
require.Error(t, err)
assert.Contains(t, err.Error(), "update failed")
Request Capture ¶
Capture and assert on requests sent to the server:
server.OnGetClusterTimescaleDB().Return(&serverv1.GetClusterTimescaleDBResponse{...})
client := serverv1connect.NewBuilderServiceClient(http.DefaultClient, server.URL)
client.GetClusterTimescaleDB(ctx, connect.NewRequest(&serverv1.GetClusterTimescaleDBRequest{
Id: "test-id",
}))
requests := server.GetCapturedRequests("GetClusterTimescaleDB")
require.Len(t, requests, 1)
capturedReq := requests[0].(*serverv1.GetClusterTimescaleDBRequest)
assert.Equal(t, "test-id", capturedReq.Id)
Custom Behaviors ¶
For complex scenarios, use custom behavior functions:
server.OnGetClusterTimescaleDB().WithBehavior(func(req proto.Message) (proto.Message, error) {
getReq := req.(*serverv1.GetClusterTimescaleDBRequest)
if getReq.Id == "invalid" {
return nil, errors.New("not found")
}
return &serverv1.GetClusterTimescaleDBResponse{
Id: getReq.Id,
Name: "dynamic-name",
}, nil
})
Thread Safety ¶
The mock server is thread-safe and can be used with t.Parallel() tests. Each test should create its own server instance to ensure isolation.
Index ¶
- type BehaviorFunc
- type MethodConfigBuilder
- type MockServer
- func (s *MockServer) GetCapturedRequests(methodName string) []proto.Message
- func (s *MockServer) OnCreateClusterTimescaleDB() *MethodConfigBuilder[*serverv1.CreateClusterTimescaleDBResponse]
- func (s *MockServer) OnDeleteClusterTimescaleDB() *MethodConfigBuilder[*serverv1.DeleteClusterTimescaleDBResponse]
- func (s *MockServer) OnGetClusterTimescaleDB() *MethodConfigBuilder[*serverv1.GetClusterTimescaleDBResponse]
- func (s *MockServer) OnGetToken() *MethodConfigBuilder[*serverv1.GetTokenResponse]
- func (s *MockServer) OnUpdateClusterTimescaleDB() *MethodConfigBuilder[*serverv1.UpdateClusterTimescaleDBResponse]
- func (s *MockServer) Reset()
- type ResponseRegistry
- func (r *ResponseRegistry) CaptureRequest(method string, req proto.Message)
- func (r *ResponseRegistry) GetBehavior(method string) BehaviorFunc
- func (r *ResponseRegistry) GetCapturedRequests(method string) []proto.Message
- func (r *ResponseRegistry) GetError(method string) error
- func (r *ResponseRegistry) GetResponse(method string) proto.Message
- func (r *ResponseRegistry) Reset()
- func (r *ResponseRegistry) SetBehavior(method string, fn BehaviorFunc)
- func (r *ResponseRegistry) SetError(method string, err error)
- func (r *ResponseRegistry) SetResponse(method string, response proto.Message)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BehaviorFunc ¶
BehaviorFunc is a custom function that can be used to implement complex response logic in tests. It receives the request and returns a response or error.
type MethodConfigBuilder ¶
MethodConfigBuilder provides a fluent API for configuring mock responses for a specific RPC method.
func (*MethodConfigBuilder[T]) Return ¶
func (b *MethodConfigBuilder[T]) Return(response T)
Return configures the method to return the given response.
func (*MethodConfigBuilder[T]) ReturnError ¶
func (b *MethodConfigBuilder[T]) ReturnError(err error)
ReturnError configures the method to return the given error.
func (*MethodConfigBuilder[T]) WithBehavior ¶
func (b *MethodConfigBuilder[T]) WithBehavior(fn BehaviorFunc)
WithBehavior configures the method to use a custom behavior function that can implement complex logic.
type MockServer ¶
MockServer wraps httptest.Server with a configuration API for setting up mock RPC responses.
func NewMockBuilderServer ¶
func NewMockBuilderServer(t testing.TB) *MockServer
NewMockBuilderServer creates a new in-process HTTP server that implements the BuilderService and AuthService RPC interfaces. The server URL can be used to create real RPC clients that will interact with the mock server.
The auth service returns a default mock token automatically, so tests don't need to configure authentication unless testing auth-specific scenarios.
Example:
server := testserver.NewMockBuilderServer(t)
defer server.Close()
server.OnGetClusterTimescaleDB().Return(&serverv1.GetClusterTimescaleDBResponse{
Id: "test-id",
})
client := serverv1connect.NewBuilderServiceClient(http.DefaultClient, server.URL)
resp, err := client.GetClusterTimescaleDB(ctx, connect.NewRequest(...))
func (*MockServer) GetCapturedRequests ¶
func (s *MockServer) GetCapturedRequests(methodName string) []proto.Message
GetCapturedRequests returns all requests captured for the given method name. This is useful for test assertions.
func (*MockServer) OnCreateClusterTimescaleDB ¶
func (s *MockServer) OnCreateClusterTimescaleDB() *MethodConfigBuilder[*serverv1.CreateClusterTimescaleDBResponse]
OnCreateClusterTimescaleDB configures the CreateClusterTimescaleDB RPC method.
func (*MockServer) OnDeleteClusterTimescaleDB ¶ added in v1.2.185
func (s *MockServer) OnDeleteClusterTimescaleDB() *MethodConfigBuilder[*serverv1.DeleteClusterTimescaleDBResponse]
OnDeleteClusterTimescaleDB configures the DeleteClusterTimescaleDB RPC method.
func (*MockServer) OnGetClusterTimescaleDB ¶
func (s *MockServer) OnGetClusterTimescaleDB() *MethodConfigBuilder[*serverv1.GetClusterTimescaleDBResponse]
OnGetClusterTimescaleDB configures the GetClusterTimescaleDB RPC method.
func (*MockServer) OnGetToken ¶ added in v1.2.185
func (s *MockServer) OnGetToken() *MethodConfigBuilder[*serverv1.GetTokenResponse]
OnGetToken configures the GetToken RPC method. By default, the mock server returns a valid token automatically. Use this method only if you need to test auth failures or custom token responses.
func (*MockServer) OnUpdateClusterTimescaleDB ¶
func (s *MockServer) OnUpdateClusterTimescaleDB() *MethodConfigBuilder[*serverv1.UpdateClusterTimescaleDBResponse]
OnUpdateClusterTimescaleDB configures the UpdateClusterTimescaleDB RPC method.
func (*MockServer) Reset ¶
func (s *MockServer) Reset()
Reset clears all configured responses, errors, behaviors, and captured requests.
type ResponseRegistry ¶
type ResponseRegistry struct {
// contains filtered or unexported fields
}
ResponseRegistry manages mock responses, errors, and captured requests for RPC methods. It is thread-safe and can be safely used from multiple goroutines.
func NewResponseRegistry ¶
func NewResponseRegistry() *ResponseRegistry
NewResponseRegistry creates a new ResponseRegistry.
func (*ResponseRegistry) CaptureRequest ¶
func (r *ResponseRegistry) CaptureRequest(method string, req proto.Message)
CaptureRequest records an incoming request for the given method.
func (*ResponseRegistry) GetBehavior ¶
func (r *ResponseRegistry) GetBehavior(method string) BehaviorFunc
GetBehavior retrieves the custom behavior function for the given method.
func (*ResponseRegistry) GetCapturedRequests ¶
func (r *ResponseRegistry) GetCapturedRequests(method string) []proto.Message
GetCapturedRequests returns all captured requests for the given method.
func (*ResponseRegistry) GetError ¶
func (r *ResponseRegistry) GetError(method string) error
GetError retrieves the configured error for the given method.
func (*ResponseRegistry) GetResponse ¶
func (r *ResponseRegistry) GetResponse(method string) proto.Message
GetResponse retrieves the configured response for the given method.
func (*ResponseRegistry) Reset ¶
func (r *ResponseRegistry) Reset()
Reset clears all state from the registry.
func (*ResponseRegistry) SetBehavior ¶
func (r *ResponseRegistry) SetBehavior(method string, fn BehaviorFunc)
SetBehavior sets a custom behavior function for the given method.
func (*ResponseRegistry) SetError ¶
func (r *ResponseRegistry) SetError(method string, err error)
SetError configures an error to return for the given method.
func (*ResponseRegistry) SetResponse ¶
func (r *ResponseRegistry) SetResponse(method string, response proto.Message)
SetResponse stores a canned response for the given method.