MCPcopy
hub / github.com/caddyserver/caddy / TestMatchExpressionMatch

Function TestMatchExpressionMatch

modules/caddyhttp/fileserver/matcher_test.go:506–549  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

504}
505
506func TestMatchExpressionMatch(t *testing.T) {
507 for _, tst := range expressionTests {
508 tc := tst
509 t.Run(tc.name, func(t *testing.T) {
510 caddyCtx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
511 defer cancel()
512 err := tc.expression.Provision(caddyCtx)
513 if err != nil {
514 if !tc.wantErr {
515 t.Errorf("MatchExpression.Provision() error = %v, wantErr %v", err, tc.wantErr)
516 }
517 return
518 }
519
520 req := httptest.NewRequest(tc.httpMethod, tc.urlTarget, nil)
521 if tc.httpHeader != nil {
522 req.Header = *tc.httpHeader
523 }
524 repl := caddyhttp.NewTestReplacer(req)
525 repl.Set("http.vars.root", "./testdata")
526 ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
527 req = req.WithContext(ctx)
528
529 matches, err := tc.expression.MatchWithError(req)
530 if err != nil {
531 t.Errorf("MatchExpression.Match() error = %v", err)
532 return
533 }
534 if matches != tc.wantResult {
535 t.Errorf("MatchExpression.Match() expected to return '%t', for expression : '%s'", tc.wantResult, tc.expression.Expr)
536 }
537
538 if tc.expectedPath != "" {
539 path, ok := repl.Get("http.matchers.file.relative")
540 if !ok {
541 t.Errorf("MatchExpression.Match() expected to return path '%s', but got none", tc.expectedPath)
542 }
543 if path != tc.expectedPath {
544 t.Errorf("MatchExpression.Match() expected to return path '%s', but got '%s'", tc.expectedPath, path)
545 }
546 }
547 })
548 }
549}

Callers

nothing calls this directly

Calls 6

NewTestReplacerFunction · 0.92
WithValueMethod · 0.80
ProvisionMethod · 0.65
MatchWithErrorMethod · 0.65
GetMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected