This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
http_server_test.go
89 lines (71 loc) · 2.03 KB
/
http_server_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
88
89
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"testing"
)
func TestHttpServerListener(t *testing.T) {
graph := NewGraph("fake title", GCVIS_TMPL)
server := NewHttpServer("127.0.0.1", "0", &graph)
url := server.Url()
if !strings.Contains(url, "http://127.0.0.1") {
t.Fatalf("Server URL didn't contain localhost address: %v", url)
}
}
func TestHttpServerResponse(t *testing.T) {
graph := NewGraph("fake title", GCVIS_TMPL)
graph.AddGCTraceGraphPoint(&gctrace{})
server := NewHttpServer("127.0.0.1", "0", &graph)
go server.Start()
defer server.Close()
response, err := http.Get(server.Url())
if err != nil {
t.Errorf("HTTP request returned an error: %v", err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Errorf("Error while reading response body: %v", err)
}
w := &bytes.Buffer{}
if err = graph.Write(w); err != nil {
t.Errorf("Error while writing template: %v", err)
}
expectedBody, err := ioutil.ReadAll(w)
if err != nil {
t.Errorf("Error while reading buffer: %v", err)
}
if !bytes.Equal(expectedBody, body) {
t.Fatalf(
"Expected response body to equal parsed template.\nExpected: %v\nGot: %v",
string(expectedBody),
string(body),
)
}
}
func TestHttpServerJsonEndpoint(t *testing.T) {
graph := NewGraph("fake title", GCVIS_TMPL)
graph.AddGCTraceGraphPoint(&gctrace{Heap1: 10})
server := NewHttpServer("127.0.0.1", "0", &graph)
go server.Start()
defer server.Close()
response, err := http.Get(server.Url() + "graph.json")
if err != nil {
t.Errorf("HTTP request returned an error: %v", err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Errorf("Error while reading response body: %v", err)
}
result, err := json.Marshal(graph)
if err != nil {
t.Errorf("Error marshalling graph: %v", err)
}
if string(result) != strings.TrimRight(string(body), "\r\n") {
t.Errorf("Expected graph to be a json string.\nExpected: %v\nGot: %v", string(result), string(body))
}
}