-
Notifications
You must be signed in to change notification settings - Fork 4
/
client_test.go
196 lines (188 loc) · 5.1 KB
/
client_test.go
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package pkggodevclient
import (
"errors"
"net"
"net/http"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func withHTTPServer(pattern string, handler http.HandlerFunc, f func(addr string)) {
m := http.NewServeMux()
m.HandleFunc(pattern, handler)
// use a random ephemeral port, which is passed into the func
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
shutdown := &sync.WaitGroup{}
shutdown.Add(1)
stopped := &sync.WaitGroup{}
stopped.Add(2)
srv := &http.Server{Handler: handler}
go func() {
err := srv.Serve(listener)
if !errors.Is(err, http.ErrServerClosed) {
println("err")
}
stopped.Done()
}()
go func() {
shutdown.Wait()
err := srv.Close()
if err != nil {
panic(err)
}
stopped.Done()
}()
f(listener.Addr().String())
shutdown.Done()
stopped.Wait()
}
func TestClient_ImportedBy(t *testing.T) {
cases := []struct {
name string
html string
httpErrCode int
expectImports []string
expectErrContains string
}{
{
name: "happy case",
html: `
<html><body>
<div class="u-breakWord">foo</div>
<div class="u-breakWord">bar</div>
</body></html>
`,
expectImports: []string{"foo", "bar"},
},
{
name: "no results",
html: "",
expectImports: nil,
},
{
name: "http error returns an error",
html: "",
httpErrCode: 500,
expectErrContains: "Internal Server Error",
},
{
name: "returns error on 404",
httpErrCode: 404,
expectErrContains: "not found on pkg.go.dev",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
withHTTPServer("/", func(rw http.ResponseWriter, r *http.Request) {
if c.httpErrCode != 0 {
rw.WriteHeader(c.httpErrCode)
return
}
rw.Write([]byte(c.html))
}, func(addr string) {
client := New(WithBaseURL("http://" + addr))
importedBy, err := client.ImportedBy(ImportedByRequest{Package: "somepackage"})
if c.expectErrContains != "" {
assert.Contains(t, err.Error(), c.expectErrContains)
return
}
assert.NoError(t, err)
assert.Equal(t, "somepackage", importedBy.Package)
assert.Equal(t, c.expectImports, importedBy.ImportedBy)
})
})
}
}
func TestClient_DescribePackage(t *testing.T) {
cases := []struct {
name string
html string
httpCode int
expectErrContains string
expectPackage Package
}{
{
name: "happy case",
html: `
<html>
<div data-test-id="UnitHeader-version"><div> fooversion </div>
<div data-test-id="UnitHeader-licenses"><div> foolicense </div>
<div class="UnitMeta"><ul>
<li><img alt="checked"/></li>
<li><img alt="checked"/></li>
<li><img alt="checked"/></li>
<li><img alt="checked"/></li>
</ul></div>
<div class="UnitMeta-repo"><div>
foorepo </div>
<div data-test-id="UnitHeader-commitTime"> Published: Feb 3, 2000 </div>
<div class="UnitHeader-titleHeading">Heading</div>
<div>package</div>
<div>module</div>
</div></html>`,
expectPackage: Package{
Package: "somepackage",
Version: "fooversion",
License: "foolicense",
HasValidGoModFile: true,
HasRedistributableLicense: true,
HasTaggedVersion: true,
HasStableVersion: true,
Repository: "foorepo",
Published: "2000-02-03",
IsModule: true,
IsPackage: true,
},
},
{
name: "package but not module",
html: `<div class="UnitHeader-titleHeading">Heading</div><div>package</div><div>something else</div>`,
expectPackage: Package{Package: "somepackage", IsPackage: true},
},
{
name: "returns an error if IsPackage is false",
html: `<div class="UnitHeader-titleHeading">Heading</div><div>module</div><div>something else</div>`,
expectErrContains: "IsPackage=false after parsing page for 'somepackage', this probably indicates a parsing bug",
},
{
name: "returns an error if HTTP req fails",
httpCode: 500,
expectErrContains: "Internal Server Error",
},
{
name: "returns an error if the published date can't be parsed",
html: `<div data-test-id="UnitHeader-commitTime"> Published: February 333, 20 </div>`,
expectErrContains: "parsing time",
},
{
name: "returns error on 404",
httpCode: 404,
expectErrContains: "not found on pkg.go.dev",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
withHTTPServer("/", func(rw http.ResponseWriter, r *http.Request) {
if c.httpCode != 0 {
rw.WriteHeader(c.httpCode)
return
}
rw.Write([]byte(c.html))
}, func(addr string) {
client := New(WithBaseURL("http://" + addr))
pkg, err := client.DescribePackage(DescribePackageRequest{
Package: "somepackage",
})
if c.expectErrContains != "" {
assert.Contains(t, err.Error(), c.expectErrContains)
return
}
assert.NoError(t, err)
assert.Equal(t, c.expectPackage, *pkg)
})
})
}
}