Split ws from jobsearch as a separate test utility rather than a feature of jobsearch

This commit is contained in:
bwilliams committed 2026-10-16 08:58:25 +01:00
1 parent b714ee14d5
commit 5e61c11eb9
9 files changed
+148 -38

No files matched your search

+7 -20
View File
@@ -1,30 +1,17 @@
// Package ws holds the "ws" command: "ws serve" runs the plain TCP server for
// websocket hub clients, and "ws dial" tests the HTTP server's /ws endpoint.
// Package ws holds the ws-test commands: "serve" runs the plain TCP server
// for websocket hub clients, and "dial" tests the HTTP server's /ws endpoint.
package ws
import (
"log/slog"
"github.com/spf13/cobra"
"git.avsdev.uk/Go/job-search/internal/cli"
)
// NewCommand returns the "ws" command, for the WebSocket servers.
func NewCommand(opts *cli.Options) *cobra.Command {
cmd := &cobra.Command{
Use: "ws",
Short: "WebSocket servers and tools",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("command persistent pre-run", "command", "ws")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("command persistent post-run", "command", "ws")
},
// NewCommands returns the ws-test commands, "serve" and "dial".
func NewCommands(opts *cli.Options) []*cobra.Command {
return []*cobra.Command{
commandServe(opts),
commandDial(opts),
}
cmd.AddCommand(commandServe(opts))
cmd.AddCommand(commandDial(opts))
return cmd
}
+4 -4
View File
@@ -40,7 +40,7 @@ type dialSettings struct {
timeout time.Duration
}
// commandDial returns the "ws dial" command, which opens a WebSocket on the
// commandDial returns the "dial" command, which opens a WebSocket on the
// HTTP server's /ws path, sends messages and prints the replies.
func commandDial(opts *cli.Options) *cobra.Command {
cmd := &cobra.Command{
@@ -67,14 +67,14 @@ server.tls.enabled is set). Set --origin to test the Origin check: an upgrade
from an origin that isn't the server's own or one of
server.csrf.cross_origin_protection.trusted_origins is refused with 403.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("command persistent pre-run", "command", "ws.dial")
slog.Debug("command persistent pre-run", "command", "dial")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("command persistent post-run", "command", "ws.dial")
slog.Debug("command persistent post-run", "command", "dial")
},
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
slog.Debug("command run", "command", "ws.dial")
slog.Debug("command run", "command", "dial")
settings, err := dialFlags(cmd)
if err != nil {
+7 -7
View File
@@ -24,7 +24,7 @@ import (
func newRoot() *cobra.Command {
opts := &cli.Options{}
return cmdtest.NewRoot(opts, NewCommand(opts))
return cmdtest.NewRoot(opts, NewCommands(opts)...)
}
// wsServer serves a hub that trusts https://admin.example.com on a test HTTP
@@ -59,7 +59,7 @@ func TestDialPing(t *testing.T) {
_, url := wsServer(t)
out := cmdtest.MustRun(
t, newRoot, "ws", "dial", "--url", url, "--message", ping,
t, newRoot, "dial", "--url", url, "--message", ping,
)
cmdtest.Contains(
t,
@@ -76,7 +76,7 @@ func TestDialPingFlag(t *testing.T) {
cmdtest.Setup(t, "")
_, url := wsServer(t)
out := cmdtest.MustRun(t, newRoot, "ws", "dial", "--url", url, "--ping")
out := cmdtest.MustRun(t, newRoot, "dial", "--url", url, "--ping")
cmdtest.Contains(
t,
out,
@@ -111,7 +111,7 @@ func TestDialPingTimeout(t *testing.T) {
t,
newRoot,
"",
"ws", "dial", "--url", url, "--ping", "--timeout", "100ms",
"dial", "--url", url, "--ping", "--timeout", "100ms",
)
cmdtest.ErrContains(t, err, "no pong within 100ms")
}
@@ -124,7 +124,7 @@ func TestDialMessages(t *testing.T) {
out := cmdtest.MustRun(
t,
newRoot,
"ws", "dial", "--url", url,
"dial", "--url", url,
"--message", `{"type":"echo","id":"7","payload":{"a":1}}`,
"--message", `{"type":"nonsense","id":"8"}`,
)
@@ -145,7 +145,7 @@ func TestDialOrigin(t *testing.T) {
t,
newRoot,
"",
"ws", "dial", "--url", url, "--message", ping,
"dial", "--url", url, "--message", ping,
"--origin", "https://evil.example",
)
cmdtest.ErrContains(t, err, "upgrade refused: 403 Forbidden")
@@ -153,7 +153,7 @@ func TestDialOrigin(t *testing.T) {
out := cmdtest.MustRun(
t,
newRoot,
"ws", "dial", "--url", url, "--message", ping,
"dial", "--url", url, "--message", ping,
"--origin", "https://admin.example.com",
)
cmdtest.Contains(t, out, "connected (101 Switching Protocols)")
+4 -4
View File
@@ -18,20 +18,20 @@ import (
"git.avsdev.uk/Go/job-search/internal/websocket/tcpserver"
)
// commandServe returns the "ws serve" command, which runs a plain TCP server
// commandServe returns the "serve" command, which runs a plain TCP server
// for websocket hub clients (see tcpserver).
func commandServe(opts *cli.Options) *cobra.Command {
cmd := &cobra.Command{
Use: "serve",
Short: "Run JobSearch TCP Websocket server",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("command persistent pre-run", "command", "ws.serve")
slog.Debug("command persistent pre-run", "command", "serve")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("command persistent post-run", "command", "ws.serve")
slog.Debug("command persistent post-run", "command", "serve")
},
RunE: func(cmd *cobra.Command, args []string) error {
slog.Debug("command run", "command", "ws.serve")
slog.Debug("command run", "command", "serve")
addrOverride, _ := cmd.Flags().GetString("listen")
hostOverride, _ := cmd.Flags().GetString("host")