Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional headers to DownloadableFile messages when downloading files #306

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
54 changes: 31 additions & 23 deletions client/clientimpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1439,27 +1439,6 @@ const packageFileURL = "/validfile.pkg"

var packageFileContent = []byte("Package File Content")

func createDownloadSrv(t *testing.T) *httptest.Server {
m := http.NewServeMux()
m.HandleFunc(packageFileURL,
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := w.Write(packageFileContent)
assert.NoError(t, err)
})

srv := httptest.NewServer(m)

u, err := url.Parse(srv.URL)
if err != nil {
t.Fatal(err)
}
endpoint := u.Host
testhelpers.WaitForEndpoint(endpoint)

return srv
}

func createPackageTestCase(name string, downloadSrv *httptest.Server) packageTestCase {
return packageTestCase{
name: name,
Expand Down Expand Up @@ -1505,14 +1484,43 @@ func createPackageTestCase(name string, downloadSrv *httptest.Server) packageTes
}
}

// Mock download server that checks headers in the request.
func createDownloadSrv(t *testing.T) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check for the Authorization header in the successWithHeaders case.
if r.Header.Get("Authorization") == "Bearer test-token" {
t.Logf("Authorization header successfully set.")
} else {
t.Errorf("Expected Authorization header to be 'Bearer test-token', but got '%s'", r.Header.Get("Authorization"))
}

// Handle file not found scenario.
if r.URL.Path == "/notfound" {
http.Error(w, "Not found", http.StatusNotFound)
return
}

// Simulate a successful file download for valid requests.
w.WriteHeader(http.StatusOK)
w.Write([]byte("file content"))
}))
}

func TestUpdatePackages(t *testing.T) {

downloadSrv := createDownloadSrv(t)
defer downloadSrv.Close()

// A success case.
var tests []packageTestCase
tests = append(tests, createPackageTestCase("success", downloadSrv))

// Success case with headers.
successWithHeaders := createPackageTestCase("success with headers", downloadSrv)
successWithHeaders.available.Packages["packageWithHeaders"].File.Headers = &protobufs.Headers{
Headers: []*protobufs.Header{
{Key: "Authorization", Value: "Bearer test-token"},
},
}
tests = append(tests, successWithHeaders)

// A case when downloading the file fails because the URL is incorrect.
notFound := createPackageTestCase("downloadable file not found", downloadSrv)
Expand Down
7 changes: 7 additions & 0 deletions client/internal/packagessyncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ func (s *packagesSyncer) downloadFile(ctx context.Context, pkgName string, file
return fmt.Errorf("cannot download file from %s: %v", file.DownloadUrl, err)
}

// Add optional headers if they exist
if file.Headers != nil {
RichardChukwu marked this conversation as resolved.
Show resolved Hide resolved
for _, header := range file.Headers.Headers {
req.Header.Add(header.Key, header.Value)
}
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("cannot download file from %s: %v", file.DownloadUrl, err)
Expand Down
Loading