github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_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/gogf/gf.
     6  
     7  package ghttp_test
     8  
     9  import (
    10  	"fmt"
    11  	"github.com/gogf/gf/debug/gdebug"
    12  	"github.com/gogf/gf/os/gfile"
    13  	"github.com/gogf/gf/os/gtime"
    14  	"github.com/gogf/gf/text/gstr"
    15  	"testing"
    16  	"time"
    17  
    18  	"github.com/gogf/gf/frame/g"
    19  	"github.com/gogf/gf/net/ghttp"
    20  	"github.com/gogf/gf/test/gtest"
    21  
    22  	_ "github.com/gogf/gf/net/ghttp/testdata/https/packed"
    23  )
    24  
    25  func Test_HTTPS_Basic(t *testing.T) {
    26  	p, _ := ports.PopRand()
    27  	s := g.Server(p)
    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  		gdebug.TestDataPath("https", "files", "server.crt"),
    35  		gdebug.TestDataPath("https", "files", "server.key"),
    36  	)
    37  	s.SetPort(p)
    38  	s.SetDumpRouterMap(false)
    39  	s.Start()
    40  	defer s.Shutdown()
    41  
    42  	time.Sleep(100 * time.Millisecond)
    43  
    44  	// HTTP
    45  	gtest.C(t, func(t *gtest.T) {
    46  		c := g.Client()
    47  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    48  		t.AssertIN(gstr.Trim(c.GetContent("/")), g.Slice{"", "Client sent an HTTP request to an HTTPS server."})
    49  		t.AssertIN(gstr.Trim(c.GetContent("/test")), g.Slice{"", "Client sent an HTTP request to an HTTPS server."})
    50  	})
    51  	// HTTPS
    52  	gtest.C(t, func(t *gtest.T) {
    53  		c := g.Client()
    54  		c.SetPrefix(fmt.Sprintf("https://127.0.0.1:%d", p))
    55  		t.Assert(c.GetContent("/"), "Not Found")
    56  		t.Assert(c.GetContent("/test"), "test")
    57  	})
    58  }
    59  
    60  func Test_HTTPS_Resource(t *testing.T) {
    61  	p, _ := ports.PopRand()
    62  	s := g.Server(p)
    63  	s.Group("/", func(group *ghttp.RouterGroup) {
    64  		group.GET("/test", func(r *ghttp.Request) {
    65  			r.Response.Write("test")
    66  		})
    67  	})
    68  	s.EnableHTTPS(
    69  		gfile.Join("files", "server.crt"),
    70  		gfile.Join("files", "server.key"),
    71  	)
    72  	s.SetPort(p)
    73  	s.SetDumpRouterMap(false)
    74  	s.Start()
    75  	defer s.Shutdown()
    76  
    77  	time.Sleep(100 * time.Millisecond)
    78  
    79  	// HTTP
    80  	gtest.C(t, func(t *gtest.T) {
    81  		c := g.Client()
    82  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    83  		t.AssertIN(gstr.Trim(c.GetContent("/")), g.Slice{"", "Client sent an HTTP request to an HTTPS server."})
    84  		t.AssertIN(gstr.Trim(c.GetContent("/test")), g.Slice{"", "Client sent an HTTP request to an HTTPS server."})
    85  	})
    86  	// HTTPS
    87  	gtest.C(t, func(t *gtest.T) {
    88  		c := g.Client()
    89  		c.SetPrefix(fmt.Sprintf("https://127.0.0.1:%d", p))
    90  		t.Assert(c.GetContent("/"), "Not Found")
    91  		t.Assert(c.GetContent("/test"), "test")
    92  	})
    93  }
    94  
    95  func Test_HTTPS_HTTP_Basic(t *testing.T) {
    96  	var (
    97  		portHttp, _  = ports.PopRand()
    98  		portHttps, _ = ports.PopRand()
    99  	)
   100  	s := g.Server(gtime.TimestampNanoStr())
   101  	s.Group("/", func(group *ghttp.RouterGroup) {
   102  		group.GET("/test", func(r *ghttp.Request) {
   103  			r.Response.Write("test")
   104  		})
   105  	})
   106  	s.EnableHTTPS(
   107  		gdebug.TestDataPath("https", "files", "server.crt"),
   108  		gdebug.TestDataPath("https", "files", "server.key"),
   109  	)
   110  	s.SetPort(portHttp)
   111  	s.SetHTTPSPort(portHttps)
   112  	s.SetDumpRouterMap(false)
   113  	s.Start()
   114  	defer s.Shutdown()
   115  
   116  	time.Sleep(100 * time.Millisecond)
   117  
   118  	// HTTP
   119  	gtest.C(t, func(t *gtest.T) {
   120  		c := g.Client()
   121  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", portHttp))
   122  		t.Assert(c.GetContent("/"), "Not Found")
   123  		t.Assert(c.GetContent("/test"), "test")
   124  	})
   125  	// HTTPS
   126  	gtest.C(t, func(t *gtest.T) {
   127  		c := g.Client()
   128  		c.SetPrefix(fmt.Sprintf("https://127.0.0.1:%d", portHttps))
   129  		t.Assert(c.GetContent("/"), "Not Found")
   130  		t.Assert(c.GetContent("/test"), "test")
   131  	})
   132  }