github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_https_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package ghttp_test
     8  
     9  import (
    10  	_ "github.com/wangyougui/gf/v2/net/ghttp/testdata/https/packed"
    11  
    12  	"fmt"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/wangyougui/gf/v2/frame/g"
    17  	"github.com/wangyougui/gf/v2/net/ghttp"
    18  	"github.com/wangyougui/gf/v2/net/gtcp"
    19  	"github.com/wangyougui/gf/v2/os/gfile"
    20  	"github.com/wangyougui/gf/v2/os/gtime"
    21  	"github.com/wangyougui/gf/v2/test/gtest"
    22  	"github.com/wangyougui/gf/v2/text/gstr"
    23  	"github.com/wangyougui/gf/v2/util/guid"
    24  )
    25  
    26  func Test_HTTPS_Basic(t *testing.T) {
    27  	s := g.Server(guid.S())
    28  	s.Group("/", func(group *ghttp.RouterGroup) {
    29  		group.GET("/test", func(r *ghttp.Request) {
    30  			r.Response.Write("test")
    31  		})
    32  	})
    33  	s.EnableHTTPS(
    34  		gtest.DataPath("https", "files", "server.crt"),
    35  		gtest.DataPath("https", "files", "server.key"),
    36  	)
    37  	s.SetDumpRouterMap(false)
    38  	s.Start()
    39  	defer s.Shutdown()
    40  
    41  	time.Sleep(100 * time.Millisecond)
    42  
    43  	// HTTP
    44  	gtest.C(t, func(t *gtest.T) {
    45  		c := g.Client()
    46  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    47  		t.AssertIN(gstr.Trim(c.GetContent(ctx, "/")), g.Slice{"", "Client sent an HTTP request to an HTTPS server."})
    48  		t.AssertIN(gstr.Trim(c.GetContent(ctx, "/test")), g.Slice{"", "Client sent an HTTP request to an HTTPS server."})
    49  	})
    50  	// HTTPS
    51  	gtest.C(t, func(t *gtest.T) {
    52  		c := g.Client()
    53  		c.SetPrefix(fmt.Sprintf("https://127.0.0.1:%d", s.GetListenedPort()))
    54  		t.Assert(c.GetContent(ctx, "/"), "Not Found")
    55  		t.Assert(c.GetContent(ctx, "/test"), "test")
    56  	})
    57  }
    58  
    59  func Test_HTTPS_Resource(t *testing.T) {
    60  	s := g.Server(guid.S())
    61  	s.Group("/", func(group *ghttp.RouterGroup) {
    62  		group.GET("/test", func(r *ghttp.Request) {
    63  			r.Response.Write("test")
    64  		})
    65  	})
    66  	s.EnableHTTPS(
    67  		gfile.Join("files", "server.crt"),
    68  		gfile.Join("files", "server.key"),
    69  	)
    70  	s.SetDumpRouterMap(false)
    71  	s.Start()
    72  	defer s.Shutdown()
    73  
    74  	time.Sleep(100 * time.Millisecond)
    75  
    76  	// HTTP
    77  	gtest.C(t, func(t *gtest.T) {
    78  		c := g.Client()
    79  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    80  		t.AssertIN(gstr.Trim(c.GetContent(ctx, "/")), g.Slice{"", "Client sent an HTTP request to an HTTPS server."})
    81  		t.AssertIN(gstr.Trim(c.GetContent(ctx, "/test")), g.Slice{"", "Client sent an HTTP request to an HTTPS server."})
    82  	})
    83  	// HTTPS
    84  	gtest.C(t, func(t *gtest.T) {
    85  		c := g.Client()
    86  		c.SetPrefix(fmt.Sprintf("https://127.0.0.1:%d", s.GetListenedPort()))
    87  		t.Assert(c.GetContent(ctx, "/"), "Not Found")
    88  		t.Assert(c.GetContent(ctx, "/test"), "test")
    89  	})
    90  }
    91  
    92  func Test_HTTPS_HTTP_Basic(t *testing.T) {
    93  	var (
    94  		portHttp, _  = gtcp.GetFreePort()
    95  		portHttps, _ = gtcp.GetFreePort()
    96  	)
    97  	s := g.Server(gtime.TimestampNanoStr())
    98  	s.Group("/", func(group *ghttp.RouterGroup) {
    99  		group.GET("/test", func(r *ghttp.Request) {
   100  			r.Response.Write("test")
   101  		})
   102  	})
   103  	s.EnableHTTPS(
   104  		gtest.DataPath("https", "files", "server.crt"),
   105  		gtest.DataPath("https", "files", "server.key"),
   106  	)
   107  	s.SetPort(portHttp)
   108  	s.SetHTTPSPort(portHttps)
   109  	s.SetDumpRouterMap(false)
   110  	s.Start()
   111  	defer s.Shutdown()
   112  
   113  	time.Sleep(100 * time.Millisecond)
   114  
   115  	// HTTP
   116  	gtest.C(t, func(t *gtest.T) {
   117  		c := g.Client()
   118  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", portHttp))
   119  		t.Assert(c.GetContent(ctx, "/"), "Not Found")
   120  		t.Assert(c.GetContent(ctx, "/test"), "test")
   121  	})
   122  	// HTTPS
   123  	gtest.C(t, func(t *gtest.T) {
   124  		c := g.Client()
   125  		c.SetPrefix(fmt.Sprintf("https://127.0.0.1:%d", portHttps))
   126  		t.Assert(c.GetContent(ctx, "/"), "Not Found")
   127  		t.Assert(c.GetContent(ctx, "/test"), "test")
   128  	})
   129  }