~bigbes/huntsman

ref: 766fa8055977fbe223afd8a84bcf37a3d13bb1ce huntsman/internal/domain/search/opensearch_test.go -rw-r--r-- 2.0 KiB
766fa805 — Eugene Blikh Add BSD 2-Clause license 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package search

import (
	"encoding/xml"
	"strings"
	"testing"
)

func TestMarshalEmitsXMLProlog(t *testing.T) {
	osd := DescriptionForRouter("https://example.com")
	body, err := Marshal(osd)
	if err != nil {
		t.Fatalf("Marshal: %v", err)
	}
	if !strings.HasPrefix(string(body), "<?xml") {
		t.Errorf("missing XML prolog: %q", body[:min(40, len(body))])
	}
	// Round-trip should succeed.
	var got OpenSearchDescription
	if err := xml.Unmarshal(body, &got); err != nil {
		t.Fatalf("unmarshal round-trip: %v", err)
	}
	if got.ShortName != osd.ShortName {
		t.Errorf("ShortName lost in round-trip: %q vs %q", got.ShortName, osd.ShortName)
	}
}

func TestDescriptionForRouterStripsTrailingSlash(t *testing.T) {
	osd := DescriptionForRouter("https://example.com///")
	if osd.URLs[0].Template != "https://example.com/search?q={searchTerms}" {
		t.Errorf("template = %q", osd.URLs[0].Template)
	}
	if osd.SearchForm != "https://example.com/" {
		t.Errorf("SearchForm = %q", osd.SearchForm)
	}
}

func TestDescriptionForProviderConvertsPlaceholder(t *testing.T) {
	p := Provider{
		ID:        "x",
		Name:      "X",
		SearchURL: "https://x.example/?q={q}",
		HomeURL:   "https://x.example/",
	}
	osd := DescriptionForProvider(p)
	if osd.URLs[0].Template != "https://x.example/?q={searchTerms}" {
		t.Errorf("template = %q", osd.URLs[0].Template)
	}
	if osd.Image != nil {
		t.Errorf("Image should be nil when IconURL empty, got %+v", osd.Image)
	}
}

func TestDescriptionForProviderIncludesIcon(t *testing.T) {
	p := Provider{
		ID:        "x",
		Name:      "X",
		SearchURL: "https://x.example/?q={q}",
		HomeURL:   "https://x.example/",
		IconURL:   "https://x.example/favicon.ico",
	}
	osd := DescriptionForProvider(p)
	if osd.Image == nil {
		t.Fatal("Image should not be nil")
	}
	if osd.Image.Value != p.IconURL {
		t.Errorf("Image.Value = %q", osd.Image.Value)
	}
	if osd.Image.Width != 16 || osd.Image.Height != 16 {
		t.Errorf("Image dimensions = %dx%d", osd.Image.Width, osd.Image.Height)
	}
}