Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed matching algorithm #1022

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading