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 10 commits into
base: main
Choose a base branch
from
40 changes: 36 additions & 4 deletions client/clientimpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ const packageFileURL = "/validfile.pkg"

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

func createDownloadSrv(t *testing.T) *httptest.Server {
func createDownloadSrvBasic(t *testing.T) *httptest.Server {
m := http.NewServeMux()
m.HandleFunc(packageFileURL,
func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -1507,12 +1507,22 @@ func createPackageTestCase(name string, downloadSrv *httptest.Server) packageTes

func TestUpdatePackages(t *testing.T) {

// Create a download server and defer its closure.
downloadSrv := createDownloadSrv(t)
defer downloadSrv.Close()

// A success case.
var tests []packageTestCase
tests = append(tests, createPackageTestCase("success", downloadSrv))
// A success case with headers set in the DownloadableFile.
successWithHeaders := createPackageTestCase("success with headers", downloadSrv)
// Add headers to the downloadable file.
successWithHeaders.available.Packages["package1"].File.Headers = &protobufs.Headers{
Headers: []*protobufs.Header{
{Key: "Authorization", Value: "Bearer test-token"},
},
}
successWithHeaders.expectedStatus.Packages["package1"].Status = protobufs.PackageStatusEnum_PackageStatusEnum_Installed
successWithHeaders.expectedStatus.Packages["package1"].ErrorMessage = ""
// Append the test case to the list of tests.
tests := []packageTestCase{successWithHeaders}

// A case when downloading the file fails because the URL is incorrect.
notFound := createPackageTestCase("downloadable file not found", downloadSrv)
Expand All @@ -1527,13 +1537,35 @@ func TestUpdatePackages(t *testing.T) {
errorOnCallback.errorOnCallback = true
tests = append(tests, errorOnCallback)

// Run each test case.
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
verifyUpdatePackages(t, test)
})
}
}

// Mock download server that checks headers in the request.
func createDownloadServWithAuth(t *testing.T) *httptest.Server {
RichardChukwu marked this conversation as resolved.
Show resolved Hide resolved
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check for the presence of the expected Authorization header in the successWithHeaders case.
if r.Header.Get("Authorization") != "" {
expectedHeader := "Bearer test-token"
if r.Header.Get("Authorization") != expectedHeader {
t.Errorf("Expected Authorization header to be '%s', but got '%s'", expectedHeader, 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 TestMissingCapabilities(t *testing.T) {
testClients(t, func(t *testing.T, client OpAMPClient) {
// Start a server.
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