-
Notifications
You must be signed in to change notification settings - Fork 8
/
middleware_tls_test.go
79 lines (69 loc) · 2.21 KB
/
middleware_tls_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
package gin
import (
"crypto/tls"
"net/http"
"net/http/httptest"
"testing"
contractshttp "github.com/goravel/framework/contracts/http"
configmocks "github.com/goravel/framework/mocks/config"
"github.com/stretchr/testify/assert"
)
func TestTls(t *testing.T) {
var (
mockConfig *configmocks.Config
responseRecorder *httptest.ResponseRecorder
)
beforeEach := func() {
mockConfig = &configmocks.Config{}
}
tests := []struct {
name string
setup func()
}{
{
name: "not use tls",
setup: func() {
mockConfig.On("GetBool", "app.debug").Return(true).Once()
mockConfig.On("GetInt", "http.drivers.gin.body_limit", 4096).Return(4096).Once()
mockConfig.On("GetString", "http.tls.host").Return("").Once()
mockConfig.On("GetString", "http.tls.port").Return("").Once()
mockConfig.On("GetString", "http.tls.ssl.cert").Return("").Once()
mockConfig.On("GetString", "http.tls.ssl.key").Return("").Once()
ConfigFacade = mockConfig
},
},
{
name: "use tls",
setup: func() {
mockConfig.On("GetBool", "app.debug").Return(true).Once()
mockConfig.On("GetInt", "http.drivers.gin.body_limit", 4096).Return(4096).Once()
mockConfig.On("GetString", "http.tls.host").Return("127.0.0.1").Once()
mockConfig.On("GetString", "http.tls.port").Return("3000").Once()
mockConfig.On("GetString", "http.tls.ssl.cert").Return("test_ca.crt").Once()
mockConfig.On("GetString", "http.tls.ssl.key").Return("test_ca.key").Once()
ConfigFacade = mockConfig
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
beforeEach()
test.setup()
route, err := NewRoute(mockConfig, nil)
assert.Nil(t, err)
route.setMiddlewares([]contractshttp.Middleware{Tls()})
route.Any("/any/{id}", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().Json(contractshttp.Json{
"id": ctx.Request().Input("id"),
})
})
responseRecorder = httptest.NewRecorder()
req, err := http.NewRequest("POST", "/any/1", nil)
req.TLS = &tls.ConnectionState{}
assert.Nil(t, err)
route.ServeHTTP(responseRecorder, req)
assert.Equal(t, http.StatusOK, responseRecorder.Code)
mockConfig.AssertExpectations(t)
})
}
}