Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
62 lines
1.8 KiB
Go
62 lines
1.8 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestQuerySearchPrefersQ(t *testing.T) {
|
|
r := httptest.NewRequest(http.MethodGet, "/api/products?q=alpha&search=beta", nil)
|
|
if got := QuerySearch(r); got != "alpha" {
|
|
t.Fatalf("got %q want alpha", got)
|
|
}
|
|
}
|
|
|
|
func TestQuerySearchFallsBackToSearch(t *testing.T) {
|
|
r := httptest.NewRequest(http.MethodGet, "/api/products?search=%20widget%20", nil)
|
|
if got := QuerySearch(r); got != "widget" {
|
|
t.Fatalf("got %q want widget", got)
|
|
}
|
|
}
|
|
|
|
func TestQuerySearchEmpty(t *testing.T) {
|
|
r := httptest.NewRequest(http.MethodGet, "/api/products", nil)
|
|
if got := QuerySearch(r); got != "" {
|
|
t.Fatalf("got %q want empty", got)
|
|
}
|
|
}
|
|
|
|
func TestQueryTruthy(t *testing.T) {
|
|
cases := []struct {
|
|
url string
|
|
key string
|
|
want bool
|
|
}{
|
|
{"/api/products", "detailed", false},
|
|
{"/api/products?detailed=", "detailed", false},
|
|
{"/api/products?detailed=0", "detailed", false},
|
|
{"/api/products?detailed=false", "detailed", false},
|
|
{"/api/products?detailed=1", "detailed", true},
|
|
{"/api/products?detailed=true", "detailed", true},
|
|
{"/api/products?detailed=YES", "detailed", true},
|
|
{"/api/products?detailed=on", "detailed", true},
|
|
{"/api/products?detailed=%201%20", "detailed", true},
|
|
}
|
|
for _, tc := range cases {
|
|
r := httptest.NewRequest(http.MethodGet, tc.url, nil)
|
|
if got := QueryTruthy(r, tc.key); got != tc.want {
|
|
t.Fatalf("%s: got %v want %v", tc.url, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestQueryDetailed(t *testing.T) {
|
|
if QueryDetailed(httptest.NewRequest(http.MethodGet, "/api/products?limit=200", nil)) {
|
|
t.Fatal("default list must be lean (detailed=false)")
|
|
}
|
|
if !QueryDetailed(httptest.NewRequest(http.MethodGet, "/api/products?detailed=1&limit=200", nil)) {
|
|
t.Fatal("detailed=1 must opt into heavy fields")
|
|
}
|
|
}
|