-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
141 lines (117 loc) · 3.14 KB
/
main_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
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
package main
import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/nicksnyder/go-i18n/i18n"
bloomsky "github.com/patrickalin/bloomsky-api-go"
)
var serv *httpServer
func TestMain(m *testing.M) {
if err := i18n.ParseTranslationFileBytes("lang/en-us.all.json", readFile("lang/en-us.all.json", true)); err != nil {
logFatal(err, funcName(), "Error read language file check in config.yaml if dev=false", "")
}
translateFunc, err := i18n.Tfunc("en-us")
checkErr(err, funcName(), "Problem with loading translate file", "")
channels := make(map[string]chan bloomsky.Bloomsky)
serv, err = createWebServer(channels["web"], ":1110", ":2220", translateFunc, true, store{}, false)
checkErr(err, funcName(), "Impossible to create server", "")
os.Exit(m.Run())
}
func TestLoadCorrectConfig(t *testing.T) {
conf := readConfig("configForTest")
//conf2 := initServerConfiguration("wrongConfigForTest")
tests := []struct {
name string
fields bool
want bool
}{
{"Test Mock good conf", conf.mock, true},
{"Test Devel good conf", conf.dev, true},
//{"Test Mock wrong conf", conf2.mock, true},
//{"Test Devel wrong conf", conf2.dev, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.fields; got != tt.want {
t.Errorf("mock = %v, want %v", got, tt.want)
}
})
}
}
func TestHanlderHome(t *testing.T) {
req, err := http.NewRequest(
http.MethodGet,
"http://localhost:1110/",
nil,
)
if err != nil {
logFatal(err, funcName(), "Could not create request: %v")
}
rec := httptest.NewRecorder()
serv.home(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("expected 200; got %d", rec.Code)
}
if !strings.Contains(rec.Body.String(), "est") {
t.Errorf("unexpected body; %q", rec.Body.String())
}
}
func TestHanlderLog(t *testing.T) {
req, err := http.NewRequest(
http.MethodGet,
"http://localhost:1110/log",
nil,
)
if err != nil {
logFatal(err, funcName(), "Could not request: %v")
}
rec := httptest.NewRecorder()
serv.log(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("expected 200; got %d", rec.Code)
}
if !strings.Contains(rec.Body.String(), "Server HTTPS listen on port") {
t.Errorf("unexpected body; %q", rec.Body.String())
}
}
func TestHanlderHistory(t *testing.T) {
req, err := http.NewRequest(
http.MethodGet,
"http://localhost:1110/history",
nil,
)
if err != nil {
logFatal(err, funcName(), "Could not request: %v")
}
rec := httptest.NewRecorder()
serv.history(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("expected 200; got %d", rec.Code)
}
if !strings.Contains(rec.Body.String(), "corechart") {
t.Errorf("unexpected body; %q", rec.Body.String())
}
}
func BenchmarkHanlder(b *testing.B) {
for i := 0; i < b.N; i++ {
req, err := http.NewRequest(
http.MethodGet,
"http://localhost:1110",
nil,
)
if err != nil {
logFatal(err, funcName(), "Could not request: %v")
}
rec := httptest.NewRecorder()
serv.home(rec, req)
if rec.Code != http.StatusOK {
b.Errorf("expected 200; got %d", rec.Code)
}
if !strings.Contains(rec.Body.String(), "Est") {
b.Errorf("unexpected body; %q", rec.Body.String())
}
}
}