From 092cf1c1a9406824ad0af3dc3f4dc12fde1b9293 Mon Sep 17 00:00:00 2001 From: MalinAhlberg Date: Thu, 25 Apr 2024 09:05:11 +0200 Subject: [PATCH] tests: update download tests --- sda-download/api/sda/sda_test.go | 36 +++++++++++++++++++------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/sda-download/api/sda/sda_test.go b/sda-download/api/sda/sda_test.go index 8c3e966b7..fd9ddaa5c 100644 --- a/sda-download/api/sda/sda_test.go +++ b/sda-download/api/sda/sda_test.go @@ -491,9 +491,14 @@ func TestDownload_Fail_OpenFile(t *testing.T) { return fileDetails, nil } - // Mock request and response holders + // Mock request and response holders and initialize headers w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) + req := &http.Request{ + URL: &url.URL{}, + Header: make(http.Header), + } + c.Request = req // Test the outcomes of the handler Download(c) @@ -520,7 +525,7 @@ func TestDownload_Fail_OpenFile(t *testing.T) { } -func TestEncrypted_Coords(t *testing.T) { +func Test_CalucalateCoords(t *testing.T) { var to, from int64 from, to = 0, 1000 fileDetails := &database.FileDownload{ @@ -529,41 +534,44 @@ func TestEncrypted_Coords(t *testing.T) { Header: make([]byte, 124), } - // htsget_range should be used first and as is + // htsget_range should be used first and its end position should be increased by one headerSize := bytes.NewReader(fileDetails.Header).Size() fullSize := headerSize + int64(fileDetails.ArchiveSize) - start, end, err := calculateEncryptedCoords(from, to, "bytes=10-20", fileDetails) + var endPos int64 + endPos = 20 + start, end, err := calculateCoords(from, to, "bytes=10-"+strconv.FormatInt(endPos, 10), fileDetails, "default") assert.Equal(t, start, int64(10)) - assert.Equal(t, end, int64(20)) + assert.Equal(t, end, endPos+1) assert.NoError(t, err) // end should be greater than or equal to inputted end - _, end, err = calculateEncryptedCoords(from, to, "", fileDetails) + _, end, err = calculateCoords(from, to, "", fileDetails, "encrypted") assert.GreaterOrEqual(t, end, from) assert.NoError(t, err) // end should not be smaller than a header - _, end, err = calculateEncryptedCoords(from, headerSize-10, "", fileDetails) + _, end, err = calculateCoords(from, headerSize-10, "", fileDetails, "encrypted") assert.GreaterOrEqual(t, end, headerSize) assert.NoError(t, err) // end should not be larger than file length + header - _, end, err = calculateEncryptedCoords(from, fullSize+1900, "", fileDetails) + _, end, err = calculateCoords(from, fullSize+1900, "", fileDetails, "encrypted") assert.Equal(t, fullSize, end) assert.NoError(t, err) - // range 0-0 should give whole file - start, end, err = calculateEncryptedCoords(0, 0, "", fileDetails) + // param range 0-0 should give whole file + start, end, err = calculateCoords(0, 0, "", fileDetails, "encrypted") assert.Equal(t, end-start, fullSize) assert.NoError(t, err) - // range 0-0 with range in the header should return the range size - _, end, err = calculateEncryptedCoords(0, 0, "bytes=0-1000", fileDetails) - assert.Equal(t, end, int64(1000)) + // byte range 0-1000 should return the range size, end coord inclusive + endPos = 1000 + _, end, err = calculateCoords(0, 0, "bytes=0-"+strconv.FormatInt(endPos, 10), fileDetails, "encrypted") + assert.Equal(t, end, endPos+1) assert.NoError(t, err) // range in the header should return error if values are not numbers - _, _, err = calculateEncryptedCoords(0, 0, "bytes=start-end", fileDetails) + _, _, err = calculateCoords(0, 0, "bytes=start-end", fileDetails, "encrypted") assert.Error(t, err) }