Skip to content

Commit

Permalink
fix fixed matching algorithm (#1022)
Browse files Browse the repository at this point in the history
Signed-off-by: EraKin575 <tejaskumar574@gmail.com>
  • Loading branch information
EraKin575 committed Oct 17, 2023
1 parent beb661e commit 8c24da2
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions pkg/proxy/integrations/genericParser/util.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package genericparser

import (
"encoding/base64"
// "fmt"
Expand All @@ -24,58 +23,46 @@ func PostgresDecoder(encoded string) ([]byte, error) {
}

func fuzzymatch(tcsMocks []*models.Mock, requestBuffers [][]byte, h *hooks.Hook) (bool, []models.GenericPayload) {

for idx, mock := range tcsMocks {
if len(mock.Spec.GenericRequests) == len(requestBuffers) {
for requestIndex, reqBuff := range requestBuffers {
matched := true // Flag to track if all requests match

// bufStr := string(reqBuff)
// if !IsAsciiPrintable(bufStr) {
for requestIndex, reqBuff := range requestBuffers {
bufStr := string(reqBuff)
if !IsAsciiPrintable(string(reqBuff)) {
bufStr = base64.StdEncoding.EncodeToString(reqBuff)
}
// }

encoded := []byte(mock.Spec.GenericRequests[requestIndex].Message[0].Data)
if !IsAsciiPrintable(mock.Spec.GenericRequests[requestIndex].Message[0].Data) {
encoded, _ = PostgresDecoder(mock.Spec.GenericRequests[requestIndex].Message[0].Data)
}

if string(encoded) == string(reqBuff) || mock.Spec.GenericRequests[requestIndex].Message[0].Data == bufStr {
log.Debug("matched in first loop")
tcsMocks = append(tcsMocks[:idx], tcsMocks[idx+1:]...)
h.SetTcsMocks(tcsMocks)
return true, mock.Spec.GenericResponses
// Compare the encoded data
if string(encoded) != string(reqBuff) || mock.Spec.GenericRequests[requestIndex].Message[0].Data != bufStr {
matched = false
break // Exit the loop if any request doesn't match
}
}

if matched {
log.Debug("matched in first loop")
tcsMocks = append(tcsMocks[:idx], tcsMocks[idx+1:]...)
h.SetTcsMocks(tcsMocks)
return true, mock.Spec.GenericResponses
}
}
}
// com := PostgresEncoder(reqBuff)
// convert all the configmocks to string array
// mockString := make([]string, len(tcsMocks))
// for i := 0; i < len(tcsMocks); i++ {
// mockString[i] = string(tcsMocks[i].Spec.PostgresReq.Payload)
// }
// // find the closest match
// if IsAsciiPrintable(string(reqBuff)) {
// fmt.Println("Inside String Match")
// idx := findStringMatch(string(reqBuff), mockString)
// if idx != -1 {
// nMatch := tcsMocks[idx].Spec.PostgresResp.Payload
// tcsMocks = append(tcsMocks[:idx], tcsMocks[idx+1:]...)
// h.SetConfigMocks(tcsMocks)
// fmt.Println("Returning mock from String Match !!")
// return true, nMatch
// }
// }

idx := findBinaryMatch(tcsMocks, requestBuffers, h)
if idx != -1 {
log.Debug("matched in first loop")
log.Debug("matched in binary match")
bestMatch := tcsMocks[idx].Spec.GenericResponses
tcsMocks = append(tcsMocks[:idx], tcsMocks[idx+1:]...)
h.SetTcsMocks(tcsMocks)
return true, bestMatch
}

return false, nil
}

Expand Down Expand Up @@ -175,4 +162,4 @@ func findStringMatch(req []string, mockString []string) int {
}
}
return bestMatch
}
}

0 comments on commit 8c24da2

Please sign in to comment.