2
0

capabilities_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package httpserver
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. func TestCapabilitiesEndpoint(t *testing.T) {
  9. server := setupKVTestServer(t)
  10. r := httptest.NewRequest(http.MethodGet, "/capabilities", nil)
  11. w := httptest.NewRecorder()
  12. server.server.Handler.ServeHTTP(w, r)
  13. if w.Code != http.StatusOK {
  14. t.Fatalf("status = %d body %s", w.Code, w.Body.String())
  15. }
  16. var resp struct {
  17. SchemaVersion int `json:"schema_version"`
  18. Engine string `json:"engine"`
  19. ErrorCodes map[string]string `json:"error_codes"`
  20. Constraints struct {
  21. Unique struct {
  22. Supported bool `json:"supported"`
  23. SQLState string `json:"sqlstate"`
  24. } `json:"unique"`
  25. } `json:"constraints"`
  26. }
  27. if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
  28. t.Fatalf("decode: %v", err)
  29. }
  30. if resp.SchemaVersion < 1 {
  31. t.Fatalf("schema_version = %d", resp.SchemaVersion)
  32. }
  33. if resp.Engine != "pizzasql" {
  34. t.Fatalf("engine = %q", resp.Engine)
  35. }
  36. if !resp.Constraints.Unique.Supported || resp.Constraints.Unique.SQLState != "23505" {
  37. t.Fatalf("unique capability = %+v", resp.Constraints.Unique)
  38. }
  39. if resp.ErrorCodes["40001"] != "serialization_failure" {
  40. t.Fatalf("error_codes = %v", resp.ErrorCodes)
  41. }
  42. }