2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-04-15 09:55:44 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package ghttp_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/debug/gdebug"
|
2020-12-09 00:06:56 +08:00
|
|
|
"github.com/gogf/gf/os/gfile"
|
2020-04-15 09:55:44 +08:00
|
|
|
"github.com/gogf/gf/os/gtime"
|
|
|
|
"github.com/gogf/gf/text/gstr"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gogf/gf/frame/g"
|
|
|
|
"github.com/gogf/gf/net/ghttp"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
2020-12-09 00:06:56 +08:00
|
|
|
|
|
|
|
_ "github.com/gogf/gf/net/ghttp/testdata/https/packed"
|
2020-04-15 09:55:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_HTTPS_Basic(t *testing.T) {
|
|
|
|
p, _ := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.GET("/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
s.EnableHTTPS(
|
2020-12-09 00:06:56 +08:00
|
|
|
gdebug.TestDataPath("https", "files", "server.crt"),
|
|
|
|
gdebug.TestDataPath("https", "files", "server.key"),
|
|
|
|
)
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// HTTP
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
c := g.Client()
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2021-09-30 14:06:46 +08:00
|
|
|
t.AssertIN(gstr.Trim(c.GetContent(ctx, "/")), g.Slice{"", "Client sent an HTTP request to an HTTPS server."})
|
|
|
|
t.AssertIN(gstr.Trim(c.GetContent(ctx, "/test")), g.Slice{"", "Client sent an HTTP request to an HTTPS server."})
|
2020-12-09 00:06:56 +08:00
|
|
|
})
|
|
|
|
// HTTPS
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
c := g.Client()
|
|
|
|
c.SetPrefix(fmt.Sprintf("https://127.0.0.1:%d", p))
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(c.GetContent(ctx, "/"), "Not Found")
|
|
|
|
t.Assert(c.GetContent(ctx, "/test"), "test")
|
2020-12-09 00:06:56 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_HTTPS_Resource(t *testing.T) {
|
|
|
|
p, _ := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.GET("/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
s.EnableHTTPS(
|
|
|
|
gfile.Join("files", "server.crt"),
|
|
|
|
gfile.Join("files", "server.key"),
|
2020-04-15 09:55:44 +08:00
|
|
|
)
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// HTTP
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
c := g.Client()
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2021-09-30 14:06:46 +08:00
|
|
|
t.AssertIN(gstr.Trim(c.GetContent(ctx, "/")), g.Slice{"", "Client sent an HTTP request to an HTTPS server."})
|
|
|
|
t.AssertIN(gstr.Trim(c.GetContent(ctx, "/test")), g.Slice{"", "Client sent an HTTP request to an HTTPS server."})
|
2020-04-15 09:55:44 +08:00
|
|
|
})
|
|
|
|
// HTTPS
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
c := g.Client()
|
|
|
|
c.SetPrefix(fmt.Sprintf("https://127.0.0.1:%d", p))
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(c.GetContent(ctx, "/"), "Not Found")
|
|
|
|
t.Assert(c.GetContent(ctx, "/test"), "test")
|
2020-04-15 09:55:44 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_HTTPS_HTTP_Basic(t *testing.T) {
|
|
|
|
var (
|
|
|
|
portHttp, _ = ports.PopRand()
|
|
|
|
portHttps, _ = ports.PopRand()
|
|
|
|
)
|
|
|
|
s := g.Server(gtime.TimestampNanoStr())
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.GET("/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
s.EnableHTTPS(
|
2020-12-09 00:06:56 +08:00
|
|
|
gdebug.TestDataPath("https", "files", "server.crt"),
|
|
|
|
gdebug.TestDataPath("https", "files", "server.key"),
|
2020-04-15 09:55:44 +08:00
|
|
|
)
|
|
|
|
s.SetPort(portHttp)
|
|
|
|
s.SetHTTPSPort(portHttps)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// HTTP
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
c := g.Client()
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", portHttp))
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(c.GetContent(ctx, "/"), "Not Found")
|
|
|
|
t.Assert(c.GetContent(ctx, "/test"), "test")
|
2020-04-15 09:55:44 +08:00
|
|
|
})
|
|
|
|
// HTTPS
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
c := g.Client()
|
|
|
|
c.SetPrefix(fmt.Sprintf("https://127.0.0.1:%d", portHttps))
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(c.GetContent(ctx, "/"), "Not Found")
|
|
|
|
t.Assert(c.GetContent(ctx, "/test"), "test")
|
2020-04-15 09:55:44 +08:00
|
|
|
})
|
|
|
|
}
|