gen/client/v1: add the generated HTTP client

Generate a strongly-typed Go client (oapi-codegen) from the emitted spec, committed under gen/client/v1 as package clientv1. Tests exercise it against the in-memory Huma server over both TCP and the unix socket.
This commit is contained in:
Kristoffer Dalby
2026-06-19 06:13:14 +00:00
parent 1572ac551d
commit ba90048cfb
3 changed files with 4839 additions and 0 deletions
File diff suppressed because it is too large Load Diff
+50
View File
@@ -0,0 +1,50 @@
package hscontrol
import (
"context"
"net/http"
"net/http/httptest"
"testing"
clientv1 "github.com/juanfont/headscale/gen/client/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestAPIV1GeneratedClient smoke-tests the generated client against the Huma
// service over a real HTTP server, exercising the typed request/response path
// end to end.
func TestAPIV1GeneratedClient(t *testing.T) {
app := createTestApp(t)
srv := httptest.NewServer(newHumaTestHandler(app))
t.Cleanup(srv.Close)
client, err := clientv1.NewClientWithResponses(srv.URL)
require.NoError(t, err)
ctx := context.Background()
health, err := client.HealthWithResponse(ctx)
require.NoError(t, err)
require.Equal(t, http.StatusOK, health.StatusCode())
require.NotNil(t, health.JSON200)
assert.True(t, health.JSON200.DatabaseConnectivity)
name := "alice"
created, err := client.CreateUserWithResponse(ctx, clientv1.CreateUserJSONRequestBody{
Name: &name,
})
require.NoError(t, err)
require.Equal(t, http.StatusOK, created.StatusCode())
require.NotNil(t, created.JSON200)
require.NotNil(t, created.JSON200.User)
assert.Equal(t, "alice", created.JSON200.User.Name)
assert.Equal(t, "1", created.JSON200.User.Id)
listed, err := client.ListUsersWithResponse(ctx, &clientv1.ListUsersParams{})
require.NoError(t, err)
require.Equal(t, http.StatusOK, listed.StatusCode())
require.NotNil(t, listed.JSON200)
require.Len(t, listed.JSON200.Users, 1)
assert.Equal(t, "alice", listed.JSON200.Users[0].Name)
}
+61
View File
@@ -0,0 +1,61 @@
package hscontrol
import (
"context"
"net"
"net/http"
"path/filepath"
"testing"
clientv1 "github.com/juanfont/headscale/gen/client/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestAPIV1SocketClient proves the CLI's local transport: the Huma handler over
// a unix socket reached by an http.Client that dials it, the same wiring as the
// server's socket listener and the CLI's newSocketClient, on local trust.
func TestAPIV1SocketClient(t *testing.T) {
app := createTestApp(t)
socketPath := filepath.Join(t.TempDir(), "headscale.sock")
lis, err := new(net.ListenConfig).Listen(context.Background(), "unix", socketPath)
require.NoError(t, err)
srv := &http.Server{Handler: newHumaTestHandler(app)} //nolint:gosec
go func() { _ = srv.Serve(lis) }()
t.Cleanup(func() { _ = srv.Close() })
httpClient := &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, "unix", socketPath)
},
},
}
client, err := clientv1.NewClientWithResponses(
"http://local",
clientv1.WithHTTPClient(httpClient),
)
require.NoError(t, err)
ctx := context.Background()
health, err := client.HealthWithResponse(ctx)
require.NoError(t, err)
require.Equal(t, http.StatusOK, health.StatusCode())
require.NotNil(t, health.JSON200)
assert.True(t, health.JSON200.DatabaseConnectivity)
name := "socket-user"
created, err := client.CreateUserWithResponse(ctx, clientv1.CreateUserJSONRequestBody{Name: &name})
require.NoError(t, err)
require.Equal(t, http.StatusOK, created.StatusCode())
require.NotNil(t, created.JSON200)
assert.Equal(t, "socket-user", created.JSON200.User.Name)
}