test: add HTTP API integration tests
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:
parent
f6128689f1
commit
f1cf23745e
2 changed files with 236 additions and 4 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue