-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseragent_test.go
More file actions
96 lines (77 loc) · 2.27 KB
/
Copy pathuseragent_test.go
File metadata and controls
96 lines (77 loc) · 2.27 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
package publiccode
import (
"net/http"
"net/http/httptest"
"net/url"
"slices"
"sync"
"testing"
)
// userAgentRecorder is a server recording the User-Agent of every request it
// receives.
type userAgentRecorder struct {
*httptest.Server
mu sync.Mutex
seen []string
}
// newUserAgentRecorder starts a recording server, stopped when the test ends.
func newUserAgentRecorder(t *testing.T) *userAgentRecorder {
t.Helper()
recorder := &userAgentRecorder{}
recorder.Server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
recorder.mu.Lock()
recorder.seen = append(recorder.seen, r.UserAgent())
recorder.mu.Unlock()
w.Header().Set("Content-Type", "text/yaml")
_, _ = w.Write([]byte("publiccodeYmlVersion: \"0.4\"\n"))
}))
t.Cleanup(recorder.Close)
return recorder
}
// userAgents returns the User-Agents recorded so far.
func (r *userAgentRecorder) userAgents() []string {
r.mu.Lock()
defer r.mu.Unlock()
return slices.Clone(r.seen)
}
// TestUserAgentIsSentOnEveryRequestPath covers the two ways the parser reaches
// the network: the request it builds itself in Parse(), and the ones built by
// the internal httpclient during the external checks.
func TestUserAgentIsSentOnEveryRequestPath(t *testing.T) {
cases := map[string]string{
"default": UserAgent(),
"override": "harvester/2.0 (+https://example.org)",
}
for name, want := range cases {
t.Run(name, func(t *testing.T) {
srv := newUserAgentRecorder(t)
config := ParserConfig{AllowNetworkToPrivateHosts: true}
if name == "override" {
config.UserAgent = want
}
p, err := NewParser(config)
if err != nil {
t.Fatal(err)
}
// Parse() builds its request itself.
_, _ = p.Parse(srv.URL + "/publiccode.yml")
// The external checks go through the internal httpclient.
u, err := url.Parse(srv.URL + "/anything")
if err != nil {
t.Fatal(err)
}
if _, err = p.isReachable(*u); err != nil {
t.Fatalf("unexpected error: %v", err)
}
seen := srv.userAgents()
if len(seen) < 2 {
t.Fatalf("expected both request paths to reach the server, got %d request(s)", len(seen))
}
for _, got := range seen {
if got != want {
t.Errorf("User-Agent received by the server: got %q, want %q", got, want)
}
}
})
}
}