Browse code

registry/endpoint: make it testable

Signed-off-by: Vincent Batts <vbatts@redhat.com>

Vincent Batts authored on 2014/10/04 04:46:42
Showing 2 changed files
... ...
@@ -35,16 +35,7 @@ func scanForAPIVersion(hostname string) (string, APIVersion) {
35 35
 }
36 36
 
37 37
 func NewEndpoint(hostname string) (*Endpoint, error) {
38
-	var (
39
-		endpoint        Endpoint
40
-		trimmedHostname string
41
-		err             error
42
-	)
43
-	if !strings.HasPrefix(hostname, "http") {
44
-		hostname = "https://" + hostname
45
-	}
46
-	trimmedHostname, endpoint.Version = scanForAPIVersion(hostname)
47
-	endpoint.URL, err = url.Parse(trimmedHostname)
38
+	endpoint, err := newEndpoint(hostname)
48 39
 	if err != nil {
49 40
 		return nil, err
50 41
 	}
... ...
@@ -59,6 +50,22 @@ func NewEndpoint(hostname string) (*Endpoint, error) {
59 59
 		}
60 60
 	}
61 61
 
62
+	return endpoint, nil
63
+}
64
+func newEndpoint(hostname string) (*Endpoint, error) {
65
+	var (
66
+		endpoint        Endpoint
67
+		trimmedHostname string
68
+		err             error
69
+	)
70
+	if !strings.HasPrefix(hostname, "http") {
71
+		hostname = "https://" + hostname
72
+	}
73
+	trimmedHostname, endpoint.Version = scanForAPIVersion(hostname)
74
+	endpoint.URL, err = url.Parse(trimmedHostname)
75
+	if err != nil {
76
+		return nil, err
77
+	}
62 78
 	return &endpoint, nil
63 79
 }
64 80
 
65 81
new file mode 100644
... ...
@@ -0,0 +1,27 @@
0
+package registry
1
+
2
+import "testing"
3
+
4
+func TestEndpointParse(t *testing.T) {
5
+	testData := []struct {
6
+		str      string
7
+		expected string
8
+	}{
9
+		{IndexServerAddress(), IndexServerAddress()},
10
+		{"http://0.0.0.0:5000", "http://0.0.0.0:5000/v1/"},
11
+		{"0.0.0.0:5000", "https://0.0.0.0:5000/v1/"},
12
+	}
13
+	for _, td := range testData {
14
+		e, err := newEndpoint(td.str)
15
+		if err != nil {
16
+			t.Errorf("%q: %s", td.str, err)
17
+		}
18
+		if e == nil {
19
+			t.Logf("something's fishy, endpoint for %q is nil", td.str)
20
+			continue
21
+		}
22
+		if e.String() != td.expected {
23
+			t.Errorf("expected %q, got %q", td.expected, e.String())
24
+		}
25
+	}
26
+}