~bigbes/huntsman

ref: 766fa8055977fbe223afd8a84bcf37a3d13bb1ce huntsman/internal/domain/search/handler_test.go -rw-r--r-- 5.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package search

import (
	"encoding/json"
	"encoding/xml"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/go-chi/chi/v5"
)

// newTestHandler builds a router with the search handler mounted at /.
func newTestHandler(t *testing.T) http.Handler {
	t.Helper()
	svc, err := NewService("gh", "https://example.com")
	if err != nil {
		t.Fatalf("NewService: %v", err)
	}
	r := chi.NewRouter()
	NewHandler(svc).RegisterRoutes(r)
	return r
}

func TestSearchRedirects(t *testing.T) {
	h := newTestHandler(t)

	cases := []struct {
		name    string
		query   string
		wantLoc string
	}{
		{"unknown prefix uses default gh", "foo bar", "https://github.com/search?q=foo+bar"},
		{"ud prefix routes to urban dictionary", "ud meme", "https://www.urbandictionary.com/define.php?term=meme"},
		{"steam prefix routes to steam", "steam half life", "https://store.steampowered.com/search/?term=half+life"},
		{"colon prefix", "ud:lit", "https://www.urbandictionary.com/define.php?term=lit"},
		{"empty query goes to default home", "", "https://github.com/"},
		{"bare prefix goes to provider home", "ud", "https://www.urbandictionary.com/"},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			req := httptest.NewRequestWithContext(t.Context(), "GET", "/search?q="+encodeQ(tc.query), http.NoBody)
			rr := httptest.NewRecorder()
			h.ServeHTTP(rr, req)

			if rr.Code != http.StatusFound {
				t.Fatalf("status = %d, want 302", rr.Code)
			}
			if got := rr.Header().Get("Location"); got != tc.wantLoc {
				t.Errorf("Location = %q\n  want %q", got, tc.wantLoc)
			}
		})
	}
}

// encodeQ percent-encodes spaces only — enough for these tests.
func encodeQ(s string) string {
	return strings.ReplaceAll(s, " ", "+")
}

func TestListProvidersJSON(t *testing.T) {
	h := newTestHandler(t)
	rr := httptest.NewRecorder()
	h.ServeHTTP(rr, httptest.NewRequestWithContext(t.Context(), "GET", "/providers", http.NoBody))

	if rr.Code != http.StatusOK {
		t.Fatalf("status = %d", rr.Code)
	}
	if got := rr.Header().Get("Content-Type"); got != "application/json" {
		t.Errorf("content-type = %q", got)
	}

	var body struct {
		Default   string `json:"default"`
		Providers []struct {
			ID            string `json:"id"`
			Name          string `json:"name"`
			Description   string `json:"description"`
			HomeURL       string `json:"home_url"`
			OpenSearchURL string `json:"opensearch_url"`
		} `json:"providers"`
	}
	if err := json.Unmarshal(rr.Body.Bytes(), &body); err != nil {
		t.Fatalf("decode: %v", err)
	}

	if body.Default != "gh" {
		t.Errorf("default = %q", body.Default)
	}
	if len(body.Providers) != 3 {
		t.Fatalf("expected 3 providers, got %d", len(body.Providers))
	}
	// Order should match AllProviders().
	wantIDs := []string{"ud", "gh", "steam"}
	for i, want := range wantIDs {
		if body.Providers[i].ID != want {
			t.Errorf("providers[%d].ID = %q, want %q", i, body.Providers[i].ID, want)
		}
	}
	// Per-provider OSD URL is built from the public URL.
	if body.Providers[0].OpenSearchURL != "https://example.com/opensearch/ud.xml" {
		t.Errorf("opensearch_url = %q", body.Providers[0].OpenSearchURL)
	}
}

func TestOpenSearchRouterXML(t *testing.T) {
	h := newTestHandler(t)
	rr := httptest.NewRecorder()
	h.ServeHTTP(rr, httptest.NewRequestWithContext(t.Context(), "GET", "/opensearch.xml", http.NoBody))

	if rr.Code != http.StatusOK {
		t.Fatalf("status = %d", rr.Code)
	}
	if got := rr.Header().Get("Content-Type"); got != "application/opensearchdescription+xml" {
		t.Errorf("content-type = %q", got)
	}
	body := rr.Body.String()
	if !strings.Contains(body, "<?xml") {
		t.Errorf("body missing XML prolog: %q", body)
	}
	if !strings.Contains(body, "https://example.com/search?q={searchTerms}") {
		t.Errorf("body missing router template: %q", body)
	}

	var osd OpenSearchDescription
	if err := xml.Unmarshal(rr.Body.Bytes(), &osd); err != nil {
		t.Fatalf("unmarshal OSD: %v", err)
	}
	if osd.ShortName != "huntsman" {
		t.Errorf("ShortName = %q", osd.ShortName)
	}
}

func TestOpenSearchProviderXML(t *testing.T) {
	h := newTestHandler(t)
	rr := httptest.NewRecorder()
	h.ServeHTTP(rr, httptest.NewRequestWithContext(t.Context(), "GET", "/opensearch/ud.xml", http.NoBody))

	if rr.Code != http.StatusOK {
		t.Fatalf("status = %d", rr.Code)
	}

	var osd OpenSearchDescription
	if err := xml.Unmarshal(rr.Body.Bytes(), &osd); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if osd.ShortName != "Urban Dictionary" {
		t.Errorf("ShortName = %q", osd.ShortName)
	}
	if len(osd.URLs) != 1 || !strings.Contains(osd.URLs[0].Template, "{searchTerms}") {
		t.Errorf("template missing {searchTerms}: %+v", osd.URLs)
	}
	if osd.Image == nil || osd.Image.Value == "" {
		t.Errorf("expected Image with icon URL")
	}
}

func TestOpenSearchProviderUnknown(t *testing.T) {
	h := newTestHandler(t)
	rr := httptest.NewRecorder()
	h.ServeHTTP(rr, httptest.NewRequestWithContext(t.Context(), "GET", "/opensearch/yahoo.xml", http.NoBody))

	if rr.Code != http.StatusNotFound {
		t.Fatalf("status = %d, want 404", rr.Code)
	}
	if got := rr.Header().Get("Content-Type"); got != "application/problem+json" {
		t.Errorf("content-type = %q", got)
	}
}