github.com/outbrain/consul@v1.4.5/agent/connect/uri_test.go (about)

     1  package connect
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  // testCertURICases contains the test cases for parsing and encoding
    10  // the SPIFFE IDs. This is a global since it is used in multiple test functions.
    11  var testCertURICases = []struct {
    12  	Name       string
    13  	URI        string
    14  	Struct     interface{}
    15  	ParseError string
    16  }{
    17  	{
    18  		"invalid scheme",
    19  		"http://google.com/",
    20  		nil,
    21  		"scheme",
    22  	},
    23  
    24  	{
    25  		"basic service ID",
    26  		"spiffe://1234.consul/ns/default/dc/dc01/svc/web",
    27  		&SpiffeIDService{
    28  			Host:       "1234.consul",
    29  			Namespace:  "default",
    30  			Datacenter: "dc01",
    31  			Service:    "web",
    32  		},
    33  		"",
    34  	},
    35  
    36  	{
    37  		"service with URL-encoded values",
    38  		"spiffe://1234.consul/ns/foo%2Fbar/dc/bar%2Fbaz/svc/baz%2Fqux",
    39  		&SpiffeIDService{
    40  			Host:       "1234.consul",
    41  			Namespace:  "foo/bar",
    42  			Datacenter: "bar/baz",
    43  			Service:    "baz/qux",
    44  		},
    45  		"",
    46  	},
    47  
    48  	{
    49  		"signing ID",
    50  		"spiffe://1234.consul",
    51  		&SpiffeIDSigning{
    52  			ClusterID: "1234",
    53  			Domain:    "consul",
    54  		},
    55  		"",
    56  	},
    57  }
    58  
    59  func TestParseCertURIFromString(t *testing.T) {
    60  	for _, tc := range testCertURICases {
    61  		t.Run(tc.Name, func(t *testing.T) {
    62  			assert := assert.New(t)
    63  
    64  			// Parse the ID and check the error/return value
    65  			actual, err := ParseCertURIFromString(tc.URI)
    66  			if err != nil {
    67  				t.Logf("parse error: %s", err.Error())
    68  			}
    69  			assert.Equal(tc.ParseError != "", err != nil, "error value")
    70  			if err != nil {
    71  				assert.Contains(err.Error(), tc.ParseError)
    72  				return
    73  			}
    74  			assert.Equal(tc.Struct, actual)
    75  		})
    76  	}
    77  }