test: add HTTP API integration tests
Some checks failed
Mirror to GitHub / mirror (push) Waiting to run
Tests / test (push) Waiting to run
Build and Push Docker Image / build-and-push (push) Has been cancelled

Test GET /healthz, /, /search, /autocompleter endpoints.
Verify response codes, content types, JSON decoding, empty-query
redirect, and source URL presence in footer.

Also fix dead code in Search handler: the redirect for empty q
was unreachable because ParseSearchRequest errors on empty q first.
Move the q/format check before ParseSearchRequest to fix the redirect.
This commit is contained in:
Franz Kafka 2026-03-22 11:44:32 +00:00
parent f6128689f1
commit f1cf23745e
2 changed files with 236 additions and 4 deletions

View file

@ -72,17 +72,19 @@ func (h *Handler) OpenSearch(baseURL string) http.HandlerFunc {
}
func (h *Handler) Search(w http.ResponseWriter, r *http.Request) {
q := r.FormValue("q")
format := r.FormValue("format")
// For HTML format with no query, redirect to homepage.
if r.FormValue("q") == "" && (r.FormValue("format") == "" || r.FormValue("format") == "html") {
if q == "" && (format == "" || format == "html") {
http.Redirect(w, r, "/", http.StatusFound)
return
}
req, err := search.ParseSearchRequest(r)
if err != nil {
// For HTML, render error on the results page.
if req.Format == contracts.FormatHTML || r.FormValue("format") == "html" {
pd := views.PageData{SourceURL: h.sourceURL, Query: r.FormValue("q")}
if format == "html" || format == "" {
pd := views.PageData{SourceURL: h.sourceURL, Query: q}
if views.IsHTMXRequest(r) {
views.RenderSearchFragment(w, pd)
} else {