Skip to content

Commit

Permalink
fixes for recorder and playback, limit filename length
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanHCB committed Apr 9, 2022
1 parent ede1c55 commit 36bb0ff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
18 changes: 17 additions & 1 deletion implementation/playback/playback.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
aurestclientapi "github.com/StephanHCB/go-autumn-restclient/api"
aurestrecorder "github.com/StephanHCB/go-autumn-restclient/implementation/recorder"
"os"
"strings"
)

type PlaybackImpl struct {
Expand All @@ -16,6 +17,11 @@ type PlaybackImpl struct {
//
// Use this in your tests.
func New(recorderPath string) aurestclientapi.Client {
if recorderPath != "" {
if !strings.HasSuffix(recorderPath, "/") {
recorderPath += "/"
}
}
return &PlaybackImpl{
RecorderPath: recorderPath,
}
Expand All @@ -40,6 +46,16 @@ func (c *PlaybackImpl) Perform(_ context.Context, method string, requestUrl stri

response.Header = recording.ParsedResponse.Header
response.Status = recording.ParsedResponse.Status
response.Body = recording.ParsedResponse.Body

// cannot just assign the body, need to re-parse into the existing pointer - using a json round trip
bodyJsonBytes, err := json.Marshal(recording.ParsedResponse.Body)
if err != nil {
return err
}
err = json.Unmarshal(bodyJsonBytes, response.Body)
if err != nil {
return err
}

return recording.Error
}
10 changes: 7 additions & 3 deletions implementation/recorder/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package aurestrecorder

import (
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
aurestclientapi "github.com/StephanHCB/go-autumn-restclient/api"
Expand Down Expand Up @@ -58,7 +60,7 @@ func (c *RecorderImpl) Perform(ctx context.Context, method string, requestUrl st
Error: responseErr,
}

jsonRecording, err := json.Marshal(&recording)
jsonRecording, err := json.MarshalIndent(&recording, "", " ")
if err == nil {
_ = os.WriteFile(c.RecorderPath+filename, jsonRecording, 0644)
}
Expand All @@ -74,8 +76,10 @@ func ConstructFilename(method string, requestUrl string) (string, error) {
}

m := strings.ToLower(method)
p := parsedUrl.EscapedPath()
q := parsedUrl.RawQuery
p := url.QueryEscape(parsedUrl.EscapedPath())
// 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[:])

filename := fmt.Sprintf("request_%s_%s_%s.json", m, p, q)
return filename, nil
Expand Down

0 comments on commit 36bb0ff

Please sign in to comment.