The serve command runs internal/server: the API under /api (health, and the CSRF token endpoint) and the WebSocket hub under /ws, over HTTP or TLS, with an optional HTTP to HTTPS redirect, the PROXY protocol from trusted sources and a graceful shutdown that closes WebSockets first. Its middleware covers the client IP behind trusted proxies, request IDs, request logging, panic recovery, global and API rate limits, Host and body size checks, security response headers, compression, CSRF protection (cross-origin checks and signed double-submit tokens) and JSON-only API bodies. Middleware that logs takes a WithLogger option. New server config sections: proxy, csrf, tls, headers and frontend (not used yet), plus max_header_bytes, max_body_bytes, global and API rate limits and HTTP read, write and idle timeouts. Every setting documents what 0 or empty means, and internal/config/README.md is reorganised by section. server.timeouts.read_header must be greater than 0, and a header or idle timeout of 0 means no limit rather than the read timeout. WebSocket upgrades are protected by the Origin check: the server's own origin, or one of csrf.cross_origin_protection.trusted_origins, which the hub now takes with WithTrustedOrigins. wsserve becomes "ws serve". "ws dial" opens a WebSocket on /ws to test it: --ping sends a ping and waits for the pong, --message sends messages and prints the replies, --origin tests the Origin check, and with neither --ping nor --message each line of stdin is sent until Ctrl+C.
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
|
|
"git.avsdev.uk/Go/job-search/internal/cli/cmdtest"
|
|
)
|
|
|
|
// The commands are tested in their own packages under internal/cli. These
|
|
// tests check newRootCmd puts them together.
|
|
|
|
func TestCommandTree(t *testing.T) {
|
|
root := newRootCmd()
|
|
|
|
var names []string
|
|
for _, c := range root.Commands() {
|
|
names = append(names, c.Name())
|
|
}
|
|
|
|
commands := []string{"config", "fetch", "migrate", "version", "ws"}
|
|
for _, want := range commands {
|
|
if !slices.Contains(names, want) {
|
|
t.Errorf("job-search has no %q command; has %v", want, names)
|
|
}
|
|
}
|
|
|
|
for _, flag := range []string{"config", "debug", "quiet"} {
|
|
if root.PersistentFlags().Lookup(flag) == nil {
|
|
t.Errorf("job-search has no --%s flag", flag)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestVersion(t *testing.T) {
|
|
cmdtest.Setup(t, "")
|
|
cmdtest.Contains(t, cmdtest.MustRun(t, newRootCmd, "version"), "JobSearch - ")
|
|
}
|
|
|
|
// A command that loads the configuration runs through the real root: its
|
|
// flags and hooks, and the subcommand beneath it.
|
|
func TestRootRunsCommands(t *testing.T) {
|
|
cmdtest.Setup(t, "logging:\n level: info\n")
|
|
cmdtest.Contains(
|
|
t,
|
|
cmdtest.MustRun(t, newRootCmd, "config", "get", "logging.level"),
|
|
"info <string>",
|
|
)
|
|
cmdtest.Contains(
|
|
t,
|
|
cmdtest.MustRun(t, newRootCmd, "config", "get", "logging.level", "--debug"),
|
|
"debug <string>",
|
|
)
|
|
}
|
|
|
|
func TestUnknownCommand(t *testing.T) {
|
|
cmdtest.Setup(t, "")
|
|
if _, err := cmdtest.Run(t, newRootCmd, "", "nope"); err == nil {
|
|
t.Error("an unknown command was accepted")
|
|
}
|
|
}
|