...

Source file src/github.com/ory/x/pagination/header_test.go

Documentation: github.com/ory/x/pagination

     1  package pagination
     2  
     3  import (
     4  	"net/http/httptest"
     5  	"net/url"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestHeader(t *testing.T) {
    13  	u, err := url.Parse("http://example.com")
    14  	if err != nil {
    15  		t.Fatal(err)
    16  	}
    17  
    18  	t.Run("Create previous and first but not next or last if at the end", func(t *testing.T) {
    19  		r := httptest.NewRecorder()
    20  		Header(r, u, 120, 50, 100)
    21  
    22  		expect := strings.Join([]string{
    23  			"<http://example.com?limit=50&offset=0>; rel=\"first\"",
    24  			"<http://example.com?limit=50&offset=50>; rel=\"prev\"",
    25  		}, ",")
    26  
    27  		assert.EqualValues(t, expect, r.Result().Header.Get("Link"))
    28  		assert.EqualValues(t, "120", r.Result().Header.Get("X-Total-Count"))
    29  	})
    30  
    31  	t.Run("Create next and last, but not previous or first if at the beginning", func(t *testing.T) {
    32  		r := httptest.NewRecorder()
    33  		Header(r, u, 120, 50, 0)
    34  
    35  		expect := strings.Join([]string{
    36  			"<http://example.com?limit=50&offset=50>; rel=\"next\"",
    37  			"<http://example.com?limit=50&offset=100>; rel=\"last\"",
    38  		}, ",")
    39  
    40  		assert.EqualValues(t, expect, r.Result().Header.Get("Link"))
    41  	})
    42  
    43  	t.Run("Create next and last, but not previous or first if on the first page", func(t *testing.T) {
    44  		r := httptest.NewRecorder()
    45  		Header(r, u, 120, 50, 10)
    46  
    47  		expect := strings.Join([]string{
    48  			"<http://example.com?limit=50&offset=50>; rel=\"next\"",
    49  			"<http://example.com?limit=50&offset=100>; rel=\"last\"",
    50  		}, ",")
    51  
    52  		assert.EqualValues(t, expect, r.Result().Header.Get("Link"))
    53  	})
    54  
    55  	t.Run("Create previous, next, first, and last if in the middle", func(t *testing.T) {
    56  		r := httptest.NewRecorder()
    57  		Header(r, u, 300, 50, 150)
    58  
    59  		expect := strings.Join([]string{
    60  			"<http://example.com?limit=50&offset=0>; rel=\"first\"",
    61  			"<http://example.com?limit=50&offset=200>; rel=\"next\"",
    62  			"<http://example.com?limit=50&offset=100>; rel=\"prev\"",
    63  			"<http://example.com?limit=50&offset=250>; rel=\"last\"",
    64  		}, ",")
    65  
    66  		assert.EqualValues(t, expect, r.Result().Header.Get("Link"))
    67  	})
    68  
    69  	t.Run("Header should default limit to 1 no limit was provided", func(t *testing.T) {
    70  		r := httptest.NewRecorder()
    71  		Header(r, u, 100, 0, 20)
    72  
    73  		expect := strings.Join([]string{
    74  			"<http://example.com?limit=1&offset=0>; rel=\"first\"",
    75  			"<http://example.com?limit=1&offset=21>; rel=\"next\"",
    76  			"<http://example.com?limit=1&offset=19>; rel=\"prev\"",
    77  			"<http://example.com?limit=1&offset=99>; rel=\"last\"",
    78  		}, ",")
    79  
    80  		assert.EqualValues(t, expect, r.Result().Header.Get("Link"))
    81  	})
    82  
    83  	t.Run("Create previous, next, first, but not last if in the middle and no total was provided", func(t *testing.T) {
    84  		r := httptest.NewRecorder()
    85  		Header(r, u, 0, 50, 150)
    86  
    87  		expect := strings.Join([]string{
    88  			"<http://example.com?limit=50&offset=0>; rel=\"first\"",
    89  			"<http://example.com?limit=50&offset=200>; rel=\"next\"",
    90  			"<http://example.com?limit=50&offset=100>; rel=\"prev\"",
    91  		}, ",")
    92  
    93  		assert.EqualValues(t, expect, r.Result().Header.Get("Link"))
    94  	})
    95  
    96  	t.Run("Create only first if the limits provided exceeds the number of clients found", func(t *testing.T) {
    97  		r := httptest.NewRecorder()
    98  		Header(r, u, 5, 50, 0)
    99  
   100  		expect := "<http://example.com?limit=5&offset=0>; rel=\"first\""
   101  
   102  		assert.EqualValues(t, expect, r.Result().Header.Get("Link"))
   103  	})
   104  }
   105  

View as plain text