-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhttp_source_test.go
173 lines (142 loc) · 5.62 KB
/
http_source_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
// Copyright (c) 2024 Mr. Gecko's Media (James Coleman). http://mrgeckosmedia.com/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package selfupdate
import (
"context"
"crypto/sha256"
"encoding/hex"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const httpTestBaseURL = "http://localhost"
// Test server for testing http repos.
type HttpRepoTestServer struct {
server *httptest.Server
repoURL string
}
// Setup test server with test data.
func NewHttpRepoTestServer() *HttpRepoTestServer {
s := new(HttpRepoTestServer)
// Setup handlers.
mux := http.NewServeMux()
fs := http.FileServer(http.Dir("./testdata/http_repo"))
mux.Handle("/repo/creativeprojects/resticprofile/", http.StripPrefix("/repo/creativeprojects/resticprofile", fs))
// Setup server config.
s.server = httptest.NewServer(mux)
s.repoURL = s.server.URL + "/repo/"
return s
}
// Stop the HTTP server.
func (s *HttpRepoTestServer) Stop() {
s.server.Close()
}
// Verify the client ignores invalid URLs.
func TestHttpClientInvalidURL(t *testing.T) {
_, err := NewHttpSource(HttpConfig{BaseURL: ":this is not a URL"})
assert.NotNil(t, err, "Invalid URL should raise an error")
}
// Verify the client accepts valid URLs.
func TestHttpClientValidURL(t *testing.T) {
_, err := NewHttpSource(HttpConfig{BaseURL: httpTestBaseURL})
require.NoError(t, err)
}
// Verify cancelled contexts actually cancels a request.
func TestHttpListReleasesContextCancelled(t *testing.T) {
// Make a valid HTTP source.
source, err := NewHttpSource(HttpConfig{BaseURL: httpTestBaseURL})
require.NoError(t, err)
// Create a cancelled context.
ctx, cancelFn := context.WithCancel(context.Background())
cancelFn()
// Attempt to list releases and verify result.
_, err = source.ListReleases(ctx, ParseSlug("creativeprojects/resticprofile"))
assert.ErrorIs(t, err, context.Canceled)
}
// Verify cancelled contexts actually cancels a download.
func TestHttpDownloadReleaseAssetContextCancelled(t *testing.T) {
// Make a valid HTTP source.
source, err := NewHttpSource(HttpConfig{BaseURL: httpTestBaseURL})
require.NoError(t, err)
// Create a cancelled context.
ctx, cancelFn := context.WithCancel(context.Background())
cancelFn()
// Attempt to download release and verify result.
_, err = source.DownloadReleaseAsset(ctx, &Release{
AssetID: 11,
AssetURL: httpTestBaseURL,
}, 11)
assert.ErrorIs(t, err, context.Canceled)
}
// Verify no release actually returns an error.
func TestHttpDownloadReleaseAssetWithNilRelease(t *testing.T) {
// Create valid HTTP source.
source, err := NewHttpSource(HttpConfig{BaseURL: httpTestBaseURL})
require.NoError(t, err)
// Attempt to download release without specifying the release and verify result.
_, err = source.DownloadReleaseAsset(context.Background(), nil, 11)
assert.ErrorIs(t, err, ErrInvalidRelease)
}
// Verify we're able to list releases and download an asset.
func TestHttpListAndDownloadReleaseAsset(t *testing.T) {
// Create test HTTP server and start it.
server := NewHttpRepoTestServer()
// Make HTTP source with our test server.
source, err := NewHttpSource(HttpConfig{BaseURL: server.repoURL})
require.NoError(t, err)
// List releases
releases, err := source.ListReleases(context.Background(), ParseSlug("creativeprojects/resticprofile"))
require.NoError(t, err)
// Confirm the manifest parsed the correct number of releases.
assert.Equal(t, len(releases), 2, "releases count is not valid")
// Confirm the manifest parsed by the first release is valid.
assert.Equal(t, releases[0].GetTagName(), "v0.1.1", "release is not as expected")
// Confirm the release assets are parsed correctly.
assets := releases[1].GetAssets()
assert.Equal(t, assets[1].GetName(), "example_linux_amd64.tar.gz", "the release asset is not valid")
// Get updater with source.
updater, err := NewUpdater(Config{
Source: source,
OS: "linux",
Arch: "amd64",
})
require.NoError(t, err)
// Find the latest release.
release, found, err := updater.DetectLatest(context.Background(), NewRepositorySlug("creativeprojects", "resticprofile"))
require.NoError(t, err)
assert.Equal(t, found, true, "no release found")
// Download asset.
body, err := source.DownloadReleaseAsset(context.Background(), release, 5)
require.NoError(t, err)
// Read data.
data, err := io.ReadAll(body)
require.NoError(t, err)
// Verify data.
hfun := sha256.New()
hfun.Write(data)
sum := hfun.Sum(nil)
hash := hex.EncodeToString(sum)
assert.Equal(t, hash, "9208c58af1265438c6894499847355bd5e77f93d04b201393baf41297d4680a3", "hash isn't valid for test file")
// Stop as we're done.
server.Stop()
}