github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/cli/connhelper/ssh/ssh_test.go (about)

     1  package ssh
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/assert"
     7  	is "gotest.tools/v3/assert/cmp"
     8  )
     9  
    10  func TestParseURL(t *testing.T) {
    11  	testCases := []struct {
    12  		url           string
    13  		expectedArgs  []string
    14  		expectedError string
    15  	}{
    16  		{
    17  			url: "ssh://foo",
    18  			expectedArgs: []string{
    19  				"--", "foo",
    20  			},
    21  		},
    22  		{
    23  			url: "ssh://me@foo:10022",
    24  			expectedArgs: []string{
    25  				"-l", "me",
    26  				"-p", "10022",
    27  				"--", "foo",
    28  			},
    29  		},
    30  		{
    31  			url:           "ssh://me:passw0rd@foo",
    32  			expectedError: "plain-text password is not supported",
    33  		},
    34  		{
    35  			url: "ssh://foo/bar",
    36  			expectedArgs: []string{
    37  				"--", "foo",
    38  			},
    39  		},
    40  		{
    41  			url:           "ssh://foo?bar",
    42  			expectedError: `extra query after the host: "bar"`,
    43  		},
    44  		{
    45  			url:           "ssh://foo#bar",
    46  			expectedError: `extra fragment after the host: "bar"`,
    47  		},
    48  		{
    49  			url:           "ssh://",
    50  			expectedError: "no host specified",
    51  		},
    52  		{
    53  			url:           "foo://bar",
    54  			expectedError: `expected scheme ssh, got "foo"`,
    55  		},
    56  	}
    57  	for _, tc := range testCases {
    58  		t.Run(tc.url, func(t *testing.T) {
    59  			sp, err := ParseURL(tc.url)
    60  			if tc.expectedError == "" {
    61  				assert.NilError(t, err)
    62  				assert.Check(t, is.DeepEqual(tc.expectedArgs, sp.Args()))
    63  			} else {
    64  				assert.ErrorContains(t, err, tc.expectedError)
    65  			}
    66  		})
    67  	}
    68  }