github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/curl/curl_test.go (about)

     1  package curl
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestFindNextArg(t *testing.T) {
     8  	command := &CurlCommand{}
     9  	args := [][]string{
    10  		{"-X", "GET", "arg1", "--foo", "bar"},
    11  		{"-X", "GET", "--server-idea", "foo", "/api/arg2"},
    12  		{"-XGET", "--foo", "bar", "--foo-bar", "meow", "arg3"},
    13  	}
    14  
    15  	expected := []struct {
    16  		int
    17  		string
    18  	}{
    19  		{2, "arg1"},
    20  		{4, "/api/arg2"},
    21  		{5, "arg3"},
    22  	}
    23  
    24  	for index, test := range args {
    25  		command.arguments = test
    26  		actualArgIndex, actualArg := command.findUriValueAndIndex()
    27  
    28  		if actualArgIndex != expected[index].int {
    29  			t.Errorf("Expected arg index of: %d, got: %d.", expected[index].int, actualArgIndex)
    30  		}
    31  		if actualArg != expected[index].string {
    32  			t.Errorf("Expected arg index of: %s, got: %s.", expected[index].string, actualArg)
    33  		}
    34  	}
    35  }
    36  
    37  func TestIsCredsFlagExists(t *testing.T) {
    38  	command := &CurlCommand{}
    39  	args := [][]string{
    40  		{"-X", "GET", "arg1", "--foo", "bar", "-uaaa:ppp"},
    41  		{"-X", "GET", "--server-idea", "foo", "-u", "aaa:ppp", "/api/arg2"},
    42  		{"-XGET", "--foo", "bar", "--foo-bar", "--user", "meow", "-Ttest"},
    43  		{"-XGET", "--foo", "bar", "--foo-bar", "-Ttest"},
    44  	}
    45  
    46  	expected := []bool{
    47  		true,
    48  		true,
    49  		true,
    50  		false,
    51  	}
    52  
    53  	for index, test := range args {
    54  		command.arguments = test
    55  		flagExists := command.isCredentialsFlagExists()
    56  
    57  		if flagExists != expected[index] {
    58  			t.Errorf("Expected flag existstence to be: %t, got: %t.", expected[index], flagExists)
    59  		}
    60  	}
    61  }
    62  
    63  func TestBuildCommandUrl(t *testing.T) {
    64  	tests := []struct {
    65  		name      string
    66  		arguments []string
    67  		uriIndex  int
    68  		uriValue  string
    69  		expectErr bool
    70  	}{
    71  		{"test1", []string{"-X", "GET", "/api/build/test1", "--server-id", "test1", "--foo", "bar"}, 2, "http://artifactory:8081/artifactory/api/build/test1", false},
    72  		{"test2", []string{"-X", "GET", "/api/build/test2", "--server-idea", "foo", "--server-id=test2"}, 2, "http://artifactory:8081/artifactory/api/build/test2", false},
    73  		{"test3", []string{"-XGET", "--/api/build/test3", "--server-id="}, 1, "http://artifactory:8081/artifactory/api/build/test3", true},
    74  		{"test4", []string{"-XGET", "-Test4", "--server-id", "bar"}, 1, "http://artifactory:8081/artifactory/api/build/test4", true},
    75  		{"test5", []string{"-X", "GET", "api/build/test5", "--server-id", "test5", "--foo", "bar"}, 2, "http://artifactory:8081/artifactory/api/build/test5", false},
    76  	}
    77  
    78  	command := &CurlCommand{}
    79  	urlPrefix := "http://artifactory:8081/artifactory/"
    80  	for _, test := range tests {
    81  		command.arguments = test.arguments
    82  		t.Run(test.name, func(t *testing.T) {
    83  			uriIndex, uriValue, err := command.buildCommandUrl(urlPrefix)
    84  
    85  			// Check errors.
    86  			if err != nil && !test.expectErr {
    87  				t.Error(err)
    88  			}
    89  			if err == nil && test.expectErr {
    90  				t.Errorf("Expecting: error, Got: nil")
    91  			}
    92  
    93  			if err == nil {
    94  				// Validate results.
    95  				if uriValue != test.uriValue {
    96  					t.Errorf("Expected uri value of: %s, got: %s.", test.uriValue, uriValue)
    97  				}
    98  				if uriIndex != test.uriIndex {
    99  					t.Errorf("Expected uri index of: %d, got: %d.", test.uriIndex, uriIndex)
   100  				}
   101  			}
   102  		})
   103  	}
   104  }