github.com/hamba/avro@v1.8.0/registry/client_test.go (about) 1 package registry_test 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 8 "github.com/hamba/avro/registry" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestNewClient(t *testing.T) { 13 client, err := registry.NewClient("http://example.com") 14 15 assert.NoError(t, err) 16 assert.Implements(t, (*registry.Registry)(nil), client) 17 } 18 19 func TestNewClient_UrlError(t *testing.T) { 20 _, err := registry.NewClient("://") 21 22 assert.Error(t, err) 23 } 24 25 func TestClient_PopulatesError(t *testing.T) { 26 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 27 w.Header().Add("Content-Type", "application/vnd.schemaregistry.v1+json") 28 w.WriteHeader(422) 29 _, _ = w.Write([]byte(`{"error_code": 42202, "message": "schema may not be empty"}"`)) 30 })) 31 defer s.Close() 32 client, _ := registry.NewClient(s.URL) 33 34 _, _, err := client.CreateSchema("test", "") 35 36 assert.Error(t, err) 37 assert.IsType(t, registry.Error{}, err) 38 assert.Equal(t, "schema may not be empty", err.Error()) 39 regError := err.(registry.Error) 40 assert.Equal(t, 422, regError.StatusCode) 41 assert.Equal(t, 42202, regError.Code) 42 assert.Equal(t, "schema may not be empty", regError.Message) 43 } 44 45 func TestNewClient_BasicAuth(t *testing.T) { 46 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 47 username, password, ok := r.BasicAuth() 48 assert.True(t, ok) 49 assert.Equal(t, "username", username) 50 assert.Equal(t, "password", password) 51 52 _, _ = w.Write([]byte(`[]`)) 53 })) 54 defer s.Close() 55 client, _ := registry.NewClient(s.URL, registry.WithBasicAuth("username", "password")) 56 57 _, err := client.GetSubjects() 58 59 assert.NoError(t, err) 60 } 61 62 func TestClient_GetSchema(t *testing.T) { 63 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 64 assert.Equal(t, "GET", r.Method) 65 assert.Equal(t, "/schemas/ids/5", r.URL.Path) 66 67 _, _ = w.Write([]byte(`{"schema":"[\"null\",\"string\",\"int\"]"}`)) 68 })) 69 defer s.Close() 70 client, _ := registry.NewClient(s.URL) 71 72 schema, err := client.GetSchema(5) 73 74 assert.NoError(t, err) 75 assert.Equal(t, `["null","string","int"]`, schema.String()) 76 } 77 78 func TestClient_GetSchemaCachesSchema(t *testing.T) { 79 count := 0 80 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 81 count++ 82 _, _ = w.Write([]byte(`{"schema":"[\"null\",\"string\",\"int\"]"}`)) 83 })) 84 defer s.Close() 85 client, _ := registry.NewClient(s.URL) 86 87 _, _ = client.GetSchema(5) 88 89 _, _ = client.GetSchema(5) 90 91 assert.Equal(t, 1, count) 92 } 93 94 func TestClient_GetSchemaRequestError(t *testing.T) { 95 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 96 w.WriteHeader(500) 97 })) 98 defer s.Close() 99 client, _ := registry.NewClient(s.URL) 100 101 _, err := client.GetSchema(5) 102 103 assert.Error(t, err) 104 } 105 106 func TestClient_GetSchemaJsonError(t *testing.T) { 107 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 108 _, _ = w.Write([]byte(`{"schema":"[\"null\",\"string\",\"int\"]"`)) 109 })) 110 defer s.Close() 111 client, _ := registry.NewClient(s.URL) 112 113 _, err := client.GetSchema(5) 114 115 assert.Error(t, err) 116 } 117 118 func TestClient_GetSchemaSchemaError(t *testing.T) { 119 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 120 _, _ = w.Write([]byte(`{"schema":""}`)) 121 })) 122 defer s.Close() 123 client, _ := registry.NewClient(s.URL) 124 125 _, err := client.GetSchema(5) 126 127 assert.Error(t, err) 128 } 129 130 func TestClient_GetSubjects(t *testing.T) { 131 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 132 assert.Equal(t, "GET", r.Method) 133 assert.Equal(t, "/subjects", r.URL.Path) 134 135 _, _ = w.Write([]byte(`["foobar"]`)) 136 })) 137 defer s.Close() 138 client, _ := registry.NewClient(s.URL) 139 140 subs, err := client.GetSubjects() 141 142 assert.NoError(t, err) 143 assert.Len(t, subs, 1) 144 assert.Equal(t, "foobar", subs[0]) 145 } 146 147 func TestClient_GetSubjectsRequestError(t *testing.T) { 148 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 149 w.WriteHeader(500) 150 })) 151 defer s.Close() 152 client, _ := registry.NewClient(s.URL) 153 154 _, err := client.GetSubjects() 155 156 assert.Error(t, err) 157 } 158 159 func TestClient_GetSubjectsJSONError(t *testing.T) { 160 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 161 _, _ = w.Write([]byte(`["foobar"`)) 162 })) 163 defer s.Close() 164 client, _ := registry.NewClient(s.URL) 165 166 _, err := client.GetSubjects() 167 168 assert.Error(t, err) 169 } 170 171 func TestClient_GetVersions(t *testing.T) { 172 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 173 assert.Equal(t, "GET", r.Method) 174 assert.Equal(t, "/subjects/foobar/versions", r.URL.Path) 175 176 _, _ = w.Write([]byte(`[10]`)) 177 })) 178 defer s.Close() 179 client, _ := registry.NewClient(s.URL) 180 181 vers, err := client.GetVersions("foobar") 182 183 assert.NoError(t, err) 184 assert.Len(t, vers, 1) 185 assert.Equal(t, 10, vers[0]) 186 } 187 188 func TestClient_GetVersionsRequestError(t *testing.T) { 189 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 190 w.WriteHeader(500) 191 })) 192 defer s.Close() 193 client, _ := registry.NewClient(s.URL) 194 195 _, err := client.GetVersions("foobar") 196 197 assert.Error(t, err) 198 } 199 200 func TestClient_GetVersionsJSONError(t *testing.T) { 201 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 202 _, _ = w.Write([]byte(`[10`)) 203 })) 204 defer s.Close() 205 client, _ := registry.NewClient(s.URL) 206 207 _, err := client.GetVersions("foobar") 208 209 assert.Error(t, err) 210 } 211 212 func TestClient_GetSchemaByVersion(t *testing.T) { 213 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 214 assert.Equal(t, "GET", r.Method) 215 assert.Equal(t, "/subjects/foobar/versions/5", r.URL.Path) 216 217 _, _ = w.Write([]byte(`{"schema":"[\"null\",\"string\",\"int\"]"}`)) 218 })) 219 defer s.Close() 220 client, _ := registry.NewClient(s.URL) 221 222 schema, err := client.GetSchemaByVersion("foobar", 5) 223 224 assert.NoError(t, err) 225 assert.Equal(t, `["null","string","int"]`, schema.String()) 226 } 227 228 func TestClient_GetSchemaByVersionRequestError(t *testing.T) { 229 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 230 w.WriteHeader(500) 231 })) 232 defer s.Close() 233 client, _ := registry.NewClient(s.URL) 234 235 _, err := client.GetSchemaByVersion("foobar", 5) 236 237 assert.Error(t, err) 238 } 239 240 func TestClient_GetSchemaByVersionJsonError(t *testing.T) { 241 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 242 _, _ = w.Write([]byte(`{"schema":"[\"null\",\"string\",\"int\"]"`)) 243 })) 244 defer s.Close() 245 client, _ := registry.NewClient(s.URL) 246 247 _, err := client.GetSchemaByVersion("foobar", 5) 248 249 assert.Error(t, err) 250 } 251 252 func TestClient_GetSchemaByVersionSchemaError(t *testing.T) { 253 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 254 _, _ = w.Write([]byte(`{"schema":""}`)) 255 })) 256 defer s.Close() 257 client, _ := registry.NewClient(s.URL) 258 259 _, err := client.GetSchemaByVersion("foobar", 5) 260 261 assert.Error(t, err) 262 } 263 264 func TestClient_GetLatestSchema(t *testing.T) { 265 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 266 assert.Equal(t, "GET", r.Method) 267 assert.Equal(t, "/subjects/foobar/versions/latest", r.URL.Path) 268 269 _, _ = w.Write([]byte(`{"schema":"[\"null\",\"string\",\"int\"]"}`)) 270 })) 271 defer s.Close() 272 client, _ := registry.NewClient(s.URL) 273 274 schema, err := client.GetLatestSchema("foobar") 275 276 assert.NoError(t, err) 277 assert.Equal(t, `["null","string","int"]`, schema.String()) 278 } 279 280 func TestClient_GetLatestSchemaRequestError(t *testing.T) { 281 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 282 w.WriteHeader(500) 283 })) 284 defer s.Close() 285 client, _ := registry.NewClient(s.URL) 286 287 _, err := client.GetLatestSchema("foobar") 288 289 assert.Error(t, err) 290 } 291 292 func TestClient_GetLatestSchemaJsonError(t *testing.T) { 293 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 294 _, _ = w.Write([]byte(`{"schema":"[\"null\",\"string\",\"int\"]"`)) 295 })) 296 defer s.Close() 297 client, _ := registry.NewClient(s.URL) 298 299 _, err := client.GetLatestSchema("foobar") 300 301 assert.Error(t, err) 302 } 303 304 func TestClient_GetLatestSchemaSchemaError(t *testing.T) { 305 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 306 _, _ = w.Write([]byte(`{"schema":""}`)) 307 })) 308 defer s.Close() 309 client, _ := registry.NewClient(s.URL) 310 311 _, err := client.GetLatestSchema("foobar") 312 313 assert.Error(t, err) 314 } 315 316 func TestClient_GetLatestSchemaInfo(t *testing.T) { 317 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 318 assert.Equal(t, "GET", r.Method) 319 assert.Equal(t, "/subjects/foobar/versions/latest", r.URL.Path) 320 321 _, _ = w.Write([]byte(`{"subject": "foobar", "version": 1, "id": 2, "schema":"[\"null\",\"string\",\"int\"]"}`)) 322 })) 323 defer s.Close() 324 client, _ := registry.NewClient(s.URL) 325 326 schemaInfo, err := client.GetLatestSchemaInfo("foobar") 327 328 assert.NoError(t, err) 329 assert.Equal(t, `["null","string","int"]`, schemaInfo.Schema.String()) 330 assert.Equal(t, 2, schemaInfo.ID) 331 assert.Equal(t, 1, schemaInfo.Version) 332 } 333 334 func TestClient_GetLatestSchemaInfoRequestError(t *testing.T) { 335 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 336 w.WriteHeader(500) 337 })) 338 defer s.Close() 339 client, _ := registry.NewClient(s.URL) 340 341 _, err := client.GetLatestSchemaInfo("foobar") 342 343 assert.Error(t, err) 344 } 345 346 func TestClient_GetLatestSchemaInfoJsonError(t *testing.T) { 347 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 348 _, _ = w.Write([]byte(`{"subject": "foobar", "version": 1, "id": 2, "schema":"[\"null\",\"string\",\"int\"]"`)) 349 })) 350 defer s.Close() 351 client, _ := registry.NewClient(s.URL) 352 353 _, err := client.GetLatestSchemaInfo("foobar") 354 355 assert.Error(t, err) 356 } 357 358 func TestClient_GetLatestSchemaInfoSchemaError(t *testing.T) { 359 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 360 _, _ = w.Write([]byte(`{"schema":""}`)) 361 })) 362 defer s.Close() 363 client, _ := registry.NewClient(s.URL) 364 365 _, err := client.GetLatestSchemaInfo("foobar") 366 367 assert.Error(t, err) 368 } 369 370 func TestClient_CreateSchema(t *testing.T) { 371 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 372 assert.Equal(t, "POST", r.Method) 373 assert.Equal(t, "/subjects/foobar/versions", r.URL.Path) 374 375 _, _ = w.Write([]byte(`{"id":10}`)) 376 })) 377 defer s.Close() 378 client, _ := registry.NewClient(s.URL) 379 380 id, schema, err := client.CreateSchema("foobar", "[\"null\",\"string\",\"int\"]") 381 382 assert.NoError(t, err) 383 assert.Equal(t, 10, id) 384 assert.Equal(t, `["null","string","int"]`, schema.String()) 385 } 386 387 func TestClient_CreateSchemaRequestError(t *testing.T) { 388 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 389 w.WriteHeader(500) 390 })) 391 defer s.Close() 392 client, _ := registry.NewClient(s.URL) 393 394 _, _, err := client.CreateSchema("foobar", "[\"null\",\"string\",\"int\"]") 395 396 assert.Error(t, err) 397 } 398 399 func TestClient_CreateSchemaJsonError(t *testing.T) { 400 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 401 _, _ = w.Write([]byte(`{"id":10`)) 402 })) 403 defer s.Close() 404 client, _ := registry.NewClient(s.URL) 405 406 _, _, err := client.CreateSchema("foobar", "[\"null\",\"string\",\"int\"]") 407 408 assert.Error(t, err) 409 } 410 411 func TestClient_CreateSchemaSchemaError(t *testing.T) { 412 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 413 _, _ = w.Write([]byte(`{"id":10}`)) 414 })) 415 defer s.Close() 416 client, _ := registry.NewClient(s.URL) 417 418 _, _, err := client.CreateSchema("foobar", "[\"null\",\"string\",\"int\"") 419 420 assert.Error(t, err) 421 } 422 423 func TestClient_IsRegistered(t *testing.T) { 424 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 425 assert.Equal(t, "POST", r.Method) 426 assert.Equal(t, "/subjects/test", r.URL.Path) 427 428 _, _ = w.Write([]byte(`{"id":10}`)) 429 })) 430 defer s.Close() 431 client, _ := registry.NewClient(s.URL) 432 433 id, schema, err := client.IsRegistered("test", "[\"null\",\"string\",\"int\"]") 434 435 assert.NoError(t, err) 436 assert.Equal(t, 10, id) 437 assert.Equal(t, `["null","string","int"]`, schema.String()) 438 } 439 440 func TestClient_IsRegisteredRequestError(t *testing.T) { 441 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 442 w.WriteHeader(500) 443 })) 444 defer s.Close() 445 client, _ := registry.NewClient(s.URL) 446 447 _, _, err := client.IsRegistered("test", "[\"null\",\"string\",\"int\"]") 448 449 assert.Error(t, err) 450 } 451 452 func TestClient_IsRegisteredJsonError(t *testing.T) { 453 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 454 _, _ = w.Write([]byte(`{"id":10`)) 455 })) 456 defer s.Close() 457 client, _ := registry.NewClient(s.URL) 458 459 _, _, err := client.IsRegistered("test", "[\"null\",\"string\",\"int\"]") 460 461 assert.Error(t, err) 462 } 463 464 func TestClient_IsRegisteredSchemaError(t *testing.T) { 465 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 466 _, _ = w.Write([]byte(`{"id":10}`)) 467 })) 468 defer s.Close() 469 client, _ := registry.NewClient(s.URL) 470 471 _, _, err := client.IsRegistered("test", "[\"null\",\"string\",\"int\"") 472 473 assert.Error(t, err) 474 } 475 476 func TestClient_HandlesServerError(t *testing.T) { 477 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) 478 s.Close() 479 client, _ := registry.NewClient(s.URL) 480 481 _, err := client.GetSchema(5) 482 483 assert.Error(t, err) 484 } 485 486 func TestError_Error(t *testing.T) { 487 err := registry.Error{ 488 StatusCode: 404, 489 Code: 40403, 490 Message: "Schema not found", 491 } 492 493 str := err.Error() 494 495 assert.Equal(t, "Schema not found", str) 496 } 497 498 func TestError_ErrorEmptyMEssage(t *testing.T) { 499 err := registry.Error{ 500 StatusCode: 404, 501 } 502 503 str := err.Error() 504 505 assert.Equal(t, "registry error: 404", str) 506 }