forked from cyberark/secretless-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_test.go
73 lines (59 loc) · 1.56 KB
/
manager_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
package main
import (
"io/ioutil"
"log"
"net"
"os"
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestManager(t *testing.T) {
host := os.Getenv("SECRETLESS_HOST")
if host == "" {
host = "localhost"
}
passingListener := host + ":6175"
rejectingListener := host + ":6176"
log.Printf("Trying to use host: %s", host)
request := "GET / HTTP/1.1\r\n" +
"Host: localhost:6174\r\n" +
"User-Agent: UserAgentString\r\n" +
"Accept: */*\r\n\r\n"
fetchPage := func(address string) ([]string, error) {
log.Printf("Resolving...")
tcpAddr, err := net.ResolveTCPAddr("tcp4", address)
if err != nil {
return nil, err
}
log.Printf("Connecting...")
connection, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
return nil, err
}
defer connection.Close()
log.Printf("Writing...")
_, err = connection.Write([]byte(request))
if err != nil {
return nil, err
}
log.Printf("Reading...")
rawContents, err := ioutil.ReadAll(connection)
if err != nil {
return nil, err
}
pageContents := string(rawContents)
return strings.Split(pageContents, "\r\n"), nil
}
Convey("Can manage connections", t, func() {
lines, err := fetchPage(passingListener)
So(err, ShouldBeNil)
So(lines, ShouldContain, "Example-Header: IsSet")
log.Printf("Done...")
// Errors are hard to detect since events are async so
// we just make sure that we don't get a full response back
lines, _ = fetchPage(rejectingListener)
So(lines, ShouldNotContain, "Example-Header: IsSet")
So(len(lines), ShouldBeLessThan, 2)
})
}