github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_z_unit_feature_request_page_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  	"testing"
    12  	"time"
    13  
    14  	"github.com/gogf/gf/v2/frame/g"
    15  	"github.com/gogf/gf/v2/net/ghttp"
    16  	"github.com/gogf/gf/v2/test/gtest"
    17  	"github.com/gogf/gf/v2/util/guid"
    18  )
    19  
    20  func Test_Params_Page(t *testing.T) {
    21  	s := g.Server(guid.S())
    22  	s.Group("/", func(group *ghttp.RouterGroup) {
    23  		group.GET("/list", func(r *ghttp.Request) {
    24  			page := r.GetPage(5, 2)
    25  			r.Response.Write(page.GetContent(4))
    26  		})
    27  		group.GET("/list/{page}.html", func(r *ghttp.Request) {
    28  			page := r.GetPage(5, 2)
    29  			r.Response.Write(page.GetContent(4))
    30  		})
    31  	})
    32  	s.SetDumpRouterMap(false)
    33  	s.Start()
    34  	defer s.Shutdown()
    35  
    36  	time.Sleep(100 * time.Millisecond)
    37  	gtest.C(t, func(t *gtest.T) {
    38  		client := g.Client()
    39  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    40  
    41  		t.Assert(client.GetContent(ctx, "/list"), `<span class="GPageSpan">首页</span><span class="GPageSpan">上一页</span><span class="GPageSpan">1</span><a class="GPageLink" href="/list?page=2" title="2">2</a><a class="GPageLink" href="/list?page=3" title="3">3</a><a class="GPageLink" href="/list?page=2" title="">下一页</a><a class="GPageLink" href="/list?page=3" title="">尾页</a>`)
    42  		t.Assert(client.GetContent(ctx, "/list?page=3"), `<a class="GPageLink" href="/list?page=1" title="">首页</a><a class="GPageLink" href="/list?page=2" title="">上一页</a><a class="GPageLink" href="/list?page=1" title="1">1</a><a class="GPageLink" href="/list?page=2" title="2">2</a><span class="GPageSpan">3</span><span class="GPageSpan">下一页</span><span class="GPageSpan">尾页</span>`)
    43  
    44  		t.Assert(client.GetContent(ctx, "/list/1.html"), `<span class="GPageSpan">首页</span><span class="GPageSpan">上一页</span><span class="GPageSpan">1</span><a class="GPageLink" href="/list/2.html" title="2">2</a><a class="GPageLink" href="/list/3.html" title="3">3</a><a class="GPageLink" href="/list/2.html" title="">下一页</a><a class="GPageLink" href="/list/3.html" title="">尾页</a>`)
    45  		t.Assert(client.GetContent(ctx, "/list/3.html"), `<a class="GPageLink" href="/list/1.html" title="">首页</a><a class="GPageLink" href="/list/2.html" title="">上一页</a><a class="GPageLink" href="/list/1.html" title="1">1</a><a class="GPageLink" href="/list/2.html" title="2">2</a><span class="GPageSpan">3</span><span class="GPageSpan">下一页</span><span class="GPageSpan">尾页</span>`)
    46  	})
    47  }