github.com/atlassian/git-lob@v0.0.0-20150806085256-2386a5ed291a/providers/smart/ssh_test.go (about) 1 package smart 2 3 import ( 4 "net/url" 5 6 . "github.com/atlassian/git-lob/Godeps/_workspace/src/github.com/onsi/ginkgo" 7 . "github.com/atlassian/git-lob/Godeps/_workspace/src/github.com/onsi/gomega" 8 ) 9 10 var _ = Describe("SSH", func() { 11 12 Context("Low level URL tests", func() { 13 14 factory := &SshTransportFactory{} 15 It("Correctly parses bare URLs", func() { 16 // Make sure we can handle all forms of SSH URL 17 // Bare url with no port and relative path 18 u, err := url.Parse("git@host.com:path/to/repo") 19 Expect(err).To(BeNil(), "Should not be a problem parsing initial URL") 20 Expect(factory.WillHandleUrl(u)).To(BeTrue(), "Should handle URL") 21 // At this point the entire URL will just be in Path 22 // So clean it up 23 u = factory.cleanupBareUrl(u) 24 Expect(u.Scheme).To(Equal("ssh"), "Scheme should be parsed correctly") 25 Expect(u.Host).To(Equal("host.com"), "Host should be parsed correctly") 26 Expect(u.User.Username()).To(Equal("git"), "User should be parsed correctly") 27 Expect(u.Path).To(Equal("/path/to/repo"), "Path should be parsed correctly; path will have '/' added but that doesn't make it rooted") 28 29 host, port := factory.getHostAndPort(u) 30 Expect(host).To(Equal("host.com"), "Host should be extracted correctly") 31 Expect(port).To(Equal(""), "Port should be extracted correctly") 32 33 // Clean bare url with no FQDN & simple repo 34 u, err = url.Parse("git@host:repo") 35 Expect(err).To(BeNil(), "Should not be a problem parsing initial URL") 36 Expect(factory.WillHandleUrl(u)).To(BeTrue(), "Should handle URL") 37 // At this point the entire URL will just be in Path 38 // So clean it up 39 u = factory.cleanupBareUrl(u) 40 Expect(u.Scheme).To(Equal("ssh"), "Scheme should be parsed correctly") 41 Expect(u.Host).To(Equal("host"), "Host should be parsed correctly") 42 Expect(u.User.Username()).To(Equal("git"), "User should be parsed correctly") 43 Expect(u.Path).To(Equal("/repo"), "Path should be parsed correctly; path will have '/' added but that doesn't make it rooted") 44 45 // Bare url with no port and rooted path 46 u, err = url.Parse("git@host.com:/rooted/path/to/repo") 47 Expect(err).To(BeNil(), "Should not be a problem parsing initial URL") 48 Expect(factory.WillHandleUrl(u)).To(BeTrue(), "Should handle URL") 49 // At this point the entire URL will just be in Path 50 // So clean it up 51 u = factory.cleanupBareUrl(u) 52 Expect(u.Scheme).To(Equal("ssh"), "Scheme should be parsed correctly") 53 Expect(u.Host).To(Equal("host.com"), "Host should be parsed correctly") 54 Expect(u.User.Username()).To(Equal("git"), "User should be parsed correctly") 55 Expect(u.Path).To(Equal("//rooted/path/to/repo"), "Path should be parsed correctly; double // makes it rooted") 56 57 // Bare url with custom port 58 u, err = url.Parse("git@host.com:1002:path/to/repo") 59 Expect(err).To(BeNil(), "Should not be a problem parsing initial URL") 60 Expect(factory.WillHandleUrl(u)).To(BeTrue(), "Should handle URL") 61 // At this point the entire URL will just be in Path 62 // So clean it up 63 u = factory.cleanupBareUrl(u) 64 Expect(u.Scheme).To(Equal("ssh"), "Scheme should be parsed correctly") 65 Expect(u.Host).To(Equal("host.com:1002"), "Host should be parsed correctly and include port") 66 Expect(u.User.Username()).To(Equal("git"), "User should be parsed correctly") 67 Expect(u.Path).To(Equal("/path/to/repo"), "Path should be parsed correctly; path will have '/' added but that doesn't make it rooted") 68 69 host, port = factory.getHostAndPort(u) 70 Expect(host).To(Equal("host.com"), "Host should be extracted correctly") 71 Expect(port).To(Equal("1002"), "Host should be extracted correctly") 72 }) 73 It("Correctly parses standard URLs", func() { 74 // Make sure we can handle all forms of SSH URL 75 // Standard url with no port and relative path 76 u, err := url.Parse("ssh://git@host.com/path/to/repo") 77 Expect(err).To(BeNil(), "Should not be a problem parsing initial URL") 78 Expect(factory.WillHandleUrl(u)).To(BeTrue(), "Should handle URL") 79 // At this point the entire URL will just be in Path 80 // So clean it up 81 u = factory.cleanupBareUrl(u) 82 Expect(u.Scheme).To(Equal("ssh"), "Scheme should be parsed correctly") 83 Expect(u.Host).To(Equal("host.com"), "Host should be parsed correctly") 84 Expect(u.User.Username()).To(Equal("git"), "User should be parsed correctly") 85 Expect(u.Path).To(Equal("/path/to/repo"), "Path should be parsed correctly; path will have '/' added but that doesn't make it rooted") 86 87 host, port := factory.getHostAndPort(u) 88 Expect(host).To(Equal("host.com"), "Host should be extracted correctly") 89 Expect(port).To(Equal(""), "Port should be extracted correctly") 90 91 // Standard url with no port and rooted path 92 u, err = url.Parse("ssh://git@host.com//rooted/path/to/repo") 93 Expect(err).To(BeNil(), "Should not be a problem parsing initial URL") 94 Expect(factory.WillHandleUrl(u)).To(BeTrue(), "Should handle URL") 95 // At this point the entire URL will just be in Path 96 // So clean it up 97 u = factory.cleanupBareUrl(u) 98 Expect(u.Scheme).To(Equal("ssh"), "Scheme should be parsed correctly") 99 Expect(u.Host).To(Equal("host.com"), "Host should be parsed correctly") 100 Expect(u.User.Username()).To(Equal("git"), "User should be parsed correctly") 101 Expect(u.Path).To(Equal("//rooted/path/to/repo"), "Path should be parsed correctly; double // makes it rooted") 102 103 // Standard url with custom port 104 u, err = url.Parse("ssh://git@host.com:1002/path/to/repo") 105 Expect(err).To(BeNil(), "Should not be a problem parsing initial URL") 106 Expect(factory.WillHandleUrl(u)).To(BeTrue(), "Should handle URL") 107 // At this point the entire URL will just be in Path 108 // So clean it up 109 u = factory.cleanupBareUrl(u) 110 Expect(u.Scheme).To(Equal("ssh"), "Scheme should be parsed correctly") 111 Expect(u.Host).To(Equal("host.com:1002"), "Host should be parsed correctly and include port") 112 Expect(u.User.Username()).To(Equal("git"), "User should be parsed correctly") 113 Expect(u.Path).To(Equal("/path/to/repo"), "Path should be parsed correctly; path will have '/' added but that doesn't make it rooted") 114 115 host, port = factory.getHostAndPort(u) 116 Expect(host).To(Equal("host.com"), "Host should be extracted correctly") 117 Expect(port).To(Equal("1002"), "Port should be extracted correctly") 118 }) 119 120 }) 121 122 })