(expectedResp models.HTTPResp, stream io.Reader, logger *zap.Logger)
| 1307 | } |
| 1308 | |
| 1309 | func compareBinaryStream(expectedResp models.HTTPResp, stream io.Reader, logger *zap.Logger) (bool, string, *StreamMismatchInfo, error) { |
| 1310 | expectedBytes := expectedBinaryBytes(expectedResp) |
| 1311 | expectedSize := len(expectedBytes) |
| 1312 | actualSize := 0 |
| 1313 | contentMatch := true |
| 1314 | mismatchOffset := -1 |
| 1315 | buffer := make([]byte, 32*1024) |
| 1316 | |
| 1317 | for { |
| 1318 | n, err := stream.Read(buffer) |
| 1319 | if n > 0 { |
| 1320 | if contentMatch && actualSize < expectedSize { |
| 1321 | end := actualSize + n |
| 1322 | if end > expectedSize { |
| 1323 | end = expectedSize |
| 1324 | } |
| 1325 | if !bytes.Equal(buffer[:end-actualSize], expectedBytes[actualSize:end]) { |
| 1326 | contentMatch = false |
| 1327 | mismatchOffset = actualSize |
| 1328 | } |
| 1329 | } |
| 1330 | actualSize += n |
| 1331 | if actualSize >= expectedSize { |
| 1332 | if actualSize > expectedSize { |
| 1333 | logger.Debug("received additional binary stream data after expected size was matched; closing stream capture", |
| 1334 | zap.Int("expected_size", expectedSize), |
| 1335 | zap.Int("actual_size", actualSize)) |
| 1336 | } |
| 1337 | break |
| 1338 | } |
| 1339 | } |
| 1340 | if err == io.EOF { |
| 1341 | break |
| 1342 | } |
| 1343 | if err != nil { |
| 1344 | return false, strconv.Itoa(actualSize), nil, err |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | if actualSize != expectedSize { |
| 1349 | logger.Debug("binary stream size mismatch", |
| 1350 | zap.Int("expected_size", expectedSize), |
| 1351 | zap.Int("actual_size", actualSize)) |
| 1352 | mismatchInfo := &StreamMismatchInfo{ |
| 1353 | FrameIndex: 0, |
| 1354 | ExpectedFrame: fmt.Sprintf("%d bytes", expectedSize), |
| 1355 | ActualFrame: fmt.Sprintf("%d bytes", actualSize), |
| 1356 | Reason: fmt.Sprintf("size mismatch: expected %d bytes, got %d bytes", expectedSize, actualSize), |
| 1357 | } |
| 1358 | return false, strconv.Itoa(actualSize), mismatchInfo, nil |
| 1359 | } |
| 1360 | |
| 1361 | if !contentMatch { |
| 1362 | logger.Debug("binary stream content mismatch", |
| 1363 | zap.Int("size", actualSize), |
| 1364 | zap.Int("first_mismatch_offset", mismatchOffset)) |
| 1365 | mismatchInfo := &StreamMismatchInfo{ |
| 1366 | FrameIndex: 0, |
no test coverage detected