MCPcopy Index your code
hub / github.com/keploy/keploy / comparePlainTextStream

Function comparePlainTextStream

pkg/util.go:1248–1307  ·  view source on GitHub ↗
(expectedResp models.HTTPResp, stream io.Reader, logger *zap.Logger)

Source from the content-addressed store, hash-verified

1246}
1247
1248func comparePlainTextStream(expectedResp models.HTTPResp, stream io.Reader, logger *zap.Logger) (bool, string, *StreamMismatchInfo, error) {
1249 expectedQueue := extractExpectedRawQueue(expectedResp, canonicalizePlainTextLine, false)
1250 actualQueue := make([]string, 0, len(expectedQueue))
1251 nextExpected := 0
1252
1253 scanner := bufio.NewScanner(stream)
1254 scanner.Buffer(make([]byte, 0, 64*1024), maxStreamTokenSize)
1255
1256 for scanner.Scan() {
1257 line := canonicalizePlainTextLine(scanner.Text())
1258
1259 if nextExpected >= len(expectedQueue) {
1260 logger.Debug("received additional plain-text stream data after expected stream was fully matched; closing stream capture",
1261 zap.Int("expected_frames", len(expectedQueue)))
1262 break
1263 }
1264
1265 actualQueue = append(actualQueue, line)
1266 expected := expectedQueue[nextExpected]
1267 if line != expected {
1268 logger.Debug("plain-text stream mismatch",
1269 zap.Int("frame_index", nextExpected),
1270 zap.String("expected", expected),
1271 zap.String("actual", line))
1272 mismatchInfo := &StreamMismatchInfo{
1273 FrameIndex: nextExpected,
1274 ExpectedFrame: expected,
1275 ActualFrame: line,
1276 Reason: fmt.Sprintf("content mismatch at frame %d", nextExpected),
1277 }
1278 return false, strings.Join(actualQueue, "\n"), mismatchInfo, nil
1279 }
1280
1281 nextExpected++
1282 if nextExpected == len(expectedQueue) {
1283 logger.Debug("all expected plain-text frames matched; closing stream capture early to avoid waiting for extra stream events",
1284 zap.Int("matched_frames", nextExpected))
1285 break
1286 }
1287 }
1288
1289 if err := scanner.Err(); err != nil {
1290 return false, strings.Join(actualQueue, "\n"), nil, err
1291 }
1292
1293 if nextExpected < len(expectedQueue) {
1294 logger.Debug("plain-text stream ended before all expected frames were received",
1295 zap.Int("expected_frames", len(expectedQueue)),
1296 zap.Int("matched_frames", nextExpected))
1297 mismatchInfo := &StreamMismatchInfo{
1298 FrameIndex: nextExpected,
1299 ExpectedFrame: expectedQueue[nextExpected],
1300 ActualFrame: "(stream ended - no more frames)",
1301 Reason: fmt.Sprintf("expected %d frames but only received %d", len(expectedQueue), nextExpected),
1302 }
1303 return false, strings.Join(actualQueue, "\n"), mismatchInfo, nil
1304 }
1305

Callers 1

CompareHTTPStreamFunction · 0.85

Calls 4

extractExpectedRawQueueFunction · 0.85
DebugMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected