diff --git a/internal/artifactory_cache_test.go b/internal/artifactory_cache_test.go index 38fa910..b7ad00a 100644 --- a/internal/artifactory_cache_test.go +++ b/internal/artifactory_cache_test.go @@ -91,49 +91,55 @@ func TestDownloadWithArtifactoryCache(t *testing.T) { // TestDeleteWithArtifactoryCache verifies deleting a resource with caching enabled. func TestDeleteWithArtifactoryCache(t *testing.T) { - // Sub-test to verify successful deletion of a resource. - t.Run("SuccessfulDelete", func(t *testing.T) { - // Set the GRABIT_ARTIFACTORY_TOKEN environment variable. - t.Setenv("GRABIT_ARTIFACTORY_TOKEN", "test-token") + // Set the GRABIT_ARTIFACTORY_TOKEN environment variable. + t.Setenv("GRABIT_ARTIFACTORY_TOKEN", "test-token") - // Setup an HTTP handler to handle DELETE requests. - handler := func(w http.ResponseWriter, r *http.Request) { - if r.Method == "DELETE" { // Respond with OK for DELETE requests. - w.WriteHeader(http.StatusOK) - } + // Setup an HTTP handler to handle DELETE requests. + handler := func(w http.ResponseWriter, r *http.Request) { + if r.Method == "DELETE" { // Respond with OK for DELETE requests. + w.WriteHeader(http.StatusOK) } - // Start the HTTP server and get the port it runs on. - port, server := test.HttpHandler(handler) - defer server.Close() // Ensure the server is stopped after the test. - - // Set up URLs for the source file and cache. - sourceURL := fmt.Sprintf("http://localhost:%d/test.txt", port) - cacheURL := fmt.Sprintf("http://localhost:%d", port) - - // Create a lock file with the resource and cache information. - lockContent := fmt.Sprintf(`[[Resource]] - Urls = ['%s'] - Integrity = 'sha256-test' - CacheUri = '%s'`, sourceURL, cacheURL) - - lockPath := test.TmpFile(t, lockContent) - lock, err := NewLock(lockPath, false) - require.NoError(t, err) // Fail the test if the lock cannot be created. - - // Save the lock file before modifying it. - err = lock.Save() - require.NoError(t, err) // Fail the test if saving fails. - - // Delete the resource from the lock file. - lock.DeleteResource(sourceURL) - - // Save the lock file again after deletion. - err = lock.Save() - require.NoError(t, err) // Fail the test if saving fails. - - // Reload the lock file and verify the resource is gone. - newLock, err := NewLock(lockPath, false) + } + // Start the HTTP server and get the port it runs on. + port, server := test.HttpHandler(handler) + defer server.Close() // Ensure the server is stopped after the test. + + // Set up URLs for the source file and cache. + sourceURL := fmt.Sprintf("http://localhost:%d/test.txt", port) + cacheURL := fmt.Sprintf("http://localhost:%d", port) + + // Create a lock file with the resource and cache information. + lockContent := fmt.Sprintf(`[[Resource]] + Urls = ['%s'] + Integrity = 'sha256-test' + CacheUri = '%s'`, sourceURL, cacheURL) + + lockPath := test.TmpFile(t, lockContent) + lock, err := NewLock(lockPath, false) + require.NoError(t, err) // Fail the test if the lock cannot be created. + + // Save the lock file before modifying it. + err = lock.Save() + require.NoError(t, err) // Fail the test if saving fails. + + // Delete the resource from the lock file. + lock.DeleteResource(sourceURL) + + // Checks that http.StatusOK was returned meaning the delete was successful + resp, err := http.Get(server.URL) + if err != nil { require.NoError(t, err) - assert.Equal(t, 0, len(newLock.conf.Resource)) // Ensure no resources remain. - }) + } + if resp.StatusCode != http.StatusOK { + t.Errorf("expected 200 but got %d", resp.StatusCode) + } + + // Save the lock file again after deletion. + err = lock.Save() + require.NoError(t, err) // Fail the test if saving fails. + + // Reload the lock file and verify the resource is gone. + newLock, err := NewLock(lockPath, false) + require.NoError(t, err) + assert.Equal(t, 0, len(newLock.conf.Resource)) // Ensure no resources remain. }