|
| 1 | +package microcms |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/url" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestClient_List(t *testing.T) { |
| 12 | + type Blog struct { |
| 13 | + ID string `json:"id"` |
| 14 | + Title string `json:"title"` |
| 15 | + } |
| 16 | + type BlogList struct { |
| 17 | + Contents []Blog |
| 18 | + TotalCount int |
| 19 | + Limit int |
| 20 | + Offset int |
| 21 | + } |
| 22 | + |
| 23 | + client := New("serviceDomain", "apiKey") |
| 24 | + client.SetHTTPClient(&httpClientMock{ |
| 25 | + t: t, |
| 26 | + method: http.MethodGet, |
| 27 | + url: &url.URL{ |
| 28 | + Scheme: "https", |
| 29 | + Host: "serviceDomain.microcms.io", |
| 30 | + Path: "/api/v1/blog", |
| 31 | + }, |
| 32 | + reqHeaders: map[string]string{"X-MICROCMS-API-KEY": "apiKey"}, |
| 33 | + resHeaders: map[string]string{"Content-Type": "application/json"}, |
| 34 | + resBody: ` |
| 35 | + { |
| 36 | + "contents": [ |
| 37 | + { |
| 38 | + "id": "foo", |
| 39 | + "title": "Hello, microCMS!", |
| 40 | + "createdAt": "2021-10-28T04:04:29.625Z", |
| 41 | + "updatedAt": "2021-10-28T04:04:29.625Z", |
| 42 | + "publishedAt": "2021-10-28T04:04:29.625Z", |
| 43 | + "revisedAt": "2021-10-28T04:04:29.625Z" |
| 44 | + } |
| 45 | + ], |
| 46 | + "totalCount": 1, |
| 47 | + "limit": 10, |
| 48 | + "offset": 0 |
| 49 | + } |
| 50 | + `, |
| 51 | + }) |
| 52 | + |
| 53 | + blog := BlogList{} |
| 54 | + err := client.List( |
| 55 | + ListParams{ |
| 56 | + Endpoint: "blog", |
| 57 | + }, |
| 58 | + &blog, |
| 59 | + ) |
| 60 | + require.NoError(t, err) |
| 61 | + require.EqualValues( |
| 62 | + t, |
| 63 | + BlogList{ |
| 64 | + Contents: []Blog{{ID: "foo", Title: "Hello, microCMS!"}}, |
| 65 | + TotalCount: 1, |
| 66 | + Limit: 10, |
| 67 | + Offset: 0, |
| 68 | + }, |
| 69 | + blog, |
| 70 | + ) |
| 71 | +} |
| 72 | + |
| 73 | +func TestClient_List_querystring(t *testing.T) { |
| 74 | + type Blog struct { |
| 75 | + ID string `json:"id"` |
| 76 | + Title string `json:"title"` |
| 77 | + } |
| 78 | + type BlogList struct { |
| 79 | + Contents []Blog |
| 80 | + TotalCount int |
| 81 | + Limit int |
| 82 | + Offset int |
| 83 | + } |
| 84 | + |
| 85 | + client := New("serviceDomain", "apiKey") |
| 86 | + client.SetHTTPClient(&httpClientMock{ |
| 87 | + t: t, |
| 88 | + method: http.MethodGet, |
| 89 | + url: &url.URL{ |
| 90 | + Scheme: "https", |
| 91 | + Host: "serviceDomain.microcms.io", |
| 92 | + Path: "/api/v1/blog", |
| 93 | + RawQuery: "depth=1&draftKey=abcd&fields=id%2Ctitle&filters=publishedAt%5Bgreater_than%5D2021-01-01&ids=foo&limit=100&offset=1&orders=createdAt&q=Hello", |
| 94 | + }, |
| 95 | + reqHeaders: map[string]string{"X-MICROCMS-API-KEY": "apiKey"}, |
| 96 | + resHeaders: map[string]string{"Content-Type": "application/json"}, |
| 97 | + resBody: ` |
| 98 | + { |
| 99 | + "contents": [ |
| 100 | + { |
| 101 | + "id": "foo", |
| 102 | + "title": "Hello, microCMS!", |
| 103 | + "createdAt": "2021-10-28T04:04:29.625Z", |
| 104 | + "updatedAt": "2021-10-28T04:04:29.625Z", |
| 105 | + "publishedAt": "2021-10-28T04:04:29.625Z", |
| 106 | + "revisedAt": "2021-10-28T04:04:29.625Z" |
| 107 | + } |
| 108 | + ], |
| 109 | + "totalCount": 1, |
| 110 | + "limit": 10, |
| 111 | + "offset": 0 |
| 112 | + } |
| 113 | + `, |
| 114 | + }) |
| 115 | + |
| 116 | + blog := BlogList{} |
| 117 | + err := client.List( |
| 118 | + ListParams{ |
| 119 | + Endpoint: "blog", |
| 120 | + DraftKey: "abcd", |
| 121 | + Limit: 100, |
| 122 | + Offset: 1, |
| 123 | + Orders: []string{"createdAt"}, |
| 124 | + Q: "Hello", |
| 125 | + Fields: []string{"id", "title"}, |
| 126 | + IDs: []string{"foo"}, |
| 127 | + Filters: "publishedAt[greater_than]2021-01-01", |
| 128 | + Depth: 1, |
| 129 | + }, |
| 130 | + &blog, |
| 131 | + ) |
| 132 | + require.NoError(t, err) |
| 133 | + require.EqualValues( |
| 134 | + t, |
| 135 | + BlogList{ |
| 136 | + Contents: []Blog{{ID: "foo", Title: "Hello, microCMS!"}}, |
| 137 | + TotalCount: 1, |
| 138 | + Limit: 10, |
| 139 | + Offset: 0, |
| 140 | + }, |
| 141 | + blog, |
| 142 | + ) |
| 143 | +} |
| 144 | + |
| 145 | +func TestClient_Get(t *testing.T) { |
| 146 | + type Blog struct { |
| 147 | + ID string `json:"id"` |
| 148 | + Title string `json:"title"` |
| 149 | + } |
| 150 | + |
| 151 | + client := New("serviceDomain", "apiKey") |
| 152 | + client.SetHTTPClient(&httpClientMock{ |
| 153 | + t: t, |
| 154 | + method: http.MethodGet, |
| 155 | + url: &url.URL{ |
| 156 | + Scheme: "https", |
| 157 | + Host: "serviceDomain.microcms.io", |
| 158 | + Path: "/api/v1/blog/foo", |
| 159 | + }, |
| 160 | + reqHeaders: map[string]string{"X-MICROCMS-API-KEY": "apiKey"}, |
| 161 | + resHeaders: map[string]string{"Content-Type": "application/json"}, |
| 162 | + resBody: ` |
| 163 | + { |
| 164 | + "id": "foo", |
| 165 | + "title": "Hello, microCMS!", |
| 166 | + "createdAt": "2021-10-28T04:04:29.625Z", |
| 167 | + "updatedAt": "2021-10-28T04:04:29.625Z", |
| 168 | + "publishedAt": "2021-10-28T04:04:29.625Z", |
| 169 | + "revisedAt": "2021-10-28T04:04:29.625Z" |
| 170 | + } |
| 171 | + `, |
| 172 | + }) |
| 173 | + |
| 174 | + blog := Blog{} |
| 175 | + err := client.Get( |
| 176 | + GetParams{ |
| 177 | + Endpoint: "blog", |
| 178 | + ContentID: "foo", |
| 179 | + }, |
| 180 | + &blog, |
| 181 | + ) |
| 182 | + require.NoError(t, err) |
| 183 | + require.EqualValues( |
| 184 | + t, Blog{ |
| 185 | + ID: "foo", |
| 186 | + Title: "Hello, microCMS!", |
| 187 | + }, |
| 188 | + blog, |
| 189 | + ) |
| 190 | +} |
| 191 | + |
| 192 | +func TestClient_Get_querystring(t *testing.T) { |
| 193 | + type Blog struct { |
| 194 | + ID string `json:"id"` |
| 195 | + Title string `json:"title"` |
| 196 | + } |
| 197 | + |
| 198 | + client := New("serviceDomain", "apiKey") |
| 199 | + client.SetHTTPClient(&httpClientMock{ |
| 200 | + t: t, |
| 201 | + method: http.MethodGet, |
| 202 | + url: &url.URL{ |
| 203 | + Scheme: "https", |
| 204 | + Host: "serviceDomain.microcms.io", |
| 205 | + Path: "/api/v1/blog/foo", |
| 206 | + RawQuery: "depth=1&draftKey=abcd&fields=id%2Ctitle", |
| 207 | + }, |
| 208 | + reqHeaders: map[string]string{"X-MICROCMS-API-KEY": "apiKey"}, |
| 209 | + resHeaders: map[string]string{"Content-Type": "application/json"}, |
| 210 | + resBody: ` |
| 211 | + { |
| 212 | + "id": "foo", |
| 213 | + "title": "Hello, microCMS!", |
| 214 | + "createdAt": "2021-10-28T04:04:29.625Z", |
| 215 | + "updatedAt": "2021-10-28T04:04:29.625Z", |
| 216 | + "publishedAt": "2021-10-28T04:04:29.625Z", |
| 217 | + "revisedAt": "2021-10-28T04:04:29.625Z" |
| 218 | + } |
| 219 | + `, |
| 220 | + }) |
| 221 | + |
| 222 | + blog := Blog{} |
| 223 | + err := client.Get( |
| 224 | + GetParams{ |
| 225 | + Endpoint: "blog", |
| 226 | + ContentID: "foo", |
| 227 | + DraftKey: "abcd", |
| 228 | + Fields: []string{"id", "title"}, |
| 229 | + Depth: 1, |
| 230 | + }, |
| 231 | + &blog, |
| 232 | + ) |
| 233 | + require.NoError(t, err) |
| 234 | + require.EqualValues( |
| 235 | + t, Blog{ |
| 236 | + ID: "foo", |
| 237 | + Title: "Hello, microCMS!", |
| 238 | + }, |
| 239 | + blog, |
| 240 | + ) |
| 241 | +} |
0 commit comments