Skip to content

Commit

Permalink
fix(httpreplay): add ignore-header flag, fix tests (#7865)
Browse files Browse the repository at this point in the history
* fix(httpreplay): add ignore-header flag

* fix TestIntegration_(RecordAndReplay|HTTPR)

* add more timeouts

* add ignoreheader on replay too
  • Loading branch information
noahdietz committed May 2, 2023
1 parent 37a7c72 commit 1829706
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
32 changes: 27 additions & 5 deletions httpreplay/cmd/httpr/httpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@ import (
"net/http"
"os"
"os/signal"
"strings"

"cloud.google.com/go/httpreplay/internal/proxy"
"github.com/google/martian/v3/martianhttp"
)

var (
port = flag.Int("port", 8080, "port of the proxy")
controlPort = flag.Int("control-port", 8181, "port for controlling the proxy")
record = flag.String("record", "", "record traffic and save to filename")
replay = flag.String("replay", "", "read filename and replay traffic")
debugHeaders = flag.Bool("debug-headers", false, "log header mismatches")
port = flag.Int("port", 8080, "port of the proxy")
controlPort = flag.Int("control-port", 8181, "port for controlling the proxy")
record = flag.String("record", "", "record traffic and save to filename")
replay = flag.String("replay", "", "read filename and replay traffic")
debugHeaders = flag.Bool("debug-headers", false, "log header mismatches")
ignoreHeaders repeatedString
)

func main() {
flag.Var(&ignoreHeaders, "ignore-header", "header key(s) to ignore when matching")

flag.Parse()
if *record == "" && *replay == "" {
log.Fatal("provide either -record or -replay")
Expand All @@ -63,6 +67,9 @@ func main() {
log.Fatal(err)
}
proxy.DebugHeaders = *debugHeaders
for _, key := range ignoreHeaders {
pr.IgnoreHeader(key)
}

// Expose handlers on the control port.
mux := http.NewServeMux()
Expand Down Expand Up @@ -108,3 +115,18 @@ func handleInitial(pr *proxy.Proxy) http.HandlerFunc {
}
}
}

type repeatedString []string

func (i *repeatedString) String() string {
v := make([]string, 0)
if i != nil {
v = *i
}
return strings.Join(v, ",")
}

func (i *repeatedString) Set(value string) error {
*i = append(*i, value)
return nil
}
13 changes: 12 additions & 1 deletion httpreplay/cmd/httpr/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,15 @@ func start(modeFlag, filename string) (*exec.Cmd, *http.Transport, string, error
if err != nil {
return nil, nil, "", err
}
cmd := exec.Command("./httpr", "-port", pport, "-control-port", cport, modeFlag, filename, "-debug-headers")
cmd := exec.Command("./httpr",
"-port", pport,
"-control-port", cport,
modeFlag,
filename,
"-debug-headers",
"-ignore-header", "X-Goog-Api-Client",
"-ignore-header", "X-Goog-Gcs-Idempotency-Token",
)
if err := cmd.Start(); err != nil {
return nil, nil, "", err
}
Expand Down Expand Up @@ -204,6 +212,9 @@ func proxyTransport(pport, cport string) (*http.Transport, error) {
}

func getBucketInfo(ctx context.Context, hc *http.Client) (string, error) {
ctx, cc := context.WithTimeout(ctx, 10*time.Second)
defer cc()

client, err := storage.NewClient(ctx, option.WithHTTPClient(hc))
if err != nil {
return "", err
Expand Down
13 changes: 11 additions & 2 deletions httpreplay/httpreplay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func TestIntegration_RecordAndReplay(t *testing.T) {
if err != nil {
t.Fatal(err)
}
rec.RemoveRequestHeaders("X-Goog-Api-Client")
rec.RemoveRequestHeaders("X-Goog-Gcs-Idempotency-Token")

hc, err := rec.Client(ctx, option.WithTokenSource(
testutil.TokenSource(ctx, storage.ScopeFullControl)))
if err != nil {
Expand All @@ -84,6 +87,8 @@ func TestIntegration_RecordAndReplay(t *testing.T) {
if err != nil {
t.Fatal(err)
}
rep.IgnoreHeader("X-Goog-Api-Client")
rep.IgnoreHeader("X-Goog-Gcs-Idempotency-Token")
defer rep.Close()
hc, err = rep.Client(ctx)
if err != nil {
Expand Down Expand Up @@ -153,7 +158,9 @@ func setup(ctx context.Context) (cleanup func(), err error) {
// TODO(jba): test errors

func run(t *testing.T, hc *http.Client) (*storage.BucketAttrs, []byte) {
ctx := context.Background()
ctx, cc := context.WithTimeout(context.Background(), 10*time.Second)
defer cc()

client, err := storage.NewClient(ctx, option.WithHTTPClient(hc))
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -188,7 +195,9 @@ func run(t *testing.T, hc *http.Client) (*storage.BucketAttrs, []byte) {
}

func testReadCRC(t *testing.T, hc *http.Client, mode string) {
ctx := context.Background()
ctx, cc := context.WithTimeout(context.Background(), 10*time.Second)
defer cc()

client, err := storage.NewClient(ctx, option.WithHTTPClient(hc))
if err != nil {
t.Fatalf("%s: %v", mode, err)
Expand Down

0 comments on commit 1829706

Please sign in to comment.