github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/url/format_test.go (about) 1 package url 2 3 import ( 4 "testing" 5 ) 6 7 type formatTestCase struct { 8 url *URL 9 environmentPrefix string 10 expected string 11 } 12 13 func (c *formatTestCase) run(t *testing.T) { 14 formatted := c.url.Format(c.environmentPrefix) 15 if formatted != c.expected { 16 t.Fatal("formatting mismatch:", formatted, "!=", c.expected) 17 } 18 } 19 20 func TestFormatInvalidProtocol(t *testing.T) { 21 defer func() { 22 if r := recover(); r == nil { 23 t.Error("formatting invalid protocol did not panic") 24 } 25 }() 26 url := &URL{Protocol: Protocol(-1)} 27 url.Format("") 28 } 29 30 func TestFormatLocal(t *testing.T) { 31 test := &formatTestCase{ 32 url: &URL{ 33 Protocol: Protocol_Local, 34 Path: "/test/path", 35 }, 36 expected: "/test/path", 37 } 38 test.run(t) 39 } 40 41 func TestFormatForwardingLocal(t *testing.T) { 42 test := &formatTestCase{ 43 url: &URL{ 44 Kind: Kind_Forwarding, 45 Protocol: Protocol_Local, 46 Path: "/test/path", 47 }, 48 expected: "/test/path", 49 } 50 test.run(t) 51 } 52 53 func TestFormatSSHHostnamePath(t *testing.T) { 54 test := &formatTestCase{ 55 url: &URL{ 56 Protocol: Protocol_SSH, 57 User: "", 58 Host: "host", 59 Port: 0, 60 Path: "/test/path", 61 }, 62 expected: "host:/test/path", 63 } 64 test.run(t) 65 } 66 67 func TestFormatForwardingSSHHostnamePath(t *testing.T) { 68 test := &formatTestCase{ 69 url: &URL{ 70 Protocol: Protocol_SSH, 71 User: "", 72 Host: "host", 73 Port: 0, 74 Path: "tcp:localhost:6060", 75 }, 76 expected: "host:tcp:localhost:6060", 77 } 78 test.run(t) 79 } 80 81 func TestFormatSSHUsernameHostnamePath(t *testing.T) { 82 test := &formatTestCase{ 83 url: &URL{ 84 Protocol: Protocol_SSH, 85 User: "user", 86 Host: "host", 87 Port: 0, 88 Path: "/test/path", 89 }, 90 expected: "user@host:/test/path", 91 } 92 test.run(t) 93 } 94 95 func TestFormatSSHHostnamePortPath(t *testing.T) { 96 test := &formatTestCase{ 97 url: &URL{ 98 Protocol: Protocol_SSH, 99 User: "", 100 Host: "host", 101 Port: 23, 102 Path: "/test/path", 103 }, 104 expected: "host:23:/test/path", 105 } 106 test.run(t) 107 } 108 109 func TestFormatSSHUsernameHostnamePortPath(t *testing.T) { 110 test := &formatTestCase{ 111 url: &URL{ 112 Protocol: Protocol_SSH, 113 User: "user", 114 Host: "host", 115 Port: 23, 116 Path: "/test/path", 117 }, 118 expected: "user@host:23:/test/path", 119 } 120 test.run(t) 121 } 122 123 func TestFormatDockerInvalidEmptyPath(t *testing.T) { 124 test := &formatTestCase{ 125 url: &URL{ 126 Protocol: Protocol_Docker, 127 Host: "container", 128 Path: "", 129 }, 130 expected: invalidDockerURLFormat, 131 } 132 test.run(t) 133 } 134 135 func TestFormatDockerInvalidBadFirstPathCharacter(t *testing.T) { 136 test := &formatTestCase{ 137 url: &URL{ 138 Protocol: Protocol_Docker, 139 Host: "container", 140 Path: "$5", 141 }, 142 expected: invalidDockerURLFormat, 143 } 144 test.run(t) 145 } 146 147 func TestFormatDocker(t *testing.T) { 148 test := &formatTestCase{ 149 url: &URL{ 150 Protocol: Protocol_Docker, 151 Host: "container", 152 Path: "/test/path/to/the file", 153 Environment: map[string]string{ 154 "DOCKER_HOST": "unix:///path/to/docker.sock", 155 }, 156 }, 157 environmentPrefix: "|", 158 expected: "docker://container/test/path/to/the file|DOCKER_HOST=unix:///path/to/docker.sock", 159 } 160 test.run(t) 161 } 162 163 func TestFormatForwardingDocker(t *testing.T) { 164 test := &formatTestCase{ 165 url: &URL{ 166 Kind: Kind_Forwarding, 167 Protocol: Protocol_Docker, 168 Host: "container", 169 Path: "tcp4:localhost:8080", 170 Environment: map[string]string{ 171 "DOCKER_HOST": "unix:///path/to/docker.sock", 172 }, 173 }, 174 environmentPrefix: "|", 175 expected: "docker://container:tcp4:localhost:8080|DOCKER_HOST=unix:///path/to/docker.sock", 176 } 177 test.run(t) 178 } 179 180 func TestFormatDockerWithUsernameAndHomeRelativePath(t *testing.T) { 181 test := &formatTestCase{ 182 url: &URL{ 183 Protocol: Protocol_Docker, 184 User: "user", 185 Host: "container", 186 Path: "~/test/path/to/the file", 187 Environment: map[string]string{ 188 "DOCKER_HOST": "unix:///path/to/docker.sock", 189 "DOCKER_TLS_VERIFY": "true", 190 }, 191 }, 192 environmentPrefix: "|", 193 expected: "docker://user@container/~/test/path/to/the file|DOCKER_HOST=unix:///path/to/docker.sock|DOCKER_TLS_VERIFY=true", 194 } 195 test.run(t) 196 } 197 198 func TestFormatDockerWithUsernameAndUserRelativePath(t *testing.T) { 199 test := &formatTestCase{ 200 url: &URL{ 201 Protocol: Protocol_Docker, 202 User: "user", 203 Host: "container", 204 Path: "~otheruser/test/path/to/the file", 205 Environment: map[string]string{ 206 "DOCKER_HOST": "unix:///path/to/docker.sock", 207 "DOCKER_TLS_VERIFY": "true", 208 }, 209 }, 210 environmentPrefix: "|", 211 expected: "docker://user@container/~otheruser/test/path/to/the file|DOCKER_HOST=unix:///path/to/docker.sock|DOCKER_TLS_VERIFY=true", 212 } 213 test.run(t) 214 } 215 216 func TestFormatDockerWithWindowsPathPath(t *testing.T) { 217 test := &formatTestCase{ 218 url: &URL{ 219 Protocol: Protocol_Docker, 220 Host: "container", 221 Path: `C:\A\Windows\File Path `, 222 Environment: map[string]string{ 223 "DOCKER_HOST": "unix:///path/to/docker.sock", 224 "DOCKER_TLS_VERIFY": "true", 225 }, 226 }, 227 environmentPrefix: "|", 228 expected: `docker://container/C:\A\Windows\File Path |DOCKER_HOST=unix:///path/to/docker.sock|DOCKER_TLS_VERIFY=true`, 229 } 230 test.run(t) 231 }