forked from apolloconfig/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify_server_test.go
63 lines (52 loc) · 1.63 KB
/
notify_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
package agollo
import (
"fmt"
"net/http"
"net/http/httptest"
"time"
)
const responseStr = `[{"namespaceName":"application","notificationId":%d}]`
//run mock notify server
func runMockNotifyServer(handler func(http.ResponseWriter, *http.Request)) {
http.HandleFunc("/notifications/v2", handler)
appConfig := GetAppConfig(nil)
if appConfig == nil {
panic("can not find apollo config!please confirm!")
}
logger.Info("runMockNotifyServer:", appConfig.Ip)
err := http.ListenAndServe(fmt.Sprintf("%s", appConfig.Ip), nil)
if err != nil {
logger.Error("runMockConfigServer err:", err)
}
}
var normalNotifyCount = 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 runNormalResponse() *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(10 * time.Second)
normalNotifyCount++
if normalNotifyCount%3 == 0 {
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf(responseStr, normalNotifyCount)))
} else {
time.Sleep(5 * time.Second)
w.WriteHeader(http.StatusNotModified)
}
}))
return ts
}
func onlyNormalResponse(rw http.ResponseWriter, req *http.Request) {
result := fmt.Sprintf(responseStr, 3)
fmt.Fprintf(rw, "%s", result)
}
//Error response
//will hold 5s and keep response 404
func runErrorResponse() *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
return ts
}