Add test cases for RunTLS and each mode (#1173)
* add RunTLS test cases and add debug/test mode cases * add release mode cases
This commit is contained in:
parent
ae9f03e6e8
commit
1f377cb847
18
fixtures/testdata/cert.pem
vendored
Normal file
18
fixtures/testdata/cert.pem
vendored
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
MIIC9DCCAdygAwIBAgIQUNSK+OxWHYYFxHVJV0IlpDANBgkqhkiG9w0BAQsFADAS
|
||||||
|
MRAwDgYDVQQKEwdBY21lIENvMB4XDTE3MTExNjEyMDA0N1oXDTE4MTExNjEyMDA0
|
||||||
|
N1owEjEQMA4GA1UEChMHQWNtZSBDbzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC
|
||||||
|
AQoCggEBAKmyj/YZpD59Bpy4w3qf6VzMw9uUBsWp+IP4kl7z5cmGHYUHn/YopTLH
|
||||||
|
vR23GAB12p6Km5QWzCBuJF4j61PJXHfg3/rjShZ77JcQ3kzxuy1iKDI+DNKN7Klz
|
||||||
|
rdjJ49QD0lczZHeBvvCL7JsJFKFjGy62rnStuW8LaIEdtjXT+GUZTxJh6G7yPYfD
|
||||||
|
MS1IsdMQGOdbGwNa+qogMuQPh0TzHw+C73myKrjY6pREijknMC/rnIMz9dLPt6Kl
|
||||||
|
xXy4br443dpY6dYGIhDuKhROT+vZ05HKasuuQUFhY7v/KoUpEZMB9rfUSzjQ5fak
|
||||||
|
eDUAMniXRcd+DmwvboG2TI6ixmuPK+ECAwEAAaNGMEQwDgYDVR0PAQH/BAQDAgWg
|
||||||
|
MBMGA1UdJQQMMAoGCCsGAQUFBwMBMAwGA1UdEwEB/wQCMAAwDwYDVR0RBAgwBocE
|
||||||
|
fwAAATANBgkqhkiG9w0BAQsFAAOCAQEAMXOLvj7BFsxdbcfRPBd0OFrH/8lI7vPV
|
||||||
|
LRcJ6r5iv0cnNvZXXbIOQLbg4clJAWjoE08nRm1KvNXhKdns0ELEV86YN2S6jThq
|
||||||
|
rIGrBqKtaJLB3M9BtDSiQ6SGPLYrWvmhj3Avi8PbSGy51bpGbqopd16j6LYU7Cp2
|
||||||
|
TefMRlOAFtHojpCVon1CMpqcNxS0WNlQ3lUBSrw3HB0o12x++roja2ibF54tSHXB
|
||||||
|
KUuadoEzN+mMBwenEBychmAGzdiG4GQHRmhigh85+mtW6UMGiqyCZHs0EgE9FCLL
|
||||||
|
sRrsTI/VOzLz6lluygXkOsXrP+PP0SvmE3eylWjj9e2nj/u/Cy2YKg==
|
||||||
|
-----END CERTIFICATE-----
|
27
fixtures/testdata/key.pem
vendored
Normal file
27
fixtures/testdata/key.pem
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
-----BEGIN RSA PRIVATE KEY-----
|
||||||
|
MIIEogIBAAKCAQEAqbKP9hmkPn0GnLjDep/pXMzD25QGxan4g/iSXvPlyYYdhQef
|
||||||
|
9iilMse9HbcYAHXanoqblBbMIG4kXiPrU8lcd+Df+uNKFnvslxDeTPG7LWIoMj4M
|
||||||
|
0o3sqXOt2Mnj1APSVzNkd4G+8IvsmwkUoWMbLraudK25bwtogR22NdP4ZRlPEmHo
|
||||||
|
bvI9h8MxLUix0xAY51sbA1r6qiAy5A+HRPMfD4LvebIquNjqlESKOScwL+ucgzP1
|
||||||
|
0s+3oqXFfLhuvjjd2ljp1gYiEO4qFE5P69nTkcpqy65BQWFju/8qhSkRkwH2t9RL
|
||||||
|
ONDl9qR4NQAyeJdFx34ObC9ugbZMjqLGa48r4QIDAQABAoIBAD5mhd+GMEo2KU9J
|
||||||
|
9b/Ku8I/HapJtW/L/7Fvn0tBPncrVQGM+zpGWfDhV95sbGwG6lwwNeNvuqIWPlNL
|
||||||
|
vAY0XkdKrrIQEDdSXH50WnpKzXxzwrou7QIj5Cmvevbjzl4xBZDBOilj0XWczmV4
|
||||||
|
IljyG5XC4UXQeAaoWEZaSZ1jk8yAt2Zq1Hgg7HqhHsK/arWXBgax+4K5nV/s9gZx
|
||||||
|
yjKU9mXTIs7k/aNnZqwQKqcZF+l3mvbZttOaFwsP14H0I8OFWhnM9hie54Dejqxi
|
||||||
|
f4/llNxDqUs6lqJfP3qNxtORLcFe75M+Yl8v7g2hkjtLdZBakPzSTEx3TAK/UHgi
|
||||||
|
aM8DdxECgYEA3fmg/PI4EgUEj0C3SCmQXR/CnQLMUQgb54s0asp4akvp+M7YCcr1
|
||||||
|
pQd3HFUpBwhBcJg5LeSe87vLupY7pHCKk56cl9WY6hse0b9sP/7DWJuGiO62m0E0
|
||||||
|
vNjQ2jpG99oR2ROIHHeWsGCpGLmrRT/kY+vR3M+AOLZniXlOCw8k0aUCgYEAw7WL
|
||||||
|
XFWLxgZYQYilywqrQmfv1MBfaUCvykO6oWB+f6mmnihSFjecI+nDw/b3yXVYGEgy
|
||||||
|
0ebkuw0jP8suC8wBqX9WuXj+9nZNomJRssJyOMiEhDEqUiTztFPSp9pdruoakLTh
|
||||||
|
Wk1p9NralOqGPUmxpXlFKVmYRTUbluikVxDypI0CgYBn6sqEQH0hann0+o4TWWn9
|
||||||
|
PrYkPUAbm1k8771tVTZERR/W3Dbldr/DL5iCihe39BR2urziEEqdvkglJNntJMar
|
||||||
|
TzDuIBADYQjvltb9qq4XGFBGYMLaMg+XbUVxNKEuvUdnwa4R7aZ9EfN34MwekkfA
|
||||||
|
w5Cu9/GGG1ajVEfGA6PwBQKBgA3o71jGs8KFXOx7e90sivOTU5Z5fc6LTHNB0Rf7
|
||||||
|
NcJ5GmCPWRY/KZfb25AoE4B8GKDRMNt+X69zxZeZJ1KrU0rqxA02rlhyHB54gnoE
|
||||||
|
G/4xMkn6/JkOC0w70PMhMBtohC7YzFOQwQEoNPT0nkno3Pl33xSLS6lPlwBo1JVj
|
||||||
|
nPtZAoGACXNLXYkR5vexE+w6FGl59r4RQhu1XU8Mr5DIHeB7kXPN3RKbS201M+Tb
|
||||||
|
SB5jbu0iDV477XkzSNmhaksFf2wM9MT6CaE+8n3UU5tMa+MmBGgwYTp/i9HkqVh5
|
||||||
|
jjpJifn1VWBINd4cpNzwCg9LXoo0tbtUPWwGzqVeyo/YE5GIHGo=
|
||||||
|
-----END RSA PRIVATE KEY-----
|
124
gin_test.go
124
gin_test.go
@ -5,6 +5,7 @@
|
|||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -21,9 +22,9 @@ func formatAsDate(t time.Time) string {
|
|||||||
return fmt.Sprintf("%d/%02d/%02d", year, month, day)
|
return fmt.Sprintf("%d/%02d/%02d", year, month, day)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupHTMLFiles(t *testing.T) func() {
|
func setupHTMLFiles(t *testing.T, mode string, tls bool) func() {
|
||||||
go func() {
|
go func() {
|
||||||
SetMode(TestMode)
|
SetMode(mode)
|
||||||
router := New()
|
router := New()
|
||||||
router.Delims("{[{", "}]}")
|
router.Delims("{[{", "}]}")
|
||||||
router.SetFuncMap(template.FuncMap{
|
router.SetFuncMap(template.FuncMap{
|
||||||
@ -38,16 +39,21 @@ func setupHTMLFiles(t *testing.T) func() {
|
|||||||
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
|
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
if tls {
|
||||||
|
// these files generated by `go run $GOROOT/src/crypto/tls/generate_cert.go --host 127.0.0.1`
|
||||||
|
router.RunTLS(":9999", "./fixtures/testdata/cert.pem", "./fixtures/testdata/key.pem")
|
||||||
|
} else {
|
||||||
router.Run(":8888")
|
router.Run(":8888")
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
t.Log("waiting 1 second for server startup")
|
t.Log("waiting 1 second for server startup")
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
return func() {}
|
return func() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupHTMLGlob(t *testing.T) func() {
|
func setupHTMLGlob(t *testing.T, mode string, tls bool) func() {
|
||||||
go func() {
|
go func() {
|
||||||
SetMode(DebugMode)
|
SetMode(mode)
|
||||||
router := New()
|
router := New()
|
||||||
router.Delims("{[{", "}]}")
|
router.Delims("{[{", "}]}")
|
||||||
router.SetFuncMap(template.FuncMap{
|
router.SetFuncMap(template.FuncMap{
|
||||||
@ -62,16 +68,20 @@ func setupHTMLGlob(t *testing.T) func() {
|
|||||||
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
|
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
if tls {
|
||||||
|
// these files generated by `go run $GOROOT/src/crypto/tls/generate_cert.go --host 127.0.0.1`
|
||||||
|
router.RunTLS(":9999", "./fixtures/testdata/cert.pem", "./fixtures/testdata/key.pem")
|
||||||
|
} else {
|
||||||
router.Run(":8888")
|
router.Run(":8888")
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
t.Log("waiting 1 second for server startup")
|
t.Log("waiting 1 second for server startup")
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
return func() {}
|
return func() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO
|
|
||||||
func TestLoadHTMLGlob(t *testing.T) {
|
func TestLoadHTMLGlob(t *testing.T) {
|
||||||
td := setupHTMLGlob(t)
|
td := setupHTMLGlob(t, DebugMode, false)
|
||||||
res, err := http.Get("http://127.0.0.1:8888/test")
|
res, err := http.Get("http://127.0.0.1:8888/test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@ -83,9 +93,55 @@ func TestLoadHTMLGlob(t *testing.T) {
|
|||||||
td()
|
td()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadHTMLGlob2(t *testing.T) {
|
||||||
|
td := setupHTMLGlob(t, TestMode, false)
|
||||||
|
res, err := http.Get("http://127.0.0.1:8888/test")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := ioutil.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp[:]))
|
||||||
|
|
||||||
|
td()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadHTMLGlob3(t *testing.T) {
|
||||||
|
td := setupHTMLGlob(t, ReleaseMode, false)
|
||||||
|
res, err := http.Get("http://127.0.0.1:8888/test")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := ioutil.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp[:]))
|
||||||
|
|
||||||
|
td()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadHTMLGlobUsingTLS(t *testing.T) {
|
||||||
|
td := setupHTMLGlob(t, DebugMode, true)
|
||||||
|
// Use InsecureSkipVerify for avoiding `x509: certificate signed by unknown authority` error
|
||||||
|
tr := &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
client := &http.Client{Transport: tr}
|
||||||
|
res, err := client.Get("https://127.0.0.1:9999/test")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := ioutil.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp[:]))
|
||||||
|
|
||||||
|
td()
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoadHTMLGlobFromFuncMap(t *testing.T) {
|
func TestLoadHTMLGlobFromFuncMap(t *testing.T) {
|
||||||
time.Now()
|
time.Now()
|
||||||
td := setupHTMLGlob(t)
|
td := setupHTMLGlob(t, DebugMode, false)
|
||||||
res, err := http.Get("http://127.0.0.1:8888/raw")
|
res, err := http.Get("http://127.0.0.1:8888/raw")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@ -97,9 +153,6 @@ func TestLoadHTMLGlobFromFuncMap(t *testing.T) {
|
|||||||
td()
|
td()
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (engine *Engine) LoadHTMLFiles(files ...string) {
|
|
||||||
// func (engine *Engine) RunTLS(addr string, cert string, key string) error {
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
SetMode(TestMode)
|
SetMode(TestMode)
|
||||||
}
|
}
|
||||||
@ -127,7 +180,7 @@ func TestCreateEngine(t *testing.T) {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
func TestLoadHTMLFiles(t *testing.T) {
|
func TestLoadHTMLFiles(t *testing.T) {
|
||||||
td := setupHTMLFiles(t)
|
td := setupHTMLFiles(t, TestMode, false)
|
||||||
res, err := http.Get("http://127.0.0.1:8888/test")
|
res, err := http.Get("http://127.0.0.1:8888/test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@ -138,9 +191,52 @@ func TestLoadHTMLFiles(t *testing.T) {
|
|||||||
td()
|
td()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadHTMLFiles2(t *testing.T) {
|
||||||
|
td := setupHTMLFiles(t, DebugMode, false)
|
||||||
|
res, err := http.Get("http://127.0.0.1:8888/test")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := ioutil.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp[:]))
|
||||||
|
td()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadHTMLFiles3(t *testing.T) {
|
||||||
|
td := setupHTMLFiles(t, ReleaseMode, false)
|
||||||
|
res, err := http.Get("http://127.0.0.1:8888/test")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := ioutil.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp[:]))
|
||||||
|
td()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadHTMLFilesUsingTLS(t *testing.T) {
|
||||||
|
td := setupHTMLFiles(t, TestMode, true)
|
||||||
|
// Use InsecureSkipVerify for avoiding `x509: certificate signed by unknown authority` error
|
||||||
|
tr := &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
client := &http.Client{Transport: tr}
|
||||||
|
res, err := client.Get("https://127.0.0.1:9999/test")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := ioutil.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp[:]))
|
||||||
|
td()
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoadHTMLFilesFuncMap(t *testing.T) {
|
func TestLoadHTMLFilesFuncMap(t *testing.T) {
|
||||||
time.Now()
|
time.Now()
|
||||||
td := setupHTMLFiles(t)
|
td := setupHTMLFiles(t, TestMode, false)
|
||||||
res, err := http.Get("http://127.0.0.1:8888/raw")
|
res, err := http.Get("http://127.0.0.1:8888/raw")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@ -152,10 +248,6 @@ func TestLoadHTMLFilesFuncMap(t *testing.T) {
|
|||||||
td()
|
td()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadHTMLReleaseMode(t *testing.T) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAddRoute(t *testing.T) {
|
func TestAddRoute(t *testing.T) {
|
||||||
router := New()
|
router := New()
|
||||||
router.addRoute("GET", "/", HandlersChain{func(_ *Context) {}})
|
router.addRoute("GET", "/", HandlersChain{func(_ *Context) {}})
|
||||||
|
Loading…
Reference in New Issue
Block a user