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

Function RenderTestCaseWithTemplates

pkg/util.go:2420–2463  ·  view source on GitHub ↗

RenderTestCaseWithTemplates returns a copy of the provided TestCase with current templated and secret values applied. This is useful for producing a concrete "expected" testcase (for example expected responses) before the test is executed and templates may get updated by the runtime.

(tc *models.TestCase)

Source from the content-addressed store, hash-verified

2418// a concrete "expected" testcase (for example expected responses) before
2419// the test is executed and templates may get updated by the runtime.
2420func RenderTestCaseWithTemplates(tc *models.TestCase) (*models.TestCase, error) {
2421 templatedValues, secretValues := snapshotTemplateState()
2422
2423 // If there are no templated or secret values, just return a deep copy
2424 if len(templatedValues) == 0 && len(secretValues) == 0 {
2425 copy := *tc
2426 return &copy, nil
2427 }
2428
2429 // Marshal the testcase and execute the template with current values
2430 testCaseStr, err := json.Marshal(tc)
2431 if err != nil {
2432 return nil, err
2433 }
2434
2435 funcMap := template.FuncMap{
2436 "int": utils.ToInt,
2437 "string": utils.ToString,
2438 "float": utils.ToFloat,
2439 }
2440 tmpl, err := template.New("template").Funcs(funcMap).Parse(string(testCaseStr))
2441 if err != nil || tmpl == nil {
2442 return nil, err
2443 }
2444
2445 data := make(map[string]interface{})
2446 for k, v := range templatedValues {
2447 data[k] = v
2448 }
2449 if len(secretValues) > 0 {
2450 data["secret"] = secretValues
2451 }
2452
2453 var output bytes.Buffer
2454 if err := tmpl.Execute(&output, data); err != nil {
2455 return nil, err
2456 }
2457
2458 var rendered models.TestCase
2459 if err := json.Unmarshal(output.Bytes(), &rendered); err != nil {
2460 return nil, err
2461 }
2462 return &rendered, nil
2463}
2464
2465// DetectNoiseFieldsInResp inspects a rendered HTTP response and returns a map
2466// of noise fields that should be marked on the testcase so matchers ignore

Callers

nothing calls this directly

Calls 5

snapshotTemplateStateFunction · 0.85
MarshalMethod · 0.80
UnmarshalMethod · 0.80
NewMethod · 0.65
BytesMethod · 0.45

Tested by

no test coverage detected