Skip to content

Commit

Permalink
Merge pull request #8 from StephanHCB/feature/7-shorter-recording-fil…
Browse files Browse the repository at this point in the history
…enames

implement shorter better readable recording filenames - fixes #7
  • Loading branch information
StephanHCB committed Sep 26, 2022
2 parents 9e11981 + d558a75 commit 995530d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
18 changes: 15 additions & 3 deletions implementation/playback/playback.go
Expand Up @@ -3,6 +3,7 @@ package aurestplayback
import (
"context"
"encoding/json"
aulogging "github.com/StephanHCB/go-autumn-logging"
aurestclientapi "github.com/StephanHCB/go-autumn-restclient/api"
aurestrecorder "github.com/StephanHCB/go-autumn-restclient/implementation/recorder"
"os"
Expand Down Expand Up @@ -31,15 +32,26 @@ func New(recorderPath string) aurestclientapi.Client {
}
}

func (c *PlaybackImpl) Perform(_ context.Context, method string, requestUrl string, _ interface{}, response *aurestclientapi.ParsedResponse) error {
filename, err := aurestrecorder.ConstructFilename(method, requestUrl)
func (c *PlaybackImpl) Perform(ctx context.Context, method string, requestUrl string, _ interface{}, response *aurestclientapi.ParsedResponse) error {
filename, err := aurestrecorder.ConstructFilenameV2(method, requestUrl)
if err != nil {
return err
}

jsonBytes, err := os.ReadFile(c.RecorderPath + filename)
if err != nil {
return err
// try old filename for compatibility (cannot fail if ConstructFilenameV2 didn't)
filenameOld, _ := aurestrecorder.ConstructFilename(method, requestUrl)

jsonBytesOld, errWithOldFilename := os.ReadFile(c.RecorderPath + filenameOld)
if errWithOldFilename != nil {
// but return original error if that also fails
return err
} else {
aulogging.Logger.Ctx(ctx).Info().Printf("use of deprecated recorder filename '%s', please move to '%s'", filenameOld, filename)
filename = filenameOld
jsonBytes = jsonBytesOld
}
}

recording := aurestrecorder.RecorderData{}
Expand Down
26 changes: 25 additions & 1 deletion implementation/recorder/recorder.go
Expand Up @@ -50,7 +50,7 @@ type RecorderData struct {
func (c *RecorderImpl) Perform(ctx context.Context, method string, requestUrl string, requestBody interface{}, response *aurestclientapi.ParsedResponse) error {
responseErr := c.Wrapped.Perform(ctx, method, requestUrl, requestBody, response)
if c.RecorderPath != "" {
filename, err := ConstructFilename(method, requestUrl)
filename, err := ConstructFilenameV2(method, requestUrl)
if err == nil {
recording := RecorderData{
Method: method,
Expand Down Expand Up @@ -87,3 +87,27 @@ func ConstructFilename(method string, requestUrl string) (string, error) {
filename := fmt.Sprintf("request_%s_%s_%s.json", m, p, q)
return filename, nil
}

func ConstructFilenameV2(method string, requestUrl string) (string, error) {
parsedUrl, err := url.Parse(requestUrl)
if err != nil {
return "", err
}

m := strings.ToLower(method)
p := url.QueryEscape(parsedUrl.EscapedPath())
if len(p) > 120 {
p = string([]byte(p)[:120])
}
p = strings.ReplaceAll(p, "%2F", "-")
p = strings.TrimLeft(p, "-")
p = strings.TrimRight(p, "-")

// we have to ensure the filenames don't get too long. git for windows only supports 260 character paths
md5sumOverQuery := md5.Sum([]byte(parsedUrl.RawQuery))
q := hex.EncodeToString(md5sumOverQuery[:])
q = q[:8]

filename := fmt.Sprintf("request_%s_%s_%s.json", m, p, q)
return filename, nil
}
16 changes: 16 additions & 0 deletions implementation/recorder/recorder_test.go
Expand Up @@ -20,3 +20,19 @@ func TestConstructFilenameShort(t *testing.T) {
require.Nil(t, err)
require.Equal(t, expected, actual)
}

func TestConstructFilenameV2Long(t *testing.T) {
requestUrl := "https://some.super.long.server.name.that.hopefully.does.not.exist/api/rest/v1/v2/v3/v4/this/is/intentionally/very/very/very/very/long/djkfjhdalsfhdsahjflkdjsahfkjlsdhafjkdshafkjlsdahf/asdfjkldsahfkjlfad/dskjfhjkdsfahlk/sdafjkhsdafklhreuih/dfgjgkjlhgjlkhg?asjdfhlkhewuirfhkjsdhlk=kjhrelrukihsjd&fsdkjhfdjklhsdf=werjkyewuiryuweiry&sfuyfddsjkhjkldsfhldkfs=sdjhdflksjhfdhsf"
actual, err := ConstructFilenameV2("GET", requestUrl)
expected := "request_get_api-rest-v1-v2-v3-v4-this-is-intentionally-very-very-very-very-long-djkfjhdalsfhdsahjflkd_fb2e3656.json"
require.Nil(t, err)
require.Equal(t, expected, actual)
}

func TestConstructFilenameV2Short(t *testing.T) {
requestUrl := "https://some.short.server.name/api/rest/v1/kittens"
actual, err := ConstructFilenameV2("GET", requestUrl)
expected := "request_get_api-rest-v1-kittens_d41d8cd9.json"
require.Nil(t, err)
require.Equal(t, expected, actual)
}

0 comments on commit 995530d

Please sign in to comment.