github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/pagination/testing/linked_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
    11  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    12  )
    13  
    14  // LinkedPager sample and test cases.
    15  
    16  type LinkedPageResult struct {
    17  	pagination.LinkedPageBase
    18  }
    19  
    20  func (r LinkedPageResult) IsEmpty() (bool, error) {
    21  	is, err := ExtractLinkedInts(r)
    22  	return len(is) == 0, err
    23  }
    24  
    25  func ExtractLinkedInts(r pagination.Page) ([]int, error) {
    26  	var s struct {
    27  		Ints []int `json:"ints"`
    28  	}
    29  	err := (r.(LinkedPageResult)).ExtractInto(&s)
    30  	return s.Ints, err
    31  }
    32  
    33  func createLinked() pagination.Pager {
    34  	th.SetupHTTP()
    35  
    36  	th.Mux.HandleFunc("/page1", func(w http.ResponseWriter, r *http.Request) {
    37  		w.Header().Add("Content-Type", "application/json")
    38  		fmt.Fprintf(w, `{ "ints": [1, 2, 3], "links": { "next": "%s/page2" } }`, th.Server.URL)
    39  	})
    40  
    41  	th.Mux.HandleFunc("/page2", func(w http.ResponseWriter, r *http.Request) {
    42  		w.Header().Add("Content-Type", "application/json")
    43  		fmt.Fprintf(w, `{ "ints": [4, 5, 6], "links": { "next": "%s/page3" } }`, th.Server.URL)
    44  	})
    45  
    46  	th.Mux.HandleFunc("/page3", func(w http.ResponseWriter, r *http.Request) {
    47  		w.Header().Add("Content-Type", "application/json")
    48  		fmt.Fprint(w, `{ "ints": [7, 8, 9], "links": { "next": null } }`)
    49  	})
    50  
    51  	client := createClient()
    52  
    53  	createPage := func(r pagination.PageResult) pagination.Page {
    54  		return LinkedPageResult{pagination.LinkedPageBase{PageResult: r}}
    55  	}
    56  
    57  	return pagination.NewPager(client, th.Server.URL+"/page1", createPage)
    58  }
    59  
    60  func TestEnumerateLinked(t *testing.T) {
    61  	pager := createLinked()
    62  	defer th.TeardownHTTP()
    63  
    64  	callCount := 0
    65  	err := pager.EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
    66  		actual, err := ExtractLinkedInts(page)
    67  		if err != nil {
    68  			return false, err
    69  		}
    70  
    71  		t.Logf("Handler invoked with %v", actual)
    72  
    73  		var expected []int
    74  		switch callCount {
    75  		case 0:
    76  			expected = []int{1, 2, 3}
    77  		case 1:
    78  			expected = []int{4, 5, 6}
    79  		case 2:
    80  			expected = []int{7, 8, 9}
    81  		default:
    82  			t.Fatalf("Unexpected call count: %d", callCount)
    83  			return false, nil
    84  		}
    85  
    86  		if !reflect.DeepEqual(expected, actual) {
    87  			t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual)
    88  		}
    89  
    90  		callCount++
    91  		return true, nil
    92  	})
    93  	if err != nil {
    94  		t.Errorf("Unexpected error for page iteration: %v", err)
    95  	}
    96  
    97  	if callCount != 3 {
    98  		t.Errorf("Expected 3 calls, but was %d", callCount)
    99  	}
   100  }
   101  
   102  func TestAllPagesLinked(t *testing.T) {
   103  	pager := createLinked()
   104  	defer th.TeardownHTTP()
   105  
   106  	page, err := pager.AllPages(context.TODO())
   107  	th.AssertNoErr(t, err)
   108  
   109  	expected := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
   110  	actual, err := ExtractLinkedInts(page)
   111  	th.AssertNoErr(t, err)
   112  	th.CheckDeepEquals(t, expected, actual)
   113  }