forked from apolloconfig/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_server_test.go
62 lines (53 loc) · 1.52 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
package agollo
import (
"net/http"
"fmt"
"time"
"github.com/cihub/seelog"
)
const configResponseStr =`{
"appId": "100004458",
"cluster": "default",
"namespaceName": "application",
"configurations": {
"key1":"value1",
"key2":"value2"
},
"releaseKey": "20170430092936-dee2d58e74515ff3"
}`
//run mock config server
func runMockConfigServer(handler func(http.ResponseWriter, *http.Request)) {
appConfig:=GetAppConfig()
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{
seelog.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 normalConfigResponse(rw http.ResponseWriter, req *http.Request) {
normalConfigCount++
if normalConfigCount%3==0 {
fmt.Fprintf(rw, configResponseStr)
}else {
time.Sleep(500 * time.Microsecond)
rw.WriteHeader(http.StatusNotModified)
}
}
//Error response
//will hold 5s and keep response 404
func errorConfigResponse(rw http.ResponseWriter, req *http.Request) {
time.Sleep(500 * time.Microsecond)
rw.WriteHeader(http.StatusNotFound)
}