github.com/blend/go-sdk@v1.20220411.3/stringutil/replace_path_parameters_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 stringutil
     9  
    10  import (
    11  	"errors"
    12  	"testing"
    13  
    14  	"github.com/blend/go-sdk/assert"
    15  )
    16  
    17  func TestReplacePathParameters(t *testing.T) {
    18  	testCases := []struct {
    19  		name   string
    20  		path   string
    21  		params map[string]string
    22  
    23  		expectRes string
    24  		expectErr error
    25  	}{
    26  		{
    27  			name: "happy case",
    28  			path: "/resource/:resource_id/children/:child_id",
    29  			params: map[string]string{
    30  				"resource_id": "123",
    31  				"child_id":    "456",
    32  			},
    33  
    34  			expectRes: "/resource/123/children/456",
    35  		},
    36  		{
    37  			name: "does not lead with '/'",
    38  			path: "resource/:resource_id/children/:child_id",
    39  			params: map[string]string{
    40  				"resource_id": "123",
    41  				"child_id":    "456",
    42  			},
    43  
    44  			expectRes: "resource/123/children/456",
    45  		},
    46  		{
    47  			name: "params include colon prefix",
    48  			path: "/resource/:resource_id/children/:child_id",
    49  			params: map[string]string{
    50  				":resource_id": "123",
    51  				":child_id":    "456",
    52  			},
    53  
    54  			expectRes: "/resource/123/children/456",
    55  		},
    56  		{
    57  			name: "more params than needed",
    58  			path: "/resource/:resource_id/children/:child_id",
    59  			params: map[string]string{
    60  				":resource_id": "123",
    61  				":child_id":    "456",
    62  				":other_id":    "789",
    63  			},
    64  
    65  			expectRes: "/resource/123/children/456",
    66  		},
    67  		{
    68  			name: "needed params are missing",
    69  			path: "/resource/:resource_id/children/:child_id",
    70  			params: map[string]string{
    71  				":resource_id": "123",
    72  			},
    73  
    74  			expectErr: ErrMissingRouteParameters,
    75  		},
    76  		{
    77  			name: "nil params map",
    78  			path: "/resource/:resource_id/children/:child_id",
    79  
    80  			expectErr: ErrMissingRouteParameters,
    81  		},
    82  	}
    83  
    84  	for _, tc := range testCases {
    85  		t.Run(tc.name, func(t *testing.T) {
    86  			its := assert.New(t)
    87  			res, err := ReplacePathParameters(tc.path, tc.params)
    88  			its.True(errors.Is(err, tc.expectErr), tc.expectErr)
    89  			its.Equal(tc.expectRes, res)
    90  		})
    91  	}
    92  }