forked from apolloconfig/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_server_test.go
114 lines (95 loc) · 2.8 KB
/
config_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
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
package agollo
import (
"net/http"
"fmt"
"time"
"net/http/httptest"
)
const configResponseStr =`{
"appId": "100004458",
"cluster": "default",
"namespaceName": "application",
"configurations": {
"key1":"value1",
"key2":"value2"
},
"releaseKey": "20170430092936-dee2d58e74515ff3"
}`
const configChangeResponseStr =`{
"appId": "100004458",
"cluster": "default",
"namespaceName": "application",
"configurations": {
"key1":"value1",
"key2":"value2",
"string":"string"
},
"releaseKey": "20170430092936-dee2d58e74515ff3"
}`
//run mock config server
func runMockConfigServer(handler func(http.ResponseWriter, *http.Request)) {
appConfig:=GetAppConfig(nil)
uri:=fmt.Sprintf("/configs/%s/%s/%s",appConfig.AppId,appConfig.Cluster,appConfig.NamespaceName)
http.HandleFunc(uri, handler)
if appConfig==nil{
panic("can not find apollo config!please confirm!")
}
err:=http.ListenAndServe(fmt.Sprintf("%s",appConfig.Ip), nil)
if err!=nil{
logger.Error("runMockConfigServer err:",err)
}
}
func closeMockConfigServer() {
http.DefaultServeMux=&http.ServeMux{}
}
var normalConfigCount=1
//Normal response
//First request will hold 5s and response http.StatusNotModified
//Second request will hold 5s and response http.StatusNotModified
//Second request will response [{"namespaceName":"application","notificationId":3}]
func runNormalConfigResponse() *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
normalConfigCount++
if normalConfigCount%3==0 {
w.WriteHeader(http.StatusOK)
w.Write([]byte(configResponseStr))
}else {
time.Sleep(500 * time.Microsecond)
w.WriteHeader(http.StatusNotModified)
}
}))
return ts
}
func runLongNotmodifiedConfigResponse() *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(500 * time.Microsecond)
w.WriteHeader(http.StatusNotModified)
}))
return ts
}
func runChangeConfigResponse()*httptest.Server{
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(configChangeResponseStr))
}))
return ts
}
func onlyNormalConfigResponse(rw http.ResponseWriter, req *http.Request) {
fmt.Fprintf(rw, configResponseStr)
}
func runNotModifyConfigResponse() *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(800 * time.Microsecond)
w.WriteHeader(http.StatusNotModified)
}))
return ts
}
//Error response
//will hold 5s and keep response 404
func runErrorConfigResponse() *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(500 * time.Microsecond)
w.WriteHeader(http.StatusNotFound)
}))
return ts
}