-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
160 lines (149 loc) · 3.36 KB
/
server_test.go
File metadata and controls
160 lines (149 loc) · 3.36 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Test manager http server
package ma
import (
"code.google.com/p/go.net/websocket"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"sync"
"testing"
"time"
urllib "net/url"
)
var (
url = fmt.Sprintf("wss://localhost:%d%s", DefaultPort, RemoteUrlPath)
once sync.Once
wg sync.WaitGroup
)
func startServer() {
_, s := NewServer(&ServerConfig{})
go s.Start()
time.Sleep(100 * time.Millisecond)
}
func testWebsocketAllowed() error {
if !AllowedIPs["127.0.0.1"] {
return errors.New("127.0.0.1 is not in AllowedIPs")
}
ws, err := WebsocketDial(url)
if err != nil {
return err
}
var msg string
err = websocket.Message.Receive(ws, &msg)
if err != nil {
return err
}
if msg != OK {
return errors.New("did't get ok from server")
}
return nil
}
func TestWebsocketAllowed(t *testing.T) {
once.Do(startServer)
AllowedIPs["127.0.0.1"] = true
for i := 0; i < 3; i++ {
wg.Add(1)
go func() {
err := testWebsocketAllowed()
if err != nil {
t.Error(err)
}
wg.Done()
}()
}
wg.Wait()
}
func TestWebsocketNotAllowed(t *testing.T) {
once.Do(startServer)
AllowedIPs["127.0.0.1"] = false
ws, err := WebsocketDial(url)
if err != nil {
t.Error(err)
}
var msg string
err = websocket.Message.Receive(ws, &msg)
if msg == OK {
t.Error("got ok from server, but should not")
}
}
func testConfigHttpGet(accept_header, config_data string) error {
client := NewHttpClient()
url := server.GetBaseUrl() + "/config"
_, _ = NewClusterConfig(config_data)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Header.Add("Accept", "application/"+accept_header)
resp, err := client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(fmt.Sprintf("GET /config %s", resp.Status))
}
content_type := resp.Header["Content-Type"][0]
if !strings.Contains(content_type, "application/"+accept_header) {
return errors.New(fmt.Sprintf(
"expect application/%s from GET /config, got %s",
accept_header, content_type))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
config, err := NewClusterConfig(string(body))
if err != nil {
return err
}
if len(config.Hosts) != 1 || config.Hosts[0].RemoteIp != "127.0.0.1" {
return errors.New(
fmt.Sprintf("got wrong response from /config `%s` for %s",
body, accept_header))
}
return nil
}
func TestConfigHttpGet(t *testing.T) {
once.Do(startServer)
m := make(map[string]string)
m["json"] = JSON_CONFIG
m["yaml"] = YAML_CONFIG
for key, value := range m {
err := testConfigHttpGet(key, value)
if err != nil {
t.Error(err)
}
}
}
func TestConfigHttpPost(t *testing.T) {
once.Do(startServer)
client := NewHttpClient()
url := server.GetBaseUrl() + "/config"
req, err := http.NewRequest("POST", url, strings.NewReader(JSON_CONFIG))
if err != nil {
t.Error(err)
return
}
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
t.Error(err)
}
if resp.StatusCode != 200 {
t.Errorf("Could not POST config in reqeust body, got status %d",
resp.StatusCode)
}
resp, err = client.PostForm(url, urllib.Values{"config": {YAML_CONFIG}})
if err != nil {
t.Error(err)
}
if resp.StatusCode != 200 {
t.Errorf("Could not POST config in config POST parameter, status %d",
resp.StatusCode)
}
if clusterConfig.Name != "TestCluster" {
t.Error("cluster config is not updated")
}
}