...

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

Documentation: github.com/ory/x/pagination

     1  /*
     2   * Copyright © 2017-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2017-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   */
    20  package pagination
    21  
    22  import (
    23  	"fmt"
    24  	"net/http"
    25  	"net/url"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestParse(t *testing.T) {
    32  	for _, tc := range []struct {
    33  		d   string
    34  		url string
    35  		dl  int
    36  		do  int
    37  		ml  int
    38  		el  int
    39  		eo  int
    40  	}{
    41  		{"normal", "http://localhost/foo?limit=10&offset=10", 0, 0, 120, 10, 10},
    42  		{"defaults", "http://localhost/foo", 5, 5, 10, 5, 5},
    43  		{"defaults_and_limits", "http://localhost/foo", 5, 5, 2, 2, 5},
    44  		{"limits", "http://localhost/foo?limit=10&offset=10", 0, 0, 5, 5, 10},
    45  		{"negatives", "http://localhost/foo?limit=-1&offset=-1", 0, 0, 5, 0, 0},
    46  		{"default_negatives", "http://localhost/foo", -1, -1, 5, 0, 0},
    47  		{"invalid_defaults", "http://localhost/foo?limit=a&offset=b", 10, 10, 15, 10, 10},
    48  	} {
    49  		t.Run(fmt.Sprintf("case=%s", tc.d), func(t *testing.T) {
    50  			u, _ := url.Parse(tc.url)
    51  			limit, offset := Parse(&http.Request{URL: u}, tc.dl, tc.do, tc.ml)
    52  			assert.EqualValues(t, limit, tc.el)
    53  			assert.EqualValues(t, offset, tc.eo)
    54  		})
    55  	}
    56  }
    57  

View as plain text