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

Method match

pkg/agent/proxy/integrations/http/match.go:135–294  ·  view source on GitHub ↗

match returns (matched, mock, diag, err). diag is non-nil only when matched is false and no error occurred. userBodyNoise carries the user's test.globalNoise body bucket (root-relative dotted paths, lowercased) so manual noise config participates in mock matching with the same vocabulary as response

(ctx context.Context, input *req, mockDb integrations.MockMemDb, headerNoise map[string][]string, userBodyNoise map[string][]string, urlNoise []string, autoURLDynamic bool, schemaNoiseDetection bool, schemaNoiseStrict bool)

Source from the content-addressed store, hash-verified

133// manual noise config participates in mock matching with the same vocabulary
134// as response assertions.
135func (h *HTTP) match(ctx context.Context, input *req, mockDb integrations.MockMemDb, headerNoise map[string][]string, userBodyNoise map[string][]string, urlNoise []string, autoURLDynamic bool, schemaNoiseDetection bool, schemaNoiseStrict bool) (bool, *models.Mock, *matchDiag, error) {
136
137 // Shared schema-noise engine for this match. HTTP is a full client of the
138 // same engine Pulsar (and any future parser) uses — httpNoiseAdapter owns
139 // only the HTTP-specific bits (body extraction, JSON/form diff).
140 noiseEngine := schemanoise.New(httpNoiseAdapter{}, schemaNoiseDetection, schemaNoiseStrict)
141
142 for {
143 if ctx.Err() != nil {
144 return false, nil, nil, ctx.Err()
145 }
146
147 // Fetch HTTP mocks from BOTH pools:
148 // - Per-test mocks (Lifetime=PerTest): test-specific app
149 // HTTP calls, window-filtered at ingest, consumed on
150 // match. These are the MORE SPECIFIC match for the
151 // current request — they were recorded for exactly this
152 // test's behaviour.
153 // - Session mocks (Lifetime=Session): auth/SigV4/SQS
154 // handshake, reusable across every test, not window-
155 // filtered, not consumed.
156 //
157 // Ordering: per-test FIRST. If a per-test mock matches, it
158 // wins over any session mock that "sort of" matches at the
159 // same score — a session mock should only be chosen when no
160 // per-test mock matches at all. This prevents the subtle
161 // regression where a per-test mock for the current test gets
162 // passed over in favour of a session mock that tied on
163 // schema but wasn't recorded for this test.
164 //
165 // Pre-unification this parser only consumed session mocks
166 // (via GetUnFilteredMocks returning the full unfiltered pool
167 // that included untagged HTTP as session-by-kind). Post-
168 // Phase-3 tag-only routing, per-test HTTP mocks land in the
169 // per-test pool — they need to be reachable here.
170 perTestMocks, err := mockDb.GetPerTestMocksInWindow()
171 if err != nil {
172 utils.LogError(h.Logger, err, "failed to get per-test mocks")
173 return false, nil, nil, errors.New("error while matching the request with the mocks")
174 }
175 sessionMocks, err := mockDb.GetSessionMocks()
176 if err != nil {
177 utils.LogError(h.Logger, err, "failed to get session mocks")
178 return false, nil, nil, errors.New("error while matching the request with the mocks")
179 }
180 combined := make([]*models.Mock, 0, len(perTestMocks)+len(sessionMocks))
181 combined = append(combined, perTestMocks...)
182 combined = append(combined, sessionMocks...)
183 unfilteredMocks := FilterHTTPMocks(combined)
184
185 // Log all mock names in a single line for better readability
186 mockNames := make([]string, len(unfilteredMocks))
187 for i, mock := range unfilteredMocks {
188 mockNames[i] = mock.Name
189 }
190 h.Logger.Debug("mocks under consideration for match function", zap.Strings("mock names", mockNames))
191
192 h.Logger.Debug(fmt.Sprintf("Length of unfilteredMocks:%v", len(unfilteredMocks)))

Callers 2

decodeHTTPMethod · 0.95

Calls 15

SchemaMatchMethod · 0.95
ExactBodyMatchMethod · 0.95
updateMockMethod · 0.95
PerformBodyMatchMethod · 0.95
PerformFuzzyMatchMethod · 0.95
NewFunction · 0.92
LogErrorFunction · 0.92
IsJSONFunction · 0.92
FilterHTTPMocksFunction · 0.85
DetectMethod · 0.80

Tested by 1