github.com/gogf/gf/v2@v2.7.4/net/goai/goai_z_unit_issue_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 goai_test
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/gogf/gf/v2/encoding/gjson"
    16  	"github.com/gogf/gf/v2/frame/g"
    17  	"github.com/gogf/gf/v2/net/ghttp"
    18  	"github.com/gogf/gf/v2/test/gtest"
    19  )
    20  
    21  var ctx = context.Background()
    22  
    23  type Issue3664DefaultReq struct {
    24  	g.Meta `path:"/default" method:"post"`
    25  	Name   string
    26  }
    27  type Issue3664DefaultRes struct{}
    28  
    29  type Issue3664RequiredTagReq struct {
    30  	g.Meta `path:"/required-tag" required:"true" method:"post"`
    31  	Name   string
    32  }
    33  type Issue3664RequiredTagRes struct{}
    34  
    35  type Issue3664 struct{}
    36  
    37  func (Issue3664) Default(ctx context.Context, req *Issue3664DefaultReq) (res *Issue3664DefaultRes, err error) {
    38  	res = &Issue3664DefaultRes{}
    39  	return
    40  }
    41  
    42  func (Issue3664) RequiredTag(ctx context.Context, req *Issue3664RequiredTagReq) (res *Issue3664RequiredTagRes, err error) {
    43  	res = &Issue3664RequiredTagRes{}
    44  	return
    45  }
    46  
    47  // https://github.com/gogf/gf/issues/3664
    48  func Test_Issue3664(t *testing.T) {
    49  	gtest.C(t, func(t *gtest.T) {
    50  		s := g.Server()
    51  		s.Use(ghttp.MiddlewareHandlerResponse)
    52  		s.Group("/", func(group *ghttp.RouterGroup) {
    53  			group.Bind(
    54  				new(Issue3664),
    55  			)
    56  		})
    57  		s.SetLogger(nil)
    58  		s.SetOpenApiPath("/api.json")
    59  		s.SetDumpRouterMap(false)
    60  		s.Start()
    61  		defer s.Shutdown()
    62  		time.Sleep(100 * time.Millisecond)
    63  
    64  		c := g.Client()
    65  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    66  		apiContent := c.GetContent(ctx, "/api.json")
    67  		j, err := gjson.LoadJson(apiContent)
    68  		t.AssertNil(err)
    69  		t.Assert(j.Get(`paths./default.post.requestBody.required`).String(), "")
    70  		t.Assert(j.Get(`paths./required-tag.post.requestBody.required`).String(), "true")
    71  	})
    72  }