github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/pagination/testing/marker_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/huaweicloud/golangsdk/pagination"
    10  	"github.com/huaweicloud/golangsdk/testhelper"
    11  )
    12  
    13  // MarkerPager sample and test cases.
    14  
    15  type MarkerPageResult struct {
    16  	pagination.MarkerPageBase
    17  }
    18  
    19  func (r MarkerPageResult) IsEmpty() (bool, error) {
    20  	results, err := ExtractMarkerStrings(r)
    21  	if err != nil {
    22  		return true, err
    23  	}
    24  	return len(results) == 0, err
    25  }
    26  
    27  func (r MarkerPageResult) LastMarker() (string, error) {
    28  	results, err := ExtractMarkerStrings(r)
    29  	if err != nil {
    30  		return "", err
    31  	}
    32  	if len(results) == 0 {
    33  		return "", nil
    34  	}
    35  	return results[len(results)-1], nil
    36  }
    37  
    38  func createMarkerPaged(t *testing.T) pagination.Pager {
    39  	testhelper.SetupHTTP()
    40  
    41  	testhelper.Mux.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) {
    42  		r.ParseForm()
    43  		ms := r.Form["marker"]
    44  		switch {
    45  		case len(ms) == 0:
    46  			fmt.Fprintf(w, "aaa\nbbb\nccc")
    47  		case len(ms) == 1 && ms[0] == "ccc":
    48  			fmt.Fprintf(w, "ddd\neee\nfff")
    49  		case len(ms) == 1 && ms[0] == "fff":
    50  			fmt.Fprintf(w, "ggg\nhhh\niii")
    51  		case len(ms) == 1 && ms[0] == "iii":
    52  			w.WriteHeader(http.StatusNoContent)
    53  		default:
    54  			t.Errorf("Request with unexpected marker: [%v]", ms)
    55  		}
    56  	})
    57  
    58  	client := createClient()
    59  
    60  	createPage := func(r pagination.PageResult) pagination.Page {
    61  		p := MarkerPageResult{pagination.MarkerPageBase{PageResult: r}}
    62  		p.MarkerPageBase.Owner = p
    63  		return p
    64  	}
    65  
    66  	return pagination.NewPager(client, testhelper.Server.URL+"/page", createPage)
    67  }
    68  
    69  func ExtractMarkerStrings(page pagination.Page) ([]string, error) {
    70  	content := page.(MarkerPageResult).Body.([]uint8)
    71  	parts := strings.Split(string(content), "\n")
    72  	results := make([]string, 0, len(parts))
    73  	for _, part := range parts {
    74  		if len(part) > 0 {
    75  			results = append(results, part)
    76  		}
    77  	}
    78  	return results, nil
    79  }
    80  
    81  func TestEnumerateMarker(t *testing.T) {
    82  	pager := createMarkerPaged(t)
    83  	defer testhelper.TeardownHTTP()
    84  
    85  	callCount := 0
    86  	err := pager.EachPage(func(page pagination.Page) (bool, error) {
    87  		actual, err := ExtractMarkerStrings(page)
    88  		if err != nil {
    89  			return false, err
    90  		}
    91  
    92  		t.Logf("Handler invoked with %v", actual)
    93  
    94  		var expected []string
    95  		switch callCount {
    96  		case 0:
    97  			expected = []string{"aaa", "bbb", "ccc"}
    98  		case 1:
    99  			expected = []string{"ddd", "eee", "fff"}
   100  		case 2:
   101  			expected = []string{"ggg", "hhh", "iii"}
   102  		default:
   103  			t.Fatalf("Unexpected call count: %d", callCount)
   104  			return false, nil
   105  		}
   106  
   107  		testhelper.CheckDeepEquals(t, expected, actual)
   108  
   109  		callCount++
   110  		return true, nil
   111  	})
   112  	testhelper.AssertNoErr(t, err)
   113  	testhelper.AssertEquals(t, callCount, 3)
   114  }
   115  
   116  func TestAllPagesMarker(t *testing.T) {
   117  	pager := createMarkerPaged(t)
   118  	defer testhelper.TeardownHTTP()
   119  
   120  	page, err := pager.AllPages()
   121  	testhelper.AssertNoErr(t, err)
   122  
   123  	expected := []string{"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii"}
   124  	actual, err := ExtractMarkerStrings(page)
   125  	testhelper.AssertNoErr(t, err)
   126  	testhelper.CheckDeepEquals(t, expected, actual)
   127  }