96 lines
3.3 KiB
Go
96 lines
3.3 KiB
Go
package research
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestSearchParsesSearXNG(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/search" {
|
|
t.Fatalf("unexpected path %q", r.URL.Path)
|
|
}
|
|
if got := r.URL.Query().Get("format"); got != "json" {
|
|
t.Fatalf("unexpected format %q", got)
|
|
}
|
|
if got := r.URL.Query().Get("language"); got != "de-DE" {
|
|
t.Fatalf("unexpected language %q", got)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"results":[{"title":"Vendor note","url":"https://example.test/a","content":"Relevant evidence"}]}`))
|
|
}))
|
|
defer srv.Close()
|
|
got, diagnostic, err := New(srv.URL).SearchDetailed(context.Background(), "vpn", 3)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 1 || got[0].URL != "https://example.test/a" || got[0].Content != "Relevant evidence" {
|
|
t.Fatalf("unexpected result: %#v", got)
|
|
}
|
|
if !diagnostic.OK || diagnostic.HTTPStatus != http.StatusOK || diagnostic.ResultCount != 1 {
|
|
t.Fatalf("unexpected diagnostic: %+v", diagnostic)
|
|
}
|
|
}
|
|
|
|
func TestSearchDetailedLanguageUsesEnglishLocale(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if got := r.URL.Query().Get("language"); got != "en-US" {
|
|
t.Fatalf("unexpected language %q", got)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"results":[]}`))
|
|
}))
|
|
defer srv.Close()
|
|
if _, _, err := New(srv.URL).SearchDetailedLanguage(context.Background(), "audit logging", 2, "en"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestSearchAcceptsBaseURLWithSearchPath(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/search" {
|
|
t.Fatalf("search path duplicated: %q", r.URL.Path)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"results":[]}`))
|
|
}))
|
|
defer srv.Close()
|
|
if _, err := New(srv.URL+"/search").Search(context.Background(), "test", 1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestSearchReportsHTTPBody(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
w.WriteHeader(http.StatusForbidden)
|
|
_, _ = w.Write([]byte("JSON format is disabled"))
|
|
}))
|
|
defer srv.Close()
|
|
_, diagnostic, err := New(srv.URL).SearchDetailed(context.Background(), "test", 1)
|
|
if err == nil || !strings.Contains(err.Error(), "HTTP 403") || !strings.Contains(err.Error(), "JSON format is disabled") {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if diagnostic.ErrorKind != "http_status" || diagnostic.HTTPStatus != http.StatusForbidden {
|
|
t.Fatalf("unexpected diagnostic: %+v", diagnostic)
|
|
}
|
|
}
|
|
|
|
func TestSearchExplainsInvalidJSON(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html")
|
|
_, _ = w.Write([]byte("<html>search page</html>"))
|
|
}))
|
|
defer srv.Close()
|
|
_, diagnostic, err := New(srv.URL).SearchDetailed(context.Background(), "test", 1)
|
|
if err == nil || !strings.Contains(err.Error(), "settings.yml") {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if diagnostic.ErrorKind != "invalid_json" {
|
|
t.Fatalf("unexpected diagnostic: %+v", diagnostic)
|
|
}
|
|
}
|