File size: 2,495 Bytes
8baf122 | 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 | package plugins
import (
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const fakePluginBytes = "fake-plugin-binary-content"
func TestDownloadPlugin_DirectDownload(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(fakePluginBytes))
}))
defer server.Close()
path, err := DownloadPlugin(server.URL, ".so")
require.NoError(t, err)
defer os.Remove(path)
data, err := os.ReadFile(path)
require.NoError(t, err)
assert.Equal(t, fakePluginBytes, string(data))
}
func TestDownloadPlugin_FollowsRedirect(t *testing.T) {
// Final destination
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(fakePluginBytes))
}))
defer target.Close()
// Redirect server (simulates GitHub → S3)
redirector := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, target.URL, http.StatusFound)
}))
defer redirector.Close()
path, err := DownloadPlugin(redirector.URL, ".so")
require.NoError(t, err)
defer os.Remove(path)
data, err := os.ReadFile(path)
require.NoError(t, err)
assert.Equal(t, fakePluginBytes, string(data))
}
func TestDownloadPlugin_TooManyRedirects(t *testing.T) {
// Server that always redirects to itself
var server *httptest.Server
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, server.URL, http.StatusFound)
}))
defer server.Close()
_, err := DownloadPlugin(server.URL, ".so")
require.Error(t, err)
assert.Contains(t, err.Error(), "too many redirects")
}
func TestDownloadPlugin_NonOKStatus(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer server.Close()
_, err := DownloadPlugin(server.URL, ".so")
require.Error(t, err)
assert.Contains(t, err.Error(), "404")
}
func TestDownloadPlugin_FileExtensionPreserved(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(fakePluginBytes))
}))
defer server.Close()
path, err := DownloadPlugin(server.URL, ".so")
require.NoError(t, err)
defer os.Remove(path)
assert.Contains(t, path, ".so")
}
|