github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/pagination/testing/info_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
    10  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
    11  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    12  )
    13  
    14  type InfoPageResult struct {
    15  	pagination.PageWithInfo
    16  }
    17  
    18  func (p InfoPageResult) IsEmpty() (bool, error) {
    19  	l, err := ExtractStructs(p)
    20  	if err != nil {
    21  		return false, err
    22  	}
    23  	return len(l) == 0, nil
    24  }
    25  
    26  type Struct struct {
    27  	ID string `json:"id"`
    28  }
    29  
    30  func ExtractStructs(p pagination.Page) ([]Struct, error) {
    31  	var structs []Struct
    32  	err := p.(InfoPageResult).ExtractIntoSlicePtr(&structs, "structs")
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	return structs, nil
    37  }
    38  
    39  var expectedPolicies = []Struct{
    40  	{
    41  		ID: tools.RandomString("plc-", 20),
    42  	},
    43  	{
    44  		ID: tools.RandomString("plc-", 20),
    45  	},
    46  	{
    47  		ID: tools.RandomString("plc-", 20),
    48  	},
    49  	{
    50  		ID: tools.RandomString("plc-", 20),
    51  	},
    52  }
    53  
    54  func createInfoPager(t *testing.T) pagination.Pager {
    55  	th.SetupHTTP()
    56  
    57  	th.Mux.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) {
    58  		_ = r.ParseForm()
    59  		ms := r.Form["marker"]
    60  		w.Header().Add("Content-Type", "application/json")
    61  		w.WriteHeader(http.StatusOK)
    62  		switch {
    63  		case len(ms) == 0:
    64  			allJson, _ := json.MarshalIndent(expectedPolicies[:2], "", "  ")
    65  			response := fmt.Sprintf(`
    66  {
    67    "page_info": {
    68      "next_marker": "%s"
    69    },
    70    "structs": %s
    71  }
    72  `, expectedPolicies[2].ID, allJson)
    73  			_, _ = fmt.Fprint(w, response)
    74  		case len(ms) == 1 && ms[0] == expectedPolicies[2].ID:
    75  			allJson, _ := json.MarshalIndent(expectedPolicies[2:], "", "  ")
    76  			response := fmt.Sprintf(`
    77  {
    78    "page_info": {
    79    },
    80    "structs": %s
    81  }
    82  `, allJson)
    83  			_, _ = fmt.Fprint(w, response)
    84  		default:
    85  			t.Errorf("Request with unexpected marker: [%v]", ms)
    86  		}
    87  	})
    88  
    89  	client := createClient()
    90  	return pagination.NewPager(client, th.Server.URL+"/page", func(r pagination.PageResult) pagination.Page {
    91  		return InfoPageResult{PageWithInfo: pagination.NewPageWithInfo(r)}
    92  	})
    93  }
    94  
    95  func TestInfoPageResult(t *testing.T) {
    96  	pager := createInfoPager(t)
    97  	defer th.TeardownHTTP()
    98  
    99  	callCount := 0
   100  	err := pager.EachPage(func(page pagination.Page) (bool, error) {
   101  		actual, err := ExtractStructs(page)
   102  		if err != nil {
   103  			return false, err
   104  		}
   105  		t.Logf("Handler invoked with %+v", actual)
   106  
   107  		switch callCount {
   108  		case 0:
   109  			th.AssertDeepEquals(t, actual, expectedPolicies[:2])
   110  		case 1:
   111  			th.AssertDeepEquals(t, actual, expectedPolicies[2:])
   112  		}
   113  
   114  		callCount += 1
   115  		return true, nil
   116  	})
   117  	th.AssertNoErr(t, err)
   118  	th.AssertEquals(t, 2, callCount)
   119  }
   120  
   121  func TestInfoPageAll(t *testing.T) {
   122  	pager := createInfoPager(t)
   123  	defer th.TeardownHTTP()
   124  
   125  	page, err := pager.AllPages()
   126  	th.AssertNoErr(t, err)
   127  	actual, err := ExtractStructs(page)
   128  	th.AssertNoErr(t, err)
   129  	th.AssertDeepEquals(t, expectedPolicies, actual)
   130  }