github.com/blend/go-sdk@v1.20220411.3/web/rewrite_rule_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package web
     9  
    10  import (
    11  	"fmt"
    12  	"regexp"
    13  	"testing"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  )
    17  
    18  func TestRewriteRuleApply(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	regex := `([0-9]+)\.([a-zA-Z]+)`
    22  	expression := regexp.MustCompile(regex)
    23  
    24  	rr := &RewriteRule{
    25  		MatchExpression: regex,
    26  		expr:            expression,
    27  		Action: func(path string, pieces ...string) string {
    28  			assert.NotEmpty(path)
    29  			assert.NotEmpty(pieces)
    30  			assert.Len(pieces, 3, fmt.Sprintf("%#v", pieces))
    31  			return path + "_ok!"
    32  		},
    33  	}
    34  
    35  	matches, result := rr.Apply("1234.abcde")
    36  	assert.True(matches)
    37  	assert.Equal("1234.abcde_ok!", result)
    38  
    39  	matches, result = rr.Apply("abcde.1234")
    40  	assert.False(matches)
    41  	assert.Equal("abcde.1234", result)
    42  }