From cc0496d753d5c424c5162395bcc562900a4e6666 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Fri, 9 Feb 2024 17:24:23 +0000 Subject: [PATCH] fixup! Add parsedRemotePersisterConfig --- browser/file_persister.go | 34 ++++++++++++++++------------------ browser/file_persister_test.go | 34 +++++++++++++++++----------------- 2 files changed, 33 insertions(+), 35 deletions(-) diff --git a/browser/file_persister.go b/browser/file_persister.go index d7c689f88..cc5fb764d 100644 --- a/browser/file_persister.go +++ b/browser/file_persister.go @@ -9,10 +9,10 @@ import ( "github.com/grafana/xk6-browser/storage" ) -type parsedRemotePersisterConfig struct { - preSignedURLGetterURL string - headers map[string]string - basePath string +type presignedURLConfig struct { + getterURL string + headers map[string]string + basePath string } // newFilePersister will return either a persister that persists file to the local @@ -29,15 +29,13 @@ func newFilePersister(envLookup env.LookupFunc) (filePersister, error) { return nil, fmt.Errorf("parsing %s: %w", env.ScreenshotsOutput, err) } - return storage.NewRemoteFilePersister(popts.preSignedURLGetterURL, popts.headers, popts.basePath), nil + return storage.NewRemoteFilePersister(popts.getterURL, popts.headers, popts.basePath), nil } // parseEnvVar will parse a value such as: // url=https://127.0.0.1/,basePath=/screenshots,header.1=a,header.2=b // and return them. -// -//nolint:funlen,cyclop -func parseEnvVar(envVarValue string) (parsedRemotePersisterConfig, error) { +func parseEnvVar(envVarValue string) (presignedURLConfig, error) { ss := strings.Split(envVarValue, ",") var ( @@ -50,7 +48,7 @@ func parseEnvVar(envVarValue string) (parsedRemotePersisterConfig, error) { // on '=' to retrieve the key and value separately. kv := strings.Split(s, "=") if len(kv) <= 1 || len(kv) > 2 { - return parsedRemotePersisterConfig{}, fmt.Errorf("format of value must be k=v, received %q", s) + return presignedURLConfig{}, fmt.Errorf("format of value must be k=v, received %q", s) } k := kv[0] @@ -66,14 +64,14 @@ func parseEnvVar(envVarValue string) (parsedRemotePersisterConfig, error) { hh := strings.Split(k, ".") if len(hh) <= 1 || len(hh) > 2 { - return parsedRemotePersisterConfig{}, fmt.Errorf("format of header must be header.k=v, received %q", s) + return presignedURLConfig{}, fmt.Errorf("format of header must be header.k=v, received %q", s) } k = hh[0] hk = hh[1] if hk == "" { - return parsedRemotePersisterConfig{}, fmt.Errorf("empty header key, received %q", s) + return presignedURLConfig{}, fmt.Errorf("empty header key, received %q", s) } } @@ -81,7 +79,7 @@ func parseEnvVar(envVarValue string) (parsedRemotePersisterConfig, error) { case "url": _, err := url.ParseRequestURI(v) if err != nil { - return parsedRemotePersisterConfig{}, fmt.Errorf("invalid url %q", s) + return presignedURLConfig{}, fmt.Errorf("invalid url %q", s) } preSignedURLGetterURL = v case "basePath": @@ -89,17 +87,17 @@ func parseEnvVar(envVarValue string) (parsedRemotePersisterConfig, error) { case "header": headers[hk] = hv default: - return parsedRemotePersisterConfig{}, fmt.Errorf("invalid option %q", k) + return presignedURLConfig{}, fmt.Errorf("invalid option %q", k) } } if preSignedURLGetterURL == "" { - return parsedRemotePersisterConfig{}, fmt.Errorf("missing required url") + return presignedURLConfig{}, fmt.Errorf("missing required url") } - return parsedRemotePersisterConfig{ - preSignedURLGetterURL: preSignedURLGetterURL, - headers: headers, - basePath: basePath, + return presignedURLConfig{ + getterURL: preSignedURLGetterURL, + headers: headers, + basePath: basePath, }, nil } diff --git a/browser/file_persister_test.go b/browser/file_persister_test.go index 6ace4d96d..a950843b0 100644 --- a/browser/file_persister_test.go +++ b/browser/file_persister_test.go @@ -12,15 +12,15 @@ func Test_parseEnvVar(t *testing.T) { tests := []struct { name string envVarValue string - want parsedRemotePersisterConfig + want presignedURLConfig wantErr string }{ { name: "url_headers_basePath", envVarValue: "url=https://127.0.0.1/,basePath=/screenshots,header.1=a,header.2=b", - want: parsedRemotePersisterConfig{ - preSignedURLGetterURL: "https://127.0.0.1/", - basePath: "/screenshots", + want: presignedURLConfig{ + getterURL: "https://127.0.0.1/", + basePath: "/screenshots", headers: map[string]string{ "1": "a", "2": "b", @@ -30,8 +30,8 @@ func Test_parseEnvVar(t *testing.T) { { name: "url_headers", envVarValue: "url=https://127.0.0.1/,header.1=a,header.2=b", - want: parsedRemotePersisterConfig{ - preSignedURLGetterURL: "https://127.0.0.1/", + want: presignedURLConfig{ + getterURL: "https://127.0.0.1/", headers: map[string]string{ "1": "a", "2": "b", @@ -41,27 +41,27 @@ func Test_parseEnvVar(t *testing.T) { { name: "url", envVarValue: "url=https://127.0.0.1/", - want: parsedRemotePersisterConfig{ - preSignedURLGetterURL: "https://127.0.0.1/", - headers: map[string]string{}, + want: presignedURLConfig{ + getterURL: "https://127.0.0.1/", + headers: map[string]string{}, }, }, { name: "url_basePath", envVarValue: "url=https://127.0.0.1/,basePath=/screenshots", - want: parsedRemotePersisterConfig{ - preSignedURLGetterURL: "https://127.0.0.1/", - basePath: "/screenshots", - headers: map[string]string{}, + want: presignedURLConfig{ + getterURL: "https://127.0.0.1/", + basePath: "/screenshots", + headers: map[string]string{}, }, }, { name: "empty_basePath", envVarValue: "url=https://127.0.0.1/,basePath=", - want: parsedRemotePersisterConfig{ - preSignedURLGetterURL: "https://127.0.0.1/", - basePath: "", - headers: map[string]string{}, + want: presignedURLConfig{ + getterURL: "https://127.0.0.1/", + basePath: "", + headers: map[string]string{}, }, }, {