connecttest is a lightweight DSL for testing ConnectRPC handlers.
It executes handlers directly using Go's httptest package, automatically decodes connect.Error responses, and
provides a fluent, expressive API for asserting response behavior.
- Runs Connect handlers directly without a network layer
- Automatically unmarshals
connect.ErrorJSON responses - Provides fluent assertion methods:
ExpectStatusExpectHeaderOut(for unmarshalling protobuf responses)Err(returns*connect.Errorif present)
- Supports structured error detail decoding (
connect.ErrorDetail) - Compatible with
connect.WithInterceptors(...)
package handler_test
import (
"net/http"
"testing"
"connectrpc.com/connect"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mickamy/connecttest"
authv1 "github.com/mickamy/connecttest-example/gen/auth/v1"
"github.com/mickamy/connecttest-example/gen/github.com/mickamy/connecttest-example/gen/auth/v1/authv1connect"
"github.com/mickamy/connecttest-example/internal/domain/session/handler"
)
func TestSession_SignIn(t *testing.T) {
t.Parallel()
var out authv1.SignInResponse
_, server := authv1connect.NewSessionServiceHandler(handler.NewSession())
ct := connecttest.
New(t, server).
Procedure(authv1connect.SessionServiceSignInProcedure).
In(&authv1.SignInRequest{
Email: "[email protected]",
Password: "password123",
}).
Do().
ExpectStatus(http.StatusOK)
ct.Out(&out)
require.NotZero(t, out.Tokens)
assert.NotZero(t, out.Tokens.Access)
assert.NotZero(t, out.Tokens.Refresh)
}ct := connecttest.New(t, handler)ct.Procedure("/package.Service/Method").Header("X-Test", "1").In(&req)ct.Do().ExpectStatus(http.StatusOK).Out(&res)if connErr := ct.Err(); connErr != nil {
fmt.Println(connErr.Code(), connErr.Message())
}