paging_test.go (5459B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package util_test 19 20 import ( 21 "testing" 22 23 "github.com/stretchr/testify/suite" 24 "github.com/superseriousbusiness/gotosocial/internal/config" 25 "github.com/superseriousbusiness/gotosocial/internal/util" 26 ) 27 28 type PagingSuite struct { 29 suite.Suite 30 } 31 32 func (suite *PagingSuite) TestPagingStandard() { 33 config.SetHost("example.org") 34 35 params := util.PageableResponseParams{ 36 Items: make([]interface{}, 10, 10), 37 Path: "/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses", 38 NextMaxIDValue: "01H11KA1DM2VH3747YDE7FV5HN", 39 PrevMinIDValue: "01H11KBBVRRDYYC5KEPME1NP5R", 40 Limit: 10, 41 } 42 43 resp, errWithCode := util.PackagePageableResponse(params) 44 if errWithCode != nil { 45 suite.FailNow(errWithCode.Error()) 46 } 47 48 suite.Equal(make([]interface{}, 10, 10), resp.Items) 49 suite.Equal(`<https://example.org/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses?limit=10&max_id=01H11KA1DM2VH3747YDE7FV5HN>; rel="next", <https://example.org/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses?limit=10&min_id=01H11KBBVRRDYYC5KEPME1NP5R>; rel="prev"`, resp.LinkHeader) 50 suite.Equal(`https://example.org/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses?limit=10&max_id=01H11KA1DM2VH3747YDE7FV5HN`, resp.NextLink) 51 suite.Equal(`https://example.org/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses?limit=10&min_id=01H11KBBVRRDYYC5KEPME1NP5R`, resp.PrevLink) 52 } 53 54 func (suite *PagingSuite) TestPagingNoLimit() { 55 config.SetHost("example.org") 56 57 params := util.PageableResponseParams{ 58 Items: make([]interface{}, 10, 10), 59 Path: "/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses", 60 NextMaxIDValue: "01H11KA1DM2VH3747YDE7FV5HN", 61 PrevMinIDValue: "01H11KBBVRRDYYC5KEPME1NP5R", 62 } 63 64 resp, errWithCode := util.PackagePageableResponse(params) 65 if errWithCode != nil { 66 suite.FailNow(errWithCode.Error()) 67 } 68 69 suite.Equal(make([]interface{}, 10, 10), resp.Items) 70 suite.Equal(`<https://example.org/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses?max_id=01H11KA1DM2VH3747YDE7FV5HN>; rel="next", <https://example.org/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses?min_id=01H11KBBVRRDYYC5KEPME1NP5R>; rel="prev"`, resp.LinkHeader) 71 suite.Equal(`https://example.org/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses?max_id=01H11KA1DM2VH3747YDE7FV5HN`, resp.NextLink) 72 suite.Equal(`https://example.org/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses?min_id=01H11KBBVRRDYYC5KEPME1NP5R`, resp.PrevLink) 73 } 74 75 func (suite *PagingSuite) TestPagingNoNextID() { 76 config.SetHost("example.org") 77 78 params := util.PageableResponseParams{ 79 Items: make([]interface{}, 10, 10), 80 Path: "/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses", 81 PrevMinIDValue: "01H11KBBVRRDYYC5KEPME1NP5R", 82 Limit: 10, 83 } 84 85 resp, errWithCode := util.PackagePageableResponse(params) 86 if errWithCode != nil { 87 suite.FailNow(errWithCode.Error()) 88 } 89 90 suite.Equal(make([]interface{}, 10, 10), resp.Items) 91 suite.Equal(`<https://example.org/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses?limit=10&min_id=01H11KBBVRRDYYC5KEPME1NP5R>; rel="prev"`, resp.LinkHeader) 92 suite.Equal(``, resp.NextLink) 93 suite.Equal(`https://example.org/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses?limit=10&min_id=01H11KBBVRRDYYC5KEPME1NP5R`, resp.PrevLink) 94 } 95 96 func (suite *PagingSuite) TestPagingNoPrevID() { 97 config.SetHost("example.org") 98 99 params := util.PageableResponseParams{ 100 Items: make([]interface{}, 10, 10), 101 Path: "/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses", 102 NextMaxIDValue: "01H11KA1DM2VH3747YDE7FV5HN", 103 Limit: 10, 104 } 105 106 resp, errWithCode := util.PackagePageableResponse(params) 107 if errWithCode != nil { 108 suite.FailNow(errWithCode.Error()) 109 } 110 111 suite.Equal(make([]interface{}, 10, 10), resp.Items) 112 suite.Equal(`<https://example.org/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses?limit=10&max_id=01H11KA1DM2VH3747YDE7FV5HN>; rel="next"`, resp.LinkHeader) 113 suite.Equal(`https://example.org/api/v1/accounts/01H11KA68PM4NNYJEG0FJQ90R3/statuses?limit=10&max_id=01H11KA1DM2VH3747YDE7FV5HN`, resp.NextLink) 114 suite.Equal(``, resp.PrevLink) 115 } 116 117 func (suite *PagingSuite) TestPagingNoItems() { 118 config.SetHost("example.org") 119 120 params := util.PageableResponseParams{ 121 NextMaxIDValue: "01H11KA1DM2VH3747YDE7FV5HN", 122 PrevMinIDValue: "01H11KBBVRRDYYC5KEPME1NP5R", 123 Limit: 10, 124 } 125 126 resp, errWithCode := util.PackagePageableResponse(params) 127 if errWithCode != nil { 128 suite.FailNow(errWithCode.Error()) 129 } 130 131 suite.Empty(resp.Items) 132 suite.Empty(resp.LinkHeader) 133 suite.Empty(resp.NextLink) 134 suite.Empty(resp.PrevLink) 135 } 136 137 func TestPagingSuite(t *testing.T) { 138 suite.Run(t, &PagingSuite{}) 139 }