Skip to content

Commit

Permalink
move to common code
Browse files Browse the repository at this point in the history
  • Loading branch information
rosecodym committed Jul 20, 2023
1 parent e628fd0 commit 7a7021b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
16 changes: 16 additions & 0 deletions pkg/common/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"crypto/tls"
"crypto/x509"
"io"
"net"
"net/http"
"strings"
Expand Down Expand Up @@ -99,6 +100,21 @@ func NewCustomTransport(T http.RoundTripper) *CustomTransport {
return &CustomTransport{T}
}

func ConstantStatusHttpClient(statusCode int) *http.Client {
return &http.Client{
Timeout: DefaultResponseTimeout,
Transport: FakeTransport{
CreateResponse: func(req *http.Request) (*http.Response, error) {
return &http.Response{
Request: req,
Body: io.NopCloser(strings.NewReader("")),
StatusCode: statusCode,
}, nil
},
},
}
}

func PinnedRetryableHttpClient() *http.Client {
httpClient := retryablehttp.NewClient()
httpClient.Logger = nil
Expand Down
20 changes: 1 addition & 19 deletions pkg/detectors/alchemy/alchemy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ package alchemy
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -107,7 +104,7 @@ func TestAlchemy_FromChunk(t *testing.T) {
},
{
name: "found, verified but unexpected api surface",
s: Scanner{client: constantStatusHttpClient(404)},
s: Scanner{client: common.ConstantStatusHttpClient(404)},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf("You can find a alchemy secret %s within", secret)),
Expand Down Expand Up @@ -162,21 +159,6 @@ func BenchmarkFromData(benchmark *testing.B) {
}
}

func constantStatusHttpClient(statusCode int) *http.Client {
return &http.Client{
Timeout: common.DefaultResponseTimeout,
Transport: common.FakeTransport{
CreateResponse: func(req *http.Request) (*http.Response, error) {
return &http.Response{
Request: req,
Body: io.NopCloser(strings.NewReader("")),
StatusCode: statusCode,
}, nil
},
},
}
}

func timeoutContext(timeout time.Duration) context.Context {
c, _ := context.WithTimeout(context.Background(), timeout)
// The cancellation function is discarded for test ergonomics - this is expected to be used with a short timeout,
Expand Down

0 comments on commit 7a7021b

Please sign in to comment.