forked from maxcnunes/waitforit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_test.go
87 lines (82 loc) · 2.41 KB
/
connection_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main_test
import (
"reflect"
"testing"
. "github.com/maxcnunes/waitforit"
)
func TestBuildConn(t *testing.T) {
type input struct {
host string
port int
fullConn string
}
testCases := []struct {
title string
data input
expected *Connection
}{
{
"Should create a default connection when only host and port are given",
input{host: "localhost", port: 80},
&Connection{Type: "tcp", Scheme: "", Port: 80, Host: "localhost", Path: ""},
},
{
"Should be able to create a connection with different host",
input{host: "localhost", port: 80},
&Connection{Type: "tcp", Scheme: "", Port: 80, Host: "localhost", Path: ""},
},
{
"Should be able to create a connection with different port",
input{host: "localhost", port: 90},
&Connection{Type: "tcp", Scheme: "", Port: 90, Host: "localhost", Path: ""},
},
{
"Should ignore the fullConn when the host is given",
input{host: "localhost", port: 90, fullConn: "tcp://remotehost:10"},
&Connection{Type: "tcp", Scheme: "", Port: 90, Host: "localhost", Path: ""},
},
{
"Should be able to craete a connection given a fullConn",
input{fullConn: "tcp://remotehost:10"},
&Connection{Type: "tcp", Scheme: "", Port: 10, Host: "remotehost", Path: ""},
},
{
"Should be able to create a http connection through the fullConn",
input{fullConn: "http://localhost"},
&Connection{Type: "tcp", Scheme: "http", Port: 80, Host: "localhost", Path: ""},
},
{
"Should be able to create a https connection through the fullConn",
input{fullConn: "https://localhost"},
&Connection{Type: "tcp", Scheme: "https", Port: 443, Host: "localhost", Path: ""},
},
{
"Should be able to create a http connection with a path through the fullConn",
input{fullConn: "https://localhost/cars"},
&Connection{Type: "tcp", Scheme: "https", Port: 443, Host: "localhost", Path: "/cars"},
},
{
"Should fail when host and full connection are not provided",
input{},
nil,
},
{
"Should fail when full connection is not a valid address format",
input{fullConn: ":/localhost;80"},
nil,
},
}
for _, v := range testCases {
cfg := &Config{
Host: v.data.host,
Port: v.data.port,
Address: v.data.fullConn,
}
conn := BuildConn(cfg)
t.Run(v.title, func(t *testing.T) {
if !reflect.DeepEqual(conn, v.expected) {
t.Errorf("Expected to %#v to be deep equal %#v", conn, v.expected)
}
})
}
}