code.gitea.io/gitea@v1.19.3/modules/git/url/url_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package url
     5  
     6  import (
     7  	"net/url"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestParseGitURLs(t *testing.T) {
    14  	kases := []struct {
    15  		kase     string
    16  		expected *GitURL
    17  	}{
    18  		{
    19  			kase: "git@127.0.0.1:go-gitea/gitea.git",
    20  			expected: &GitURL{
    21  				URL: &url.URL{
    22  					Scheme: "ssh",
    23  					User:   url.User("git"),
    24  					Host:   "127.0.0.1",
    25  					Path:   "go-gitea/gitea.git",
    26  				},
    27  				extraMark: 1,
    28  			},
    29  		},
    30  		{
    31  			kase: "git@[fe80:14fc:cec5:c174:d88%2510]:go-gitea/gitea.git",
    32  			expected: &GitURL{
    33  				URL: &url.URL{
    34  					Scheme: "ssh",
    35  					User:   url.User("git"),
    36  					Host:   "[fe80:14fc:cec5:c174:d88%10]",
    37  					Path:   "go-gitea/gitea.git",
    38  				},
    39  				extraMark: 1,
    40  			},
    41  		},
    42  		{
    43  			kase: "git@[::1]:go-gitea/gitea.git",
    44  			expected: &GitURL{
    45  				URL: &url.URL{
    46  					Scheme: "ssh",
    47  					User:   url.User("git"),
    48  					Host:   "[::1]",
    49  					Path:   "go-gitea/gitea.git",
    50  				},
    51  				extraMark: 1,
    52  			},
    53  		},
    54  		{
    55  			kase: "git@github.com:go-gitea/gitea.git",
    56  			expected: &GitURL{
    57  				URL: &url.URL{
    58  					Scheme: "ssh",
    59  					User:   url.User("git"),
    60  					Host:   "github.com",
    61  					Path:   "go-gitea/gitea.git",
    62  				},
    63  				extraMark: 1,
    64  			},
    65  		},
    66  		{
    67  			kase: "ssh://git@github.com/go-gitea/gitea.git",
    68  			expected: &GitURL{
    69  				URL: &url.URL{
    70  					Scheme: "ssh",
    71  					User:   url.User("git"),
    72  					Host:   "github.com",
    73  					Path:   "/go-gitea/gitea.git",
    74  				},
    75  				extraMark: 0,
    76  			},
    77  		},
    78  		{
    79  			kase: "ssh://git@[::1]/go-gitea/gitea.git",
    80  			expected: &GitURL{
    81  				URL: &url.URL{
    82  					Scheme: "ssh",
    83  					User:   url.User("git"),
    84  					Host:   "[::1]",
    85  					Path:   "/go-gitea/gitea.git",
    86  				},
    87  				extraMark: 0,
    88  			},
    89  		},
    90  		{
    91  			kase: "/repositories/go-gitea/gitea.git",
    92  			expected: &GitURL{
    93  				URL: &url.URL{
    94  					Scheme: "file",
    95  					Path:   "/repositories/go-gitea/gitea.git",
    96  				},
    97  				extraMark: 2,
    98  			},
    99  		},
   100  		{
   101  			kase: "file:///repositories/go-gitea/gitea.git",
   102  			expected: &GitURL{
   103  				URL: &url.URL{
   104  					Scheme: "file",
   105  					Path:   "/repositories/go-gitea/gitea.git",
   106  				},
   107  				extraMark: 0,
   108  			},
   109  		},
   110  		{
   111  			kase: "https://github.com/go-gitea/gitea.git",
   112  			expected: &GitURL{
   113  				URL: &url.URL{
   114  					Scheme: "https",
   115  					Host:   "github.com",
   116  					Path:   "/go-gitea/gitea.git",
   117  				},
   118  				extraMark: 0,
   119  			},
   120  		},
   121  		{
   122  			kase: "https://git:git@github.com/go-gitea/gitea.git",
   123  			expected: &GitURL{
   124  				URL: &url.URL{
   125  					Scheme: "https",
   126  					Host:   "github.com",
   127  					User:   url.UserPassword("git", "git"),
   128  					Path:   "/go-gitea/gitea.git",
   129  				},
   130  				extraMark: 0,
   131  			},
   132  		},
   133  		{
   134  			kase: "https://[fe80:14fc:cec5:c174:d88%2510]:20/go-gitea/gitea.git",
   135  			expected: &GitURL{
   136  				URL: &url.URL{
   137  					Scheme: "https",
   138  					Host:   "[fe80:14fc:cec5:c174:d88%10]:20",
   139  					Path:   "/go-gitea/gitea.git",
   140  				},
   141  				extraMark: 0,
   142  			},
   143  		},
   144  
   145  		{
   146  			kase: "git://github.com/go-gitea/gitea.git",
   147  			expected: &GitURL{
   148  				URL: &url.URL{
   149  					Scheme: "git",
   150  					Host:   "github.com",
   151  					Path:   "/go-gitea/gitea.git",
   152  				},
   153  				extraMark: 0,
   154  			},
   155  		},
   156  	}
   157  
   158  	for _, kase := range kases {
   159  		t.Run(kase.kase, func(t *testing.T) {
   160  			u, err := Parse(kase.kase)
   161  			assert.NoError(t, err)
   162  			assert.EqualValues(t, kase.expected.extraMark, u.extraMark)
   163  			assert.EqualValues(t, *kase.expected, *u)
   164  		})
   165  	}
   166  }