-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
116 lines (78 loc) · 1.94 KB
/
handler_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
package main
import (
"testing"
"net/http"
"net/http/httptest"
"github.com/go-redis/redis"
"strconv"
. "gopkg.in/check.v1"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
type MySuite struct {
client *redis.Client
config Configuration
ip string
country string
}
var _ = Suite(&MySuite{
ip : "8.8.1.",
country : "United States",
})
func (s *MySuite) SetUpTest(c *C) {
var err error
s.config, err = GetConfiguration()
if err != nil {
c.Fatalf("Configuration read error : " + err.Error())
}
s.client, err = RedisNewClient()
if err != nil {
c.Fatalf(err.Error())
}
}
func (s *MySuite) TearDownTest(c *C) {
s.client.FlushDB()
s.client.Close()
}
func (s *MySuite) TestHandler(c *C) {
client = s.client
config = s.config
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
c.Fatal(err)
}
req.RemoteAddr = s.ip+"8:8080"
testRecorder := httptest.NewRecorder()
handler := http.HandlerFunc(handler)
handler.ServeHTTP(testRecorder, req)
c.Assert(testRecorder.Code,Equals,http.StatusOK)
c.Assert(testRecorder.Body.String(),Equals,`{"CountryName":"`+s.country+`"}`)
ipCountry, err := client.Get(s.ip+"8").Result()
if err != nil {
c.Fatalf(err.Error())
}
c.Assert(ipCountry,Equals,s.country)
}
func (s *MySuite) TestHandlerMass(c *C) {
client = s.client
config = s.config
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
c.Fatal(err)
}
var addr string
for i := 0; i < 30; i++ {
addr = s.ip + strconv.Itoa(i)
req.Header.Set("Origin", "http://"+addr+":8080")
testRecorder := httptest.NewRecorder()
handler := http.HandlerFunc(handler)
handler.ServeHTTP(testRecorder, req)
c.Assert(testRecorder.Code,Equals,http.StatusOK)
c.Assert(testRecorder.Body.String(),Equals,`{"CountryName":"`+s.country+`"}`)
ipCountry, err := client.Get(addr).Result()
if err != nil {
c.Fatalf(err.Error())
}
c.Assert(ipCountry,Equals,s.country)
}
}