-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubdocs_test.go
More file actions
137 lines (128 loc) · 3.62 KB
/
Copy pathgithubdocs_test.go
File metadata and controls
137 lines (128 loc) · 3.62 KB
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
package githubdocs_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/tamnd/githubdocs-cli/githubdocs"
)
func testClient(baseURL string) *githubdocs.Client {
cfg := githubdocs.DefaultConfig()
cfg.BaseURL = baseURL
cfg.Rate = 0 // no pacing in tests
cfg.Retries = 3
return githubdocs.NewClient(cfg)
}
func TestSearch(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("User-Agent") == "" {
t.Error("request carried no User-Agent")
}
if r.URL.Path != "/api/search/v1" {
t.Errorf("unexpected path %q", r.URL.Path)
}
q := r.URL.Query().Get("query")
if q == "" {
t.Error("missing query param")
}
resp := map[string]any{
"meta": map[string]any{
"found": map[string]any{"value": 2},
"page": 1,
"size": 10,
},
"hits": []map[string]any{
{
"id": "abc123",
"url": "/en/actions/overview",
"title": "GitHub <mark>Actions</mark> overview",
"breadcrumbs": "GitHub Actions",
"highlights": map[string]any{
"title": []string{"GitHub <mark>Actions</mark> overview"},
"content": []string{"About GitHub <mark>Actions</mark>."},
},
},
{
"id": "def456",
"url": "/en/actions/concepts",
"title": "GitHub <mark>Actions</mark> concepts",
"breadcrumbs": "GitHub Actions / Concepts",
"highlights": map[string]any{
"title": []string{"GitHub <mark>Actions</mark> concepts"},
"content": []string{"Core concepts for GitHub <mark>Actions</mark>."},
},
},
},
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
}))
defer srv.Close()
c := testClient(srv.URL)
articles, err := c.Search(context.Background(), "actions", 10)
if err != nil {
t.Fatal(err)
}
if len(articles) != 2 {
t.Fatalf("got %d articles, want 2", len(articles))
}
if articles[0].Title != "GitHub Actions overview" {
t.Errorf("title = %q, want mark tags stripped", articles[0].Title)
}
if articles[0].Breadcrumb != "GitHub Actions" {
t.Errorf("breadcrumb = %q", articles[0].Breadcrumb)
}
if articles[0].URL != srv.URL+"/en/actions/overview" {
t.Errorf("url = %q", articles[0].URL)
}
}
func TestSearchRetriesOn503(t *testing.T) {
var hits int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits++
if hits < 3 {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
resp := map[string]any{
"meta": map[string]any{"found": map[string]any{"value": 0}, "page": 1, "size": 10},
"hits": []any{},
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
}))
defer srv.Close()
c := testClient(srv.URL)
start := time.Now()
_, err := c.Search(context.Background(), "test", 0)
if err != nil {
t.Fatal(err)
}
if hits != 3 {
t.Errorf("server saw %d hits, want 3", hits)
}
if time.Since(start) < 500*time.Millisecond {
t.Error("retries did not back off")
}
}
func TestSearchEmptyResults(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp := map[string]any{
"meta": map[string]any{"found": map[string]any{"value": 0}, "page": 1, "size": 10},
"hits": []any{},
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
}))
defer srv.Close()
c := testClient(srv.URL)
articles, err := c.Search(context.Background(), "xyzzy-no-match", 10)
if err != nil {
t.Fatal(err)
}
if len(articles) != 0 {
t.Errorf("got %d articles, want 0", len(articles))
}
}