Fix linting issues and refactor for improved code quality

This commit is contained in:
Tobias Gesellchen
2026-02-14 12:39:39 +01:00
parent 9be1c7d588
commit dcf2e29c16
4 changed files with 172 additions and 156 deletions
@@ -19,8 +19,10 @@ func (s *Server) RecordMiddleware(next http.Handler) http.Handler {
// Buffer the request body if it exists
var reqBody []byte
if r.Body != nil {
var err error
reqBody, err = io.ReadAll(r.Body)
if err == nil {
r.Body = io.NopCloser(bytes.NewBuffer(reqBody))
@@ -37,6 +39,9 @@ func (s *Server) RecordMiddleware(next http.Handler) http.Handler {
// Create a response object for the recorder
res := rw.getRecordedResponse(r)
if res.Body != nil {
defer func() { _ = res.Body.Close() }()
}
// Put back the original request body for recording
r.Body = io.NopCloser(bytes.NewBuffer(reqBody))
@@ -89,5 +94,6 @@ func (rw *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if h, ok := rw.ResponseWriter.(http.Hijacker); ok {
return h.Hijack()
}
return nil, nil, fmt.Errorf("ResponseWriter does not support Hijacker")
}
+4
View File
@@ -25,6 +25,7 @@ func LoadPatterns(path string) (PathPatterns, error) {
if os.IsNotExist(err) {
return PathPatterns{}, nil
}
return nil, err
}
@@ -38,6 +39,7 @@ func LoadPatterns(path string) (PathPatterns, error) {
if err != nil {
return nil, fmt.Errorf("invalid regex in pattern %s: %w", patterns[i].Name, err)
}
patterns[i].compiled = re
}
@@ -51,6 +53,7 @@ func (pp PathPatterns) Sanitize(segment string) (string, string) {
return p.Replacement, p.Replacement
}
}
return segment, ""
}
@@ -63,5 +66,6 @@ func DefaultPatterns() PathPatterns {
}
re, _ := regexp.Compile(p.Regexp)
p.compiled = re
return PathPatterns{p}
}
+81 -48
View File
@@ -29,6 +29,7 @@ type Recorder struct {
// NewRecorder creates a new HTTP interaction recorder.
func NewRecorder(baseDir string) *Recorder {
sessionID := time.Now().Format("20060102-150405") + "-" + fmt.Sprintf("%d", os.Getpid())
return &Recorder{
BaseDir: baseDir,
SessionID: sessionID,
@@ -43,10 +44,35 @@ func (r *Recorder) Record(category string, req *http.Request, res *http.Response
return nil
}
// Group by URL path, sanitizing variable segments like IP addresses
pathSegments := strings.Split(strings.Trim(req.URL.Path, "/"), "/")
sanitizedSegments, replacements := r.getSanitizedSegments(req.URL.Path)
dir := r.getRecordingDir(category, sanitizedSegments)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create directory %s: %w", dir, err)
}
path := r.getRecordingPath(dir, req.Method)
var buf bytes.Buffer
r.writeRequest(&buf, req, replacements)
if res != nil {
r.writeResponse(&buf, res)
}
if err := os.WriteFile(path, buf.Bytes(), 0644); err != nil {
return err
}
return r.updateEnvFile(replacements)
}
func (r *Recorder) getSanitizedSegments(path string) ([]string, map[string]string) {
pathSegments := strings.Split(strings.Trim(path, "/"), "/")
sanitizedSegments := make([]string, 0, len(pathSegments))
replacements := make(map[string]string)
for _, segment := range pathSegments {
if segment == "" {
continue
@@ -54,53 +80,63 @@ func (r *Recorder) Record(category string, req *http.Request, res *http.Response
sanitized, replacement := r.Patterns.Sanitize(segment)
sanitizedSegments = append(sanitizedSegments, sanitized)
if replacement != "" {
replacements[segment] = replacement
}
}
return sanitizedSegments, replacements
}
func (r *Recorder) getRecordingDir(category string, sanitizedSegments []string) string {
subDir := "root"
if len(sanitizedSegments) > 0 {
subDir = filepath.Join(sanitizedSegments...)
}
dir := filepath.Join(r.BaseDir, "interactions", r.SessionID, category, subDir)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create directory %s: %w", dir, err)
}
return filepath.Join(r.BaseDir, "interactions", r.SessionID, category, subDir)
}
func (r *Recorder) getRecordingPath(dir, method string) string {
timestamp := time.Now().Format("15-04-05.000")
count := atomic.AddUint64(&r.counter, 1)
filename := fmt.Sprintf("%04d-%s-%s.http", count, timestamp, req.Method)
path := filepath.Join(dir, filename)
filename := fmt.Sprintf("%04d-%s-%s.http", count, timestamp, method)
var buf bytes.Buffer
return filepath.Join(dir, filename)
}
// Write Request
func (r *Recorder) writeRequest(buf *bytes.Buffer, req *http.Request, replacements map[string]string) {
displayURL := req.URL.String()
for orig, repl := range replacements {
displayURL = strings.ReplaceAll(displayURL, orig, "{{"+strings.Trim(repl, "{}")+"}}")
}
buf.WriteString(fmt.Sprintf("### %s %s\n", req.Method, displayURL))
fmt.Fprintf(buf, "### %s %s\n", req.Method, displayURL)
for orig, repl := range replacements {
key := strings.Trim(repl, "{}")
buf.WriteString(fmt.Sprintf("// %s: %s\n", key, orig))
fmt.Fprintf(buf, "// %s: %s\n", key, orig)
}
buf.WriteString(fmt.Sprintf("%s %s\n", req.Method, displayURL))
fmt.Fprintf(buf, "%s %s\n", req.Method, displayURL)
for k, vv := range req.Header {
if r.Redact && isSensitive(k) {
buf.WriteString(fmt.Sprintf("%s: [REDACTED]\n", k))
fmt.Fprintf(buf, "%s: [REDACTED]\n", k)
continue
}
for _, v := range vv {
val := v
for orig, repl := range replacements {
val = strings.ReplaceAll(val, orig, "{{"+strings.Trim(repl, "{}")+"}}")
}
buf.WriteString(fmt.Sprintf("%s: %s\n", k, val))
fmt.Fprintf(buf, "%s: %s\n", k, val)
}
}
buf.WriteString("\n")
if req.Body != nil {
@@ -111,46 +147,42 @@ func (r *Recorder) Record(category string, req *http.Request, res *http.Response
buf.WriteString("\n")
}
}
}
// Write Response if available
if res != nil {
buf.WriteString("\n")
buf.WriteString("> {% \n")
buf.WriteString(fmt.Sprintf(" // Response: %d %s\n", res.StatusCode, http.StatusText(res.StatusCode)))
buf.WriteString(" // Headers:\n")
for k, vv := range res.Header {
if r.Redact && isSensitive(k) {
buf.WriteString(fmt.Sprintf(" // %s: [REDACTED]\n", k))
continue
}
for _, v := range vv {
buf.WriteString(fmt.Sprintf(" // %s: %s\n", k, v))
}
func (r *Recorder) writeResponse(buf *bytes.Buffer, res *http.Response) {
buf.WriteString("\n")
buf.WriteString("> {% \n")
fmt.Fprintf(buf, " // Response: %d %s\n", res.StatusCode, http.StatusText(res.StatusCode))
buf.WriteString(" // Headers:\n")
for k, vv := range res.Header {
if r.Redact && isSensitive(k) {
fmt.Fprintf(buf, " // %s: [REDACTED]\n", k)
continue
}
buf.WriteString("%}\n")
if res.Body != nil {
bodyBytes, err := io.ReadAll(res.Body)
if err == nil {
res.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
contentType := res.Header.Get("Content-Type")
if strings.Contains(contentType, "xml") || strings.Contains(contentType, "json") || strings.Contains(contentType, "text") {
buf.WriteString("\n/*\n")
buf.Write(bodyBytes)
buf.WriteString("\n*/\n")
} else {
buf.WriteString(fmt.Sprintf("\n// [Binary response body: %d bytes]\n", len(bodyBytes)))
}
}
for _, v := range vv {
fmt.Fprintf(buf, " // %s: %s\n", k, v)
}
}
if err := os.WriteFile(path, buf.Bytes(), 0644); err != nil {
return err
}
buf.WriteString("%}\n")
return r.updateEnvFile(replacements)
if res.Body != nil {
bodyBytes, err := io.ReadAll(res.Body)
if err == nil {
res.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
contentType := res.Header.Get("Content-Type")
if strings.Contains(contentType, "xml") || strings.Contains(contentType, "json") || strings.Contains(contentType, "text") {
buf.WriteString("\n/*\n")
buf.Write(bodyBytes)
buf.WriteString("\n*/\n")
} else {
fmt.Fprintf(buf, "\n// [Binary response body: %d bytes]\n", len(bodyBytes))
}
}
}
}
func (r *Recorder) updateEnvFile(newVars map[string]string) error {
@@ -162,6 +194,7 @@ func (r *Recorder) updateEnvFile(newVars map[string]string) error {
defer r.mu.Unlock()
changed := false
for orig, repl := range newVars {
key := strings.Trim(repl, "{}")
if r.variables[key] != orig {