MCPcopy
hub / github.com/keploy/keploy / printRequestDiff

Function printRequestDiff

pkg/service/replay/utils.go:990–1031  ·  view source on GitHub ↗

printRequestDiff renders the closest mock's recorded request against the live request in the SAME visual style as an HTTP test-case (response) mismatch: a boxed diff whose rows are Expect/Actual sub-tables for the request line, headers and body, produced by the shared matcher.DiffsPrinter. expected

(mockName, expected, received string)

Source from the content-addressed store, hash-verified

988// parser emitted; the body therefore diffs field-by-field (changed fields
989// only), exactly like a response-body mismatch.
990func printRequestDiff(mockName, expected, received string) {
991 title := mockName
992 if title == "" {
993 title = "closest mock"
994 }
995 dp := matcherUtils.NewDiffsPrinter(title)
996
997 mockLine, mockHeaders, mockBody := parseRenderedRequest(expected)
998 liveLine, liveHeaders, liveBody := parseRenderedRequest(received)
999
1000 // Request line -> method + target, surfaced as Expect/Actual rows only when
1001 // they differ (for a body mismatch the closest mock shares method/path).
1002 mockMethod, mockTarget := splitRequestLine(mockLine)
1003 liveMethod, liveTarget := splitRequestLine(liveLine)
1004 if mockMethod != liveMethod {
1005 dp.PushHeaderDiff(mockMethod, liveMethod, "method", nil)
1006 }
1007 if mockTarget != liveTarget {
1008 dp.PushHeaderDiff(mockTarget, liveTarget, "url", nil)
1009 }
1010
1011 // Headers: push a row whenever a key is present on only one side or its
1012 // value differs. Two-value lookups distinguish an absent header from one
1013 // present with an empty value, so a key-presence mismatch (the exact reason
1014 // a mock can be rejected) is never silently dropped.
1015 for k, mv := range mockHeaders {
1016 if lv, ok := liveHeaders[k]; !ok || mv != lv {
1017 dp.PushHeaderDiff(mv, lv, k, nil)
1018 }
1019 }
1020 for k, lv := range liveHeaders {
1021 if _, ok := mockHeaders[k]; !ok {
1022 dp.PushHeaderDiff("", lv, k, nil)
1023 }
1024 }
1025
1026 // Body: JSON bodies diff field-by-field (changed fields only), just like a
1027 // response body mismatch; non-JSON falls back to a plain Expect/Actual cell.
1028 dp.PushBodyDiff(mockBody, liveBody, nil)
1029
1030 _ = dp.Render()
1031}
1032
1033// parseRenderedRequest splits a rendered request
1034// ("METHOD target\nKey: val\n...\n\n<body>") back into its request line, header

Callers 1

printMismatchReportFunction · 0.85

Calls 5

PushHeaderDiffMethod · 0.95
PushBodyDiffMethod · 0.95
RenderMethod · 0.95
parseRenderedRequestFunction · 0.85
splitRequestLineFunction · 0.85

Tested by

no test coverage detected