feat: add repository artifact client
All checks were successful
default / ensure tests work (push) Successful in 28s
All checks were successful
default / ensure tests work (push) Successful in 28s
This commit is contained in:
parent
2b90918d74
commit
cc3b3b095f
1 changed files with 219 additions and 0 deletions
128
artifact/repository_test.go
Normal file
128
artifact/repository_test.go
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
package artifact
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRepositoryClient_ListRunArtifacts(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var gotPath, gotAuth string
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotPath = r.URL.Path
|
||||
gotAuth = r.Header.Get("Authorization")
|
||||
w.Header().Set("content-type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"total_count":1,"artifacts":[{"id":7,"name":"foo","size_in_bytes":10,"expired":false}]}`)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
c := NewRepositoryClient(server.URL, "secret")
|
||||
res, err := c.ListRunArtifacts(context.Background(), "owner", "repo", 42)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if got, want := gotPath, "/repos/owner/repo/actions/runs/42/artifacts"; got != want {
|
||||
t.Errorf("expected path %q to be %q", got, want)
|
||||
}
|
||||
if got, want := gotAuth, "Bearer secret"; got != want {
|
||||
t.Errorf("expected Authorization %q to be %q", got, want)
|
||||
}
|
||||
if len(res.Artifacts) != 1 || res.Artifacts[0].Id != 7 || res.Artifacts[0].Name != "foo" {
|
||||
t.Errorf("unexpected response: %+v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepositoryClient_ListRunArtifacts_Error(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("content-type", "application/json")
|
||||
http.Error(w, `{"message":"not found"}`, http.StatusNotFound)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
c := NewRepositoryClient(server.URL, "secret")
|
||||
if _, err := c.ListRunArtifacts(context.Background(), "owner", "repo", 42); err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepositoryClient_DownloadArtifact(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const zip = "zip-bytes"
|
||||
var gotPath string
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotPath = r.URL.Path
|
||||
_, _ = io.WriteString(w, zip)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
c := NewRepositoryClient(server.URL, "secret")
|
||||
body, err := c.DownloadArtifact(context.Background(), "owner", "repo", 7)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer body.Close()
|
||||
|
||||
if got, want := gotPath, "/repos/owner/repo/actions/artifacts/7/zip"; got != want {
|
||||
t.Errorf("expected path %q to be %q", got, want)
|
||||
}
|
||||
data, err := io.ReadAll(body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(data) != zip {
|
||||
t.Errorf("expected %q, got %q", zip, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepositoryClient_DownloadArtifact_Redirect(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
blob := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = io.WriteString(w, "redirected-zip")
|
||||
}))
|
||||
defer blob.Close()
|
||||
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, blob.URL+"/blob", http.StatusFound)
|
||||
}))
|
||||
defer api.Close()
|
||||
|
||||
c := NewRepositoryClient(api.URL, "secret")
|
||||
body, err := c.DownloadArtifact(context.Background(), "owner", "repo", 7)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer body.Close()
|
||||
|
||||
data, err := io.ReadAll(body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(data) != "redirected-zip" {
|
||||
t.Errorf("expected %q, got %q", "redirected-zip", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepositoryClient_DownloadArtifact_Error(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "nope", http.StatusNotFound)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
c := NewRepositoryClient(server.URL, "secret")
|
||||
_, err := c.DownloadArtifact(context.Background(), "owner", "repo", 7)
|
||||
if err == nil || !strings.Contains(err.Error(), "download artifact") {
|
||||
t.Fatalf("expected download error, got %v", err)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue