- Add a generic tide flow schema with YAML fixture IO and HTTP record/replay - Register parity:record and parity:replay on the bonfire summer tool - Diff JSON scalars at $.path and non-JSON bodies at the changed byte offset Co-authored-by: Cursor <cursoragent@cursor.com>
96 lines
2.3 KiB
Go
96 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.golem15.com/golem15/summercms/bonfire"
|
|
"git.golem15.com/golem15/summercms/tide"
|
|
)
|
|
|
|
func parityRecordCommand() bonfire.Command {
|
|
return bonfire.Command{
|
|
Name: "parity:record",
|
|
Description: "Record HTTP responses for a one-flow YAML spec",
|
|
Flags: []bonfire.Flag{
|
|
{Name: "spec", Description: "YAML request spec path"},
|
|
{Name: "target", Description: "Base URL of the HTTP backend"},
|
|
{Name: "output", Description: "Destination fixture path"},
|
|
},
|
|
Run: runParityRecord,
|
|
}
|
|
}
|
|
|
|
func parityReplayCommand() bonfire.Command {
|
|
return bonfire.Command{
|
|
Name: "parity:replay",
|
|
Description: "Replay recorded fixtures against an HTTP backend",
|
|
Flags: []bonfire.Flag{
|
|
{Name: "fixtures", Description: "Recorded YAML fixture path"},
|
|
{Name: "target", Description: "Base URL of the HTTP backend"},
|
|
},
|
|
Run: runParityReplay,
|
|
}
|
|
}
|
|
|
|
func runParityRecord(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
|
|
specPath, err := requireFlag(in, "spec", "parity:record")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
target, err := requireFlag(in, "target", "parity:record")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
output, err := requireFlag(in, "output", "parity:record")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
spec, err := tide.LoadFlow(specPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
flow, err := tide.RecordFlow(ctx, spec, tide.RecordConfig{Target: target})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := tide.SaveFlow(output, flow); err != nil {
|
|
return err
|
|
}
|
|
out.Success(fmt.Sprintf("recorded %s", output))
|
|
return nil
|
|
}
|
|
|
|
func runParityReplay(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
|
|
path, err := requireFlag(in, "fixtures", "parity:replay")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
target, err := requireFlag(in, "target", "parity:replay")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
flow, err := tide.LoadFlow(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
result, err := tide.ReplayFlow(ctx, flow, tide.ReplayConfig{Target: target})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if result.OK {
|
|
out.Success("replay matched")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func requireFlag(in bonfire.Input, name, cmd string) (string, error) {
|
|
v, ok := in.Flag(name)
|
|
v = strings.TrimSpace(v)
|
|
if !ok || v == "" {
|
|
return "", fmt.Errorf("%s requires --%s", cmd, name)
|
|
}
|
|
return v, nil
|
|
}
|