-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping_test.go
103 lines (87 loc) · 3.32 KB
/
ping_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
package ginid_uuidv4_test
import (
"encoding/json"
"fmt"
"github.com/bar-counter/gin-correlation-id/gin_correlation_cors"
"github.com/bar-counter/gin-correlation-id/gin_correlation_id_shortuuid"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
func ginPingJsonRouter(correlationKey string, isSupportCorsHeader bool) *gin.Engine {
gin_correlation_cors.SetIsSupportCorsHeader(isSupportCorsHeader)
gin_correlation_cors.SetCorrelationIDShortUuidKey(correlationKey)
router := gin.New()
router.Use(gin_correlation_id_shortuuid.Middleware())
router.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
gin_correlation_cors.CorrelationIDHeaderDefault: gin_correlation_id_shortuuid.GetCorrelationID(c),
})
})
return router
}
func TestPanicSetCorrelationIDShortUuidKey(t *testing.T) {
// mock TestPanicSetCorrelationIDShortUuidKey
errString := "can not SetCorrelationIDShortUuidKey set by empty"
if !assert.PanicsWithError(t, errString, func() {
// do TestPanicSetCorrelationIDShortUuidKey
gin_correlation_cors.SetCorrelationIDShortUuidKey("")
}) {
// verify TestPanicSetCorrelationIDShortUuidKey
t.Fatalf("TestPanicSetCorrelationIDShortUuidKey should panic")
}
}
func TestPingOnce(t *testing.T) {
// mock PingOnce
t.Logf("~> mock PingOnce")
router := ginPingJsonRouter(gin_correlation_cors.CorrelationIDHeaderDefault, true)
// do PingOnce
t.Logf("~> do PingOnce")
w := performRequest(router, "GET", "/ping")
// verify PingOnce
assert.Equal(t, http.StatusOK, w.Code)
assert.NotNil(t, w.Header().Get(gin_correlation_cors.CorrelationIDHeaderDefault))
var response map[string]string
err := json.Unmarshal([]byte(w.Body.String()), &response)
assert.Nil(t, err)
value, exists := response[gin_correlation_cors.CorrelationIDHeaderDefault]
t.Logf("~> verify PingOnce %s", value)
assert.True(t, exists)
assert.NotNil(t, value)
}
func TestTestCloseCors(t *testing.T) {
// mock CloseCors
t.Logf("~> mock CloseCors")
// do CloseCors
router := ginPingJsonRouter(gin_correlation_cors.CorrelationIDHeaderDefault, false)
// do CloseCors
t.Logf("~> do CloseCors")
w := performRequest(router, "GET", "/ping")
// verify PingOnce
assert.Equal(t, http.StatusOK, w.Code)
assert.NotNil(t, w.Header().Get(gin_correlation_cors.CorrelationIDHeaderDefault))
// verify CloseCors
assert.Equal(t, "", w.Header().Get(gin_correlation_cors.HeaderAccessControlExposeHeaders))
}
func TestSetCorrelationIDShortUuidKey(t *testing.T) {
// mock SetCorrelationIDShortUuidKey
t.Logf("~> mock SetCorrelationIDShortUuidKey")
router := ginPingJsonRouter(gin_correlation_cors.KeyCorrelationIDHeaderId, true)
// do SetCorrelationIDShortUuidKey
t.Logf("~> do SetCorrelationIDShortUuidKey")
w := performRequest(router, "GET", "/ping")
assert.Equal(t, http.StatusOK, w.Code)
assert.NotNil(t, w.Header().Get(gin_correlation_cors.KeyCorrelationIDHeaderId))
// verify SetCorrelationIDShortUuidKey
assert.Equal(t, fmt.Sprintf("%s, %s",
gin_correlation_cors.KeyCorrelationIDHeaderId, gin_correlation_cors.CorrelationIDHeaderDefault),
w.Header().Get(gin_correlation_cors.HeaderAccessControlExposeHeaders))
}