-
Notifications
You must be signed in to change notification settings - Fork 0
/
packages_test.go
76 lines (58 loc) · 1.83 KB
/
packages_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
package uiiclient
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPackages(t *testing.T) {
assert := assert.New(t)
t.Run("success", func(t *testing.T) {
c, err := NewClient("my-token")
assert.NoError(err)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := os.ReadFile("testdata/packages.json")
assert.NoError(err)
_, _ = w.Write(b)
}))
c.url, err = url.Parse(server.URL)
assert.NoError(err)
actual, err := c.Packages(PackageArgs{Distribution: "test", Version: "A"}, PackageOpts{})
assert.NoError(err)
expected := []string{}
b, err := os.ReadFile("testdata/TestPackages_success.golden")
assert.NoError(err)
err = json.Unmarshal(b, &expected)
assert.NoError(err)
assert.Equal(expected, actual)
})
t.Run("failure-api-error", func(t *testing.T) {
c, err := NewClient("my-token")
assert.NoError(err)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := os.ReadFile("testdata/error.json")
assert.NoError(err)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write(b)
}))
c.url, err = url.Parse(server.URL)
assert.NoError(err)
_, err = c.Packages(PackageArgs{Distribution: "test", Version: "A"}, PackageOpts{})
assert.Error(err)
})
t.Run("failure-unknown-error", func(t *testing.T) {
c, err := NewClient("my-token")
assert.NoError(err)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("some error"))
}))
c.url, err = url.Parse(server.URL)
assert.NoError(err)
_, err = c.Packages(PackageArgs{Distribution: "test", Version: "A"}, PackageOpts{})
assert.Error(err)
})
}