github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_openapi_swagger_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  	"context"
    11  	"fmt"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/wangyougui/gf/v2/errors/gerror"
    16  	"github.com/wangyougui/gf/v2/frame/g"
    17  	"github.com/wangyougui/gf/v2/net/ghttp"
    18  	"github.com/wangyougui/gf/v2/test/gtest"
    19  	"github.com/wangyougui/gf/v2/text/gstr"
    20  	"github.com/wangyougui/gf/v2/util/gmeta"
    21  	"github.com/wangyougui/gf/v2/util/guid"
    22  )
    23  
    24  func Test_OpenApi_Swagger(t *testing.T) {
    25  	type TestReq struct {
    26  		gmeta.Meta `method:"get" summary:"Test summary" tags:"Test"`
    27  		Age        int
    28  		Name       string
    29  	}
    30  	type TestRes struct {
    31  		Id   int
    32  		Age  int
    33  		Name string
    34  	}
    35  	s := g.Server(guid.S())
    36  	s.SetSwaggerPath("/swagger")
    37  	s.SetOpenApiPath("/api.json")
    38  	s.Use(ghttp.MiddlewareHandlerResponse)
    39  	s.BindHandler("/test", func(ctx context.Context, req *TestReq) (res *TestRes, err error) {
    40  		return &TestRes{
    41  			Id:   1,
    42  			Age:  req.Age,
    43  			Name: req.Name,
    44  		}, nil
    45  	})
    46  	s.BindHandler("/test/error", func(ctx context.Context, req *TestReq) (res *TestRes, err error) {
    47  		return &TestRes{
    48  			Id:   1,
    49  			Age:  req.Age,
    50  			Name: req.Name,
    51  		}, gerror.New("error")
    52  	})
    53  	s.SetDumpRouterMap(false)
    54  	s.Start()
    55  	defer s.Shutdown()
    56  
    57  	time.Sleep(100 * time.Millisecond)
    58  	gtest.C(t, func(t *gtest.T) {
    59  		c := g.Client()
    60  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    61  
    62  		t.Assert(c.GetContent(ctx, "/test?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18,"Name":"john"}}`)
    63  		t.Assert(c.GetContent(ctx, "/test/error"), `{"code":50,"message":"error","data":{"Id":1,"Age":0,"Name":""}}`)
    64  
    65  		t.Assert(gstr.Contains(c.GetContent(ctx, "/swagger/"), `API Reference`), true)
    66  		t.Assert(gstr.Contains(c.GetContent(ctx, "/api.json"), `/test/error`), true)
    67  	})
    68  }
    69  
    70  func Test_OpenApi_Multiple_Methods_Swagger(t *testing.T) {
    71  	type TestReq struct {
    72  		gmeta.Meta `method:"get,post" summary:"Test summary" tags:"Test"`
    73  		Age        int
    74  		Name       string
    75  	}
    76  	type TestRes struct {
    77  		Id   int
    78  		Age  int
    79  		Name string
    80  	}
    81  	s := g.Server(guid.S())
    82  	s.SetSwaggerPath("/swagger")
    83  	s.SetOpenApiPath("/api.json")
    84  	s.Use(ghttp.MiddlewareHandlerResponse)
    85  	s.BindHandler("/test", func(ctx context.Context, req *TestReq) (res *TestRes, err error) {
    86  		return &TestRes{
    87  			Id:   1,
    88  			Age:  req.Age,
    89  			Name: req.Name,
    90  		}, nil
    91  	})
    92  	s.BindHandler("/test/error", func(ctx context.Context, req *TestReq) (res *TestRes, err error) {
    93  		return &TestRes{
    94  			Id:   1,
    95  			Age:  req.Age,
    96  			Name: req.Name,
    97  		}, gerror.New("error")
    98  	})
    99  	s.SetDumpRouterMap(false)
   100  	s.Start()
   101  	defer s.Shutdown()
   102  
   103  	time.Sleep(100 * time.Millisecond)
   104  	gtest.C(t, func(t *gtest.T) {
   105  		openapi := s.GetOpenApi()
   106  		t.AssertNE(openapi.Paths["/test"].Get, nil)
   107  		t.AssertNE(openapi.Paths["/test"].Post, nil)
   108  		t.AssertNE(openapi.Paths["/test/error"].Get, nil)
   109  		t.AssertNE(openapi.Paths["/test/error"].Post, nil)
   110  
   111  		t.Assert(len(openapi.Paths["/test"].Get.Parameters), 2)
   112  		t.Assert(len(openapi.Paths["/test/error"].Get.Parameters), 2)
   113  		t.Assert(len(openapi.Components.Schemas.Get(`github.com.gogf.gf.v2.net.ghttp_test.TestReq`).Value.Properties.Map()), 2)
   114  
   115  		c := g.Client()
   116  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   117  
   118  		// Only works on GET & POST methods.
   119  		t.Assert(c.GetContent(ctx, "/test?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18,"Name":"john"}}`)
   120  		t.Assert(c.GetContent(ctx, "/test/error"), `{"code":50,"message":"error","data":{"Id":1,"Age":0,"Name":""}}`)
   121  		t.Assert(c.PostContent(ctx, "/test?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18,"Name":"john"}}`)
   122  		t.Assert(c.PostContent(ctx, "/test/error"), `{"code":50,"message":"error","data":{"Id":1,"Age":0,"Name":""}}`)
   123  
   124  		// Not works on other methods.
   125  		t.Assert(c.PutContent(ctx, "/test?age=18&name=john"), `{"code":65,"message":"Not Found","data":null}`)
   126  		t.Assert(c.PutContent(ctx, "/test/error"), `{"code":65,"message":"Not Found","data":null}`)
   127  
   128  		t.Assert(gstr.Contains(c.GetContent(ctx, "/swagger/"), `API Reference`), true)
   129  		t.Assert(gstr.Contains(c.GetContent(ctx, "/api.json"), `/test/error`), true)
   130  	})
   131  }
   132  
   133  func Test_OpenApi_Method_All_Swagger(t *testing.T) {
   134  	type TestReq struct {
   135  		gmeta.Meta `method:"all" summary:"Test summary" tags:"Test"`
   136  		Age        int
   137  		Name       string
   138  	}
   139  	type TestRes struct {
   140  		Id   int
   141  		Age  int
   142  		Name string
   143  	}
   144  	s := g.Server(guid.S())
   145  	s.SetSwaggerPath("/swagger")
   146  	s.SetOpenApiPath("/api.json")
   147  	s.Use(ghttp.MiddlewareHandlerResponse)
   148  	s.BindHandler("/test", func(ctx context.Context, req *TestReq) (res *TestRes, err error) {
   149  		return &TestRes{
   150  			Id:   1,
   151  			Age:  req.Age,
   152  			Name: req.Name,
   153  		}, nil
   154  	})
   155  	s.BindHandler("/test/error", func(ctx context.Context, req *TestReq) (res *TestRes, err error) {
   156  		return &TestRes{
   157  			Id:   1,
   158  			Age:  req.Age,
   159  			Name: req.Name,
   160  		}, gerror.New("error")
   161  	})
   162  	s.SetDumpRouterMap(false)
   163  	s.Start()
   164  	defer s.Shutdown()
   165  
   166  	time.Sleep(100 * time.Millisecond)
   167  	gtest.C(t, func(t *gtest.T) {
   168  		openapi := s.GetOpenApi()
   169  		t.AssertNE(openapi.Paths["/test"].Get, nil)
   170  		t.AssertNE(openapi.Paths["/test"].Post, nil)
   171  		t.AssertNE(openapi.Paths["/test"].Delete, nil)
   172  		t.AssertNE(openapi.Paths["/test/error"].Get, nil)
   173  		t.AssertNE(openapi.Paths["/test/error"].Post, nil)
   174  		t.AssertNE(openapi.Paths["/test/error"].Delete, nil)
   175  
   176  		c := g.Client()
   177  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   178  
   179  		t.Assert(c.GetContent(ctx, "/test?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18,"Name":"john"}}`)
   180  		t.Assert(c.GetContent(ctx, "/test/error"), `{"code":50,"message":"error","data":{"Id":1,"Age":0,"Name":""}}`)
   181  		t.Assert(c.PostContent(ctx, "/test?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18,"Name":"john"}}`)
   182  		t.Assert(c.PostContent(ctx, "/test/error"), `{"code":50,"message":"error","data":{"Id":1,"Age":0,"Name":""}}`)
   183  
   184  		t.Assert(gstr.Contains(c.GetContent(ctx, "/swagger/"), `API Reference`), true)
   185  		t.Assert(gstr.Contains(c.GetContent(ctx, "/api.json"), `/test/error`), true)
   186  	})
   187  }