aboutsummaryrefslogtreecommitdiff
path: root/server_test.go
blob: 8ef9a5a43cfd2824cdceb8b22b8189065070804f (plain)
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
package goxy

import (
	"io"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"net/rpc"
	"testing"
)

const cannary = "hello from backend"

func TestNewServer(t *testing.T) {
	backendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		io.WriteString(w, cannary)
	}))
	defer backendServer.Close()

	server, err := NewServer("data.gob")
	if err != nil {
		t.Error(err)
	}

	err = server.Start()
	if err != nil {
		t.Error(err)
	}

	client, err := rpc.DialHTTP("tcp", ":http-alt")
	if err != nil {
		t.Error(err)
	}
	defer client.Close()

	e := Entry{
		ServerName: "localhost",
		Upstream:   backendServer.URL,
	}
	client.Call("GoXY.Add", e, nil)

	resp, err := http.Get("http://localhost")
	if err != nil {
		t.Error(err)
	}
	defer resp.Body.Close()

	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Error(err)
	}

	if string(b) != cannary {
		t.Error("got", string(b), "expected", cannary)
	}
}