github.com/wangyougui/gf/v2@v2.6.5/frame/gins/gins_z_unit_server_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 gins_test
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/wangyougui/gf/v2/frame/gins"
    15  	"github.com/wangyougui/gf/v2/internal/instance"
    16  	"github.com/wangyougui/gf/v2/net/ghttp"
    17  	"github.com/wangyougui/gf/v2/os/gcfg"
    18  	"github.com/wangyougui/gf/v2/os/gctx"
    19  	"github.com/wangyougui/gf/v2/os/gfile"
    20  	"github.com/wangyougui/gf/v2/test/gtest"
    21  )
    22  
    23  func Test_Server(t *testing.T) {
    24  	gtest.C(t, func(t *gtest.T) {
    25  		var (
    26  			path                = gcfg.DefaultConfigFileName
    27  			serverConfigContent = gtest.DataContent("server", "config.yaml")
    28  			err                 = gfile.PutContents(path, serverConfigContent)
    29  		)
    30  		t.AssertNil(err)
    31  		defer gfile.Remove(path)
    32  
    33  		instance.Clear()
    34  		defer instance.Clear()
    35  
    36  		s := gins.Server("tempByInstanceName")
    37  		s.BindHandler("/", func(r *ghttp.Request) {
    38  			r.Response.Write("hello")
    39  		})
    40  		s.SetDumpRouterMap(false)
    41  		s.Start()
    42  		defer s.Shutdown()
    43  
    44  		time.Sleep(100 * time.Millisecond)
    45  
    46  		prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
    47  		client := gins.HttpClient()
    48  		client.SetPrefix(prefix)
    49  		t.Assert(client.GetContent(gctx.New(), "/"), "hello")
    50  	})
    51  }