|
1 | 1 | package config_helpers
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/base64" |
4 | 5 | "io/ioutil"
|
| 6 | + gourl "net/url" |
5 | 7 | "os"
|
6 | 8 | "path/filepath"
|
7 | 9 | "strings"
|
@@ -103,3 +105,62 @@ func TestConfigDir_IbmCloudConfigHomeSet_Exists(t *testing.T) {
|
103 | 105 | os.Setenv("IBMCLOUD_CONFIG_HOME", userHome)
|
104 | 106 | assert.Equal(userHome, ConfigDir())
|
105 | 107 | }
|
| 108 | + |
| 109 | +func TestIsValidPaginationNextURL(t *testing.T) { |
| 110 | + assert := assert.New(t) |
| 111 | + |
| 112 | + testCases := []struct { |
| 113 | + name string |
| 114 | + nextURL string |
| 115 | + encodedQueryParam string |
| 116 | + expectedQueries gourl.Values |
| 117 | + isValid bool |
| 118 | + }{ |
| 119 | + { |
| 120 | + name: "return true for matching expected queries in pagination url", |
| 121 | + nextURL: "/api/example?cursor=" + base64.RawURLEncoding.EncodeToString([]byte("limit=100&active=true")), |
| 122 | + encodedQueryParam: "cursor", |
| 123 | + expectedQueries: gourl.Values{ |
| 124 | + "limit": []string{"100"}, |
| 125 | + "active": []string{"true"}, |
| 126 | + }, |
| 127 | + isValid: true, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "return true for matching expected queries with extraneous queries in pagination url", |
| 131 | + nextURL: "/api/example?cursor=" + base64.RawURLEncoding.EncodeToString([]byte("limit=100&active=true&extra=foo")), |
| 132 | + encodedQueryParam: "cursor", |
| 133 | + expectedQueries: gourl.Values{ |
| 134 | + "limit": []string{"100"}, |
| 135 | + "active": []string{"true"}, |
| 136 | + }, |
| 137 | + isValid: true, |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "return false for different limit in pagination url", |
| 141 | + nextURL: "/api/example?cursor=" + base64.RawURLEncoding.EncodeToString([]byte("limit=200")), |
| 142 | + encodedQueryParam: "cursor", |
| 143 | + expectedQueries: gourl.Values{ |
| 144 | + "limit": []string{"100"}, |
| 145 | + }, |
| 146 | + isValid: false, |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "return false for different query among multiple parameters in the pagination url", |
| 150 | + nextURL: "/api/example?cursor=" + base64.RawURLEncoding.EncodeToString([]byte("limit=100&active=true")), |
| 151 | + encodedQueryParam: "cursor", |
| 152 | + expectedQueries: gourl.Values{ |
| 153 | + "limit": []string{"100"}, |
| 154 | + "active": []string{"false"}, |
| 155 | + }, |
| 156 | + isValid: false, |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + for _, tc := range testCases { |
| 161 | + t.Run(tc.name, func(_ *testing.T) { |
| 162 | + isValid := IsValidPaginationNextURL(tc.nextURL, tc.encodedQueryParam, tc.expectedQueries) |
| 163 | + assert.Equal(tc.isValid, isValid) |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
0 commit comments