github.com/quay/claircore@v1.5.28/rhel/rhcc/fetcher_test.go (about)

     1  package rhcc
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/quay/zlog"
    12  
    13  	"github.com/quay/claircore/libvuln/driver"
    14  )
    15  
    16  func TestFetcher(t *testing.T) {
    17  	t.Parallel()
    18  	const serveFile = "testdata/cve-2021-3762.xml"
    19  	ctx := zlog.Test(context.Background(), t)
    20  
    21  	fi, err := os.Stat(serveFile)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	tag := fmt.Sprintf(`"%d"`, fi.ModTime().UnixNano())
    26  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    27  		if r.Header.Get("if-none-match") == tag {
    28  			w.WriteHeader(http.StatusNotModified)
    29  			return
    30  		}
    31  		w.Header().Set("etag", tag)
    32  		http.ServeFile(w, r, serveFile)
    33  	}))
    34  	defer srv.Close()
    35  
    36  	u := &updater{
    37  		url:    srv.URL,
    38  		client: srv.Client(),
    39  	}
    40  	rd, hint, err := u.Fetch(ctx, "")
    41  	if err != nil {
    42  		t.Error(err)
    43  	}
    44  	if rd != nil {
    45  		rd.Close()
    46  	}
    47  	_, _, err = u.Fetch(ctx, driver.Fingerprint(hint))
    48  	if got, want := err, driver.Unchanged; got != want {
    49  		t.Errorf("got: %v, want: %v", got, want)
    50  	}
    51  }