// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. // // 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" "testing" "time" _ "github.com/gogf/gf/v2/net/ghttp/testdata/https/packed" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/os/gfile" "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/text/gstr" "github.com/gogf/gf/v2/util/guid" ) func Test_HTTPS_Basic(t *testing.T) { s := g.Server(guid.S()) s.Group("/", func(group *ghttp.RouterGroup) { group.GET("/test", func(r *ghttp.Request) { r.Response.Write("test") }) }) s.EnableHTTPS( gtest.DataPath("https", "files", "server.crt"), gtest.DataPath("https", "files", "server.key"), ) 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", s.GetListenedPort())) 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."}) }) // HTTPS gtest.C(t, func(t *gtest.T) { c := g.Client() c.SetPrefix(fmt.Sprintf("https://127.0.0.1:%d", s.GetListenedPort())) t.Assert(c.GetContent(ctx, "/"), "Not Found") t.Assert(c.GetContent(ctx, "/test"), "test") }) } func Test_HTTPS_Resource(t *testing.T) { s := g.Server(guid.S()) 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"), ) 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", s.GetListenedPort())) 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."}) }) // HTTPS gtest.C(t, func(t *gtest.T) { c := g.Client() c.SetPrefix(fmt.Sprintf("https://127.0.0.1:%d", s.GetListenedPort())) t.Assert(c.GetContent(ctx, "/"), "Not Found") t.Assert(c.GetContent(ctx, "/test"), "test") }) } func Test_HTTPS_HTTP_Basic(t *testing.T) { s := g.Server(gtime.TimestampNanoStr()) s.Group("/", func(group *ghttp.RouterGroup) { group.GET("/test", func(r *ghttp.Request) { r.Response.Write("test") }) }) s.EnableHTTPS( gtest.DataPath("https", "files", "server.crt"), gtest.DataPath("https", "files", "server.key"), ) s.SetPort(0) s.SetHTTPSPort(0) 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", s.GetListenedPort())) t.Assert(c.GetContent(ctx, "/"), "Not Found") t.Assert(c.GetContent(ctx, "/test"), "test") }) // HTTPS gtest.C(t, func(t *gtest.T) { c := g.Client() c.SetPrefix(fmt.Sprintf("https://127.0.0.1:%d", s.GetListenedHTTPSPort())) t.Assert(c.GetContent(ctx, "/"), "Not Found") t.Assert(c.GetContent(ctx, "/test"), "test") }) }