github.com/iwataka/ghq@v0.7.5-0.20160611155400-0aa07ac077a9/url_test.go (about)

     1  package main
     2  
     3  import (
     4  	. "github.com/onsi/gomega"
     5  	"net/url"
     6  	"testing"
     7  )
     8  
     9  func TestNewURL(t *testing.T) {
    10  	RegisterTestingT(t)
    11  
    12  	// Does nothing whent the URL has scheme part
    13  	httpsUrl, err := NewURL("https://github.com/motemen/pusheen-explorer")
    14  	Expect(httpsUrl.String()).To(Equal("https://github.com/motemen/pusheen-explorer"))
    15  	Expect(httpsUrl.Host).To(Equal("github.com"))
    16  	Expect(err).To(BeNil())
    17  
    18  	// Convert SCP-like URL to SSH URL
    19  	scpUrl, err := NewURL("git@github.com:motemen/pusheen-explorer.git")
    20  	Expect(scpUrl.String()).To(Equal("ssh://git@github.com/motemen/pusheen-explorer.git"))
    21  	Expect(scpUrl.Host).To(Equal("github.com"))
    22  	Expect(err).To(BeNil())
    23  
    24  	scpUrlWithRoot, err := NewURL("git@github.com:/motemen/pusheen-explorer.git")
    25  	Expect(scpUrlWithRoot.String()).To(Equal("ssh://git@github.com/motemen/pusheen-explorer.git"))
    26  	Expect(scpUrlWithRoot.Host).To(Equal("github.com"))
    27  	Expect(err).To(BeNil())
    28  
    29  	scpUrlWithoutUser, err := NewURL("github.com:motemen/pusheen-explorer.git")
    30  	Expect(scpUrlWithoutUser.String()).To(Equal("ssh://github.com/motemen/pusheen-explorer.git"))
    31  	Expect(scpUrlWithoutUser.Host).To(Equal("github.com"))
    32  	Expect(err).To(BeNil())
    33  
    34  	differentNameRepository, err := NewURL("motemen/ghq")
    35  	Expect(differentNameRepository.String()).To(Equal("https://github.com/motemen/ghq"))
    36  	Expect(differentNameRepository.Host).To(Equal("github.com"))
    37  	Expect(err).To(BeNil())
    38  
    39  	sameNameRepository, err := NewURL("same-name-ghq")
    40  	Expect(sameNameRepository.String()).To(Equal("https://github.com/same-name-ghq/same-name-ghq"))
    41  	Expect(sameNameRepository.Host).To(Equal("github.com"))
    42  	Expect(err).To(BeNil())
    43  }
    44  
    45  func TestConvertGitURLHTTPToSSH(t *testing.T) {
    46  	RegisterTestingT(t)
    47  
    48  	var (
    49  		httpsURL, sshURL *url.URL
    50  		err              error
    51  	)
    52  
    53  	httpsURL, err = NewURL("https://github.com/motemen/pusheen-explorer")
    54  	sshURL, err = ConvertGitURLHTTPToSSH(httpsURL)
    55  	Expect(err).To(BeNil())
    56  	Expect(sshURL.String()).To(Equal("ssh://git@github.com/motemen/pusheen-explorer"))
    57  
    58  	httpsURL, err = NewURL("https://ghe.example.com/motemen/pusheen-explorer")
    59  	sshURL, err = ConvertGitURLHTTPToSSH(httpsURL)
    60  	Expect(err).To(BeNil())
    61  	Expect(sshURL.String()).To(Equal("ssh://git@ghe.example.com/motemen/pusheen-explorer"))
    62  }