Просмотр исходного кода

lib/connections: Fix port fixup in Go 1.8 (fixes #3817)

The test for the error string is fragile, and the error string changed
in Go 1.8 so the relevant part is no longer a prefix. This covers it
with a test though, so it should be fine in the future as well.

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3818
Jakob Borg 9 лет назад
Родитель
Сommit
9c67bd2550
3 измененных файлов с 27 добавлено и 11 удалено
  1. 26 0
      lib/connections/connections_test.go
  2. 0 10
      lib/connections/empty_test.go
  3. 1 1
      lib/connections/tcp_listen.go

+ 26 - 0
lib/connections/connections_test.go

@@ -0,0 +1,26 @@
+// Copyright (C) 2016 The Syncthing Authors.
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this file,
+// You can obtain one at http://mozilla.org/MPL/2.0/.
+
+package connections
+
+import "testing"
+import "net/url"
+
+func TestFixupPort(t *testing.T) {
+	cases := [][2]string{
+		{"tcp://1.2.3.4:5", "tcp://1.2.3.4:5"},
+		{"tcp://1.2.3.4:", "tcp://1.2.3.4:22000"},
+		{"tcp://1.2.3.4", "tcp://1.2.3.4:22000"},
+	}
+
+	for _, tc := range cases {
+		u0, _ := url.Parse(tc[0])
+		u1 := fixupPort(u0).String()
+		if u1 != tc[1] {
+			t.Errorf("fixupPort(%q) => %q, expected %q", tc[0], u1, tc[1])
+		}
+	}
+}

+ 0 - 10
lib/connections/empty_test.go

@@ -1,10 +0,0 @@
-// Copyright (C) 2016 The Syncthing Authors.
-//
-// This Source Code Form is subject to the terms of the Mozilla Public
-// License, v. 2.0. If a copy of the MPL was not distributed with this file,
-// You can obtain one at http://mozilla.org/MPL/2.0/.
-
-// The existence of this file means we get 0% test coverage rather than no
-// test coverage at all. Remove when implementing an actual test.
-
-package connections

+ 1 - 1
lib/connections/tcp_listen.go

@@ -192,7 +192,7 @@ func fixupPort(uri *url.URL) *url.URL {
 	copyURI := *uri
 
 	host, port, err := net.SplitHostPort(uri.Host)
-	if err != nil && strings.HasPrefix(err.Error(), "missing port") {
+	if err != nil && strings.Contains(err.Error(), "missing port") {
 		// addr is on the form "1.2.3.4"
 		copyURI.Host = net.JoinHostPort(uri.Host, "22000")
 	} else if err == nil && port == "" {