github.com/mendersoftware/go-lib-micro@v0.0.0-20240304135804-e8e39c59b148/rest_utils/paging_test.go (about)

     1  // Copyright 2023 Northern.tech AS
     2  //
     3  //    Licensed under the Apache License, Version 2.0 (the "License");
     4  //    you may not use this file except in compliance with the License.
     5  //    You may obtain a copy of the License at
     6  //
     7  //        http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //    Unless required by applicable law or agreed to in writing, software
    10  //    distributed under the License is distributed on an "AS IS" BASIS,
    11  //    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //    See the License for the specific language governing permissions and
    13  //    limitations under the License.
    14  
    15  package rest_utils
    16  
    17  import (
    18  	"net/http"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/ant0ine/go-json-rest/rest"
    23  	"github.com/ant0ine/go-json-rest/rest/test"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestParseQueryParmStr(t *testing.T) {
    28  
    29  	testCases := map[string]struct {
    30  		url      string
    31  		username string
    32  	}{
    33  		"test 1": {
    34  			url:      "/test?username=demo+user@mender.io",
    35  			username: "demo user@mender.io",
    36  		},
    37  		"test 2": {
    38  			url:      "/test?username=demo%2Buser@mender.io",
    39  			username: "demo+user@mender.io",
    40  		},
    41  		"test 3": {
    42  			url:      "/test?username=demo%2Fuser@mender.io",
    43  			username: "demo/user@mender.io",
    44  		},
    45  	}
    46  	for k, tc := range testCases {
    47  		t.Run(k, func(t *testing.T) {
    48  			httpReq := test.MakeSimpleRequest("POST", tc.url, "")
    49  			req := &rest.Request{Request: httpReq}
    50  
    51  			value, err := ParseQueryParmStr(req, "username", false, nil)
    52  			assert.Equal(t, tc.username, value)
    53  			assert.Nil(t, err)
    54  		})
    55  	}
    56  }
    57  
    58  func TestMakePageLinkHdrs(t *testing.T) {
    59  	testCases := []struct {
    60  		Name string
    61  
    62  		HasNext       bool
    63  		Page, PerPage uint64
    64  		Path          string
    65  
    66  		Expected []string
    67  	}{{
    68  		Name: "First page",
    69  
    70  		HasNext: true,
    71  		Path:    "/root",
    72  		Page:    1,
    73  		PerPage: 20,
    74  
    75  		Expected: []string{
    76  			`</root?page=2&per_page=20>; rel="next"`,
    77  			`</root?page=1&per_page=20>; rel="first"`,
    78  		},
    79  	}, {
    80  		Name: "Second page",
    81  
    82  		HasNext: true,
    83  		Path:    "/root/child",
    84  		Page:    3,
    85  		PerPage: 10,
    86  
    87  		Expected: []string{
    88  			`</root/child?page=1&per_page=10>; rel="first"`,
    89  			`</root/child?page=2&per_page=10>; rel="prev"`,
    90  			`</root/child?page=4&per_page=10>; rel="next"`,
    91  		},
    92  	}}
    93  
    94  	for _, tc := range testCases {
    95  		t.Run(tc.Name, func(t *testing.T) {
    96  			req, _ := http.NewRequest(
    97  				"GET",
    98  				"http://localhost/"+
    99  					strings.TrimPrefix(tc.Path, "/"),
   100  				nil,
   101  			)
   102  			res := MakePageLinkHdrs(
   103  				&rest.Request{Request: req},
   104  				tc.Page, tc.PerPage, tc.HasNext,
   105  			)
   106  			for _, link := range tc.Expected {
   107  				assert.Contains(t, res, link)
   108  			}
   109  		})
   110  	}
   111  }