Skip to content

Commit

Permalink
all: use httputil to construct requests
Browse files Browse the repository at this point in the history
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Jan 10, 2023
1 parent a367a7a commit 577a55d
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cmd/clairctl/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func openInput(ctx context.Context, c *http.Client, n string) (io.ReadCloser, er
}
u, uerr := url.Parse(n)
if uerr == nil {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/clairctl/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"golang.org/x/sync/errgroup"

"github.com/quay/clair/v4/internal/codec"
"github.com/quay/clair/v4/internal/httputil"
)

var ManifestCmd = &cli.Command{
Expand Down Expand Up @@ -136,7 +137,7 @@ func Inspect(ctx context.Context, r string) (*claircore.Manifest, error) {
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion health/readinesshandler_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package health_test

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/quay/clair/v4/health"
"github.com/quay/clair/v4/internal/httputil"
)

func TestReadinessHandler(t *testing.T) {
ctx := context.Background()
server := httptest.NewServer(health.ReadinessHandler())
client := server.Client()

req, err := http.NewRequest("GET", server.URL, nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, server.URL, nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
Expand Down
6 changes: 4 additions & 2 deletions httptransport/concurrentlimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/quay/zlog"
"golang.org/x/sync/semaphore"

"github.com/quay/clair/v4/internal/httputil"
)

func TestConcurrentRequests(t *testing.T) {
Expand Down Expand Up @@ -39,7 +41,7 @@ func TestConcurrentRequests(t *testing.T) {
c := srv.Client()

done := make(chan struct{})
req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL, nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, srv.URL, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -60,7 +62,7 @@ func TestConcurrentRequests(t *testing.T) {
// Wait for the above goroutine to hit the handler.
<-ready
for i := 0; i < 10; i++ {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL, nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, srv.URL, nil)
if err != nil {
t.Errorf("%d: %v", i, err)
}
Expand Down
17 changes: 9 additions & 8 deletions httptransport/indexer_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"go.opentelemetry.io/otel/trace"

"github.com/quay/clair/v4/indexer"
"github.com/quay/clair/v4/internal/httputil"
)

func TestIndexReportBadLayer(t *testing.T) {
Expand Down Expand Up @@ -46,7 +47,7 @@ func TestIndexReportBadLayer(t *testing.T) {
t.Run("POST", func(t *testing.T) {
const body = `{"hash":"sha256:0000000000000000000000000000000000000000000000000000000000000000",` +
`"layers":[{}]}`
req, err := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL+path, strings.NewReader(body))
req, err := httputil.NewRequestWithContext(ctx, http.MethodPost, srv.URL+path, strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -107,7 +108,7 @@ func TestIndexerV1(t *testing.T) {
ctx := zlog.Test(ctx, t)
const path = `/index_state`
t.Run("GET", func(t *testing.T) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+path, nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, srv.URL+path, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -128,7 +129,7 @@ func TestIndexerV1(t *testing.T) {
t.Run("POST", func(t *testing.T) {
const body = `{"hash":"sha256:0000000000000000000000000000000000000000000000000000000000000000",` +
`"layers":[{}]}`
req, err := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL+path, strings.NewReader(body))
req, err := httputil.NewRequestWithContext(ctx, http.MethodPost, srv.URL+path, strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
Expand All @@ -144,7 +145,7 @@ func TestIndexerV1(t *testing.T) {
})
t.Run("DELETE", func(t *testing.T) {
const body = `["sha256:0000000000000000000000000000000000000000000000000000000000000000"]`
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, srv.URL+path, strings.NewReader(body))
req, err := httputil.NewRequestWithContext(ctx, http.MethodDelete, srv.URL+path, strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
Expand All @@ -168,7 +169,7 @@ func TestIndexerV1(t *testing.T) {
}
})
t.Run("GET", func(t *testing.T) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+path, nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, srv.URL+path, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -187,7 +188,7 @@ func TestIndexerV1(t *testing.T) {
ctx := zlog.Test(ctx, t)
const path = `/index_report/sha256:0000000000000000000000000000000000000000000000000000000000000000`
t.Run("DELETE", func(t *testing.T) {
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, srv.URL+path, nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodDelete, srv.URL+path, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -202,7 +203,7 @@ func TestIndexerV1(t *testing.T) {
}
})
t.Run("GET", func(t *testing.T) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+path, nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, srv.URL+path, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -222,7 +223,7 @@ func TestIndexerV1(t *testing.T) {
const path = `/internal/affected_manifest/`
t.Run("POST", func(t *testing.T) {
const body = `{"vulnerabilities":[]}`
req, err := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL+path, strings.NewReader(body))
req, err := httputil.NewRequestWithContext(ctx, http.MethodPost, srv.URL+path, strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
Expand Down
39 changes: 21 additions & 18 deletions httptransport/matcher_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/google/uuid"
"github.com/quay/clair/v4/indexer"
"github.com/quay/clair/v4/internal/httputil"
"github.com/quay/clair/v4/matcher"
"github.com/quay/claircore/libvuln/driver"
"github.com/quay/zlog"
Expand Down Expand Up @@ -66,12 +67,12 @@ func testUpdateDiffMatcher(t *testing.T) {
q := url.Query()
q.Set("cur", "892737b2-a616-4113-a7a9-137139c8f91e")
url.RawQuery = q.Encode()
t.Log(url)

req := &http.Request{
URL: url,
Method: http.MethodGet,
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
if err != nil {
t.Fatalf("failed to construct request: %v", err)
}
t.Log(url)
resp, err := srvOK.Client().Do(req)
if err != nil {
t.Fatalf("failed to do request: %v", err)
Expand All @@ -96,10 +97,11 @@ func testUpdateDiffMatcher(t *testing.T) {
q = url.Query()
q.Set("cur", "892737b2-a616-4113-a7a9-137139c8f91e")
url.RawQuery = q.Encode()
t.Log(url)

req = &http.Request{
URL: url,
Method: http.MethodGet,
req, err = httputil.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
if err != nil {
t.Fatalf("failed to construct request: %v", err)
}
resp, err = srvErr.Client().Do(req)
if err != nil {
Expand Down Expand Up @@ -174,9 +176,9 @@ func testUpdateDiffHandlerParams(t *testing.T) {
q.Set("prev", test.cur)
u.RawQuery = q.Encode()

req := &http.Request{
URL: u,
Method: http.MethodGet,
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
t.Fatalf("failed to construct request: %v", err)
}
resp, err := c.Do(req)
if err != nil {
Expand Down Expand Up @@ -224,7 +226,7 @@ func testUpdateDiffHandlerMethods(t *testing.T) {
http.MethodPut,
http.MethodTrace,
} {
req, err := http.NewRequest(m, u.String(), nil)
req, err := httputil.NewRequestWithContext(ctx, m, u.String(), nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
Expand Down Expand Up @@ -275,7 +277,7 @@ func testUpdateOperationHandlerErrors(t *testing.T) {
c := srv.Client()

// perform get with failing differ
req, err := http.NewRequest(http.MethodGet, srv.URL+path.Join("/", "internal", "update_operation"), nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, srv.URL+path.Join("/", "internal", "update_operation"), nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
Expand All @@ -289,7 +291,7 @@ func testUpdateOperationHandlerErrors(t *testing.T) {

// perform delete with failing differ
u := srv.URL + path.Join("/", "internal", "update_operation") + "/" + id
req, err = http.NewRequest(http.MethodDelete, u, nil)
req, err = httputil.NewRequestWithContext(ctx, http.MethodDelete, u, nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
Expand Down Expand Up @@ -324,7 +326,7 @@ func testUpdateOperationHandlerMethods(t *testing.T) {
http.MethodPut,
http.MethodTrace,
} {
req, err := http.NewRequest(m, srv.URL+path.Join("/", "internal", "update_operation"), nil)
req, err := httputil.NewRequestWithContext(ctx, m, srv.URL+path.Join("/", "internal", "update_operation"), nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
Expand Down Expand Up @@ -370,7 +372,7 @@ func testUpdateOperationHandlerGet(t *testing.T) {
c := srv.Client()

// get without latest param
req, err := http.NewRequest(http.MethodGet, srv.URL+path.Join("/", "internal", "update_operation"), nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, srv.URL+path.Join("/", "internal", "update_operation"), nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
Expand All @@ -395,9 +397,10 @@ func testUpdateOperationHandlerGet(t *testing.T) {
q := u.Query()
q.Add("latest", "true")
u.RawQuery = q.Encode()
req = &http.Request{
URL: u,
Method: http.MethodGet,

req, err = httputil.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
resp, err = c.Do(req)
if err != nil {
Expand Down
21 changes: 11 additions & 10 deletions httptransport/notification_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"

"github.com/quay/clair/v4/internal/httputil"
"github.com/quay/clair/v4/notifier"
"github.com/quay/clair/v4/notifier/service"
)
Expand Down Expand Up @@ -53,9 +54,9 @@ func testNotificationHandlerDelete(ctx context.Context) func(*testing.T) {
}
rr := httptest.NewRecorder()
u, _ := url.Parse("http://clair-notifier/notifier/api/v1/notification/" + noteID.String())
req := &http.Request{
URL: u,
Method: http.MethodGet,
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
t.Error(err)
}

h.delete(rr, req)
Expand Down Expand Up @@ -104,9 +105,9 @@ func testNotificationHandlerGet(ctx context.Context) func(*testing.T) {
}
rr := httptest.NewRecorder()
u, _ := url.Parse("http://clair-notifier/notifier/api/v1/notification/" + noteID.String())
req := &http.Request{
URL: u,
Method: http.MethodGet,
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
t.Error(err)
}

h.get(rr, req)
Expand Down Expand Up @@ -171,9 +172,9 @@ func testNotificationHandlerGetParams(ctx context.Context) func(*testing.T) {
v.Set("page_size", pageSizeParam)
v.Set("page", pageParam)
u.RawQuery = v.Encode()
req := &http.Request{
URL: u,
Method: http.MethodGet,
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
t.Error(err)
}

h.get(rr, req)
Expand Down Expand Up @@ -215,7 +216,7 @@ func testNotificationsHandlerMethods(ctx context.Context) func(*testing.T) {
http.MethodPut,
http.MethodTrace,
} {
req, err := http.NewRequest(m, u, nil)
req, err := httputil.NewRequestWithContext(ctx, m, u, nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
Expand Down
5 changes: 4 additions & 1 deletion middleware/auth/httpauth_psk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth

import (
"bytes"
"context"
"encoding/base64"
"fmt"
"math/rand"
Expand All @@ -13,6 +14,7 @@ import (
"testing/quick"
"time"

"github.com/quay/clair/v4/internal/httputil"
"gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/jwt"
)
Expand Down Expand Up @@ -88,6 +90,7 @@ func (tc *pskTestcase) Handler(t *testing.T) http.Handler {
// Roundtrips returns a function suitable for passing to quick.Check.
func roundtrips(t *testing.T) func(*pskTestcase) bool {
return func(tc *pskTestcase) bool {
ctx := context.Background()
t.Log(tc)
// Set up the jwt signer.
sk := jose.SigningKey{
Expand Down Expand Up @@ -122,7 +125,7 @@ func roundtrips(t *testing.T) func(*pskTestcase) bool {
defer srv.Close()

// Mint a request.
req, err := http.NewRequest(http.MethodGet, srv.URL, nil)
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, srv.URL, nil)
if err != nil {
t.Error(err)
return false
Expand Down

0 comments on commit 577a55d

Please sign in to comment.