-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
87 lines (63 loc) · 1.58 KB
/
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
package main
import (
"fmt"
"io/ioutil"
"net/http/httptest"
"strings"
"testing"
)
func TestNewServer(t *testing.T) {
s := NewServer(337, "something", 123)
if s.port != 337 {
t.Fatal("Invalid port found!")
}
if s.redisDB == nil {
t.Fatal("Invalid redis db client!")
}
if s.getMatch == nil {
t.Fatal("Get match invalid!")
}
if s.setMatch == nil {
t.Fatal("Set match invalid!")
}
}
func TestRoutingMatches(t *testing.T) {
s := NewServer(337, "localhost", 6379)
if s.getMatch.MatchString("/get/site.com/live/value") != true {
t.Fatal("invalid get match regex!")
}
if s.setMatch.MatchString("/set/site.com/live/value") != true {
t.Fatal("Invalid set match regex!")
}
}
func TestSetValue(t *testing.T) {
s := NewServer(123, "localhost", 6379)
w := httptest.NewRecorder()
s.setValue(w, "something", "live", "key")
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
t.Fatal("Invalid status code received!")
}
bodyStr := string(body)
fmt.Println(bodyStr)
if strings.Contains(bodyStr, `"key":"key"`) == false {
t.Fatal("Did not find key field!")
}
}
func TestServeHTTP(t *testing.T) {
s := NewServer(123, "localhost", 6379)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/set/seomthing/something/something", nil)
s.ServeHTTP(w, r)
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
t.Fatal("Invalid status code received!")
}
bodyStr := string(body)
fmt.Println(bodyStr)
if strings.Contains(bodyStr, `"key":"key"`) == false {
t.Fatal("Did not find key field!")
}
}