1
0

dist_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package controller
  2. import (
  3. "encoding/json"
  4. "testing"
  5. )
  6. func TestWithServerBasePath(t *testing.T) {
  7. spec := []byte(`{"openapi":"3.0.3","info":{"title":"x"},"servers":[{"url":"/","description":"old"}],"paths":{"/p":{"get":{"summary":"s"}}}}`)
  8. out, err := withServerBasePath(spec, "/test/")
  9. if err != nil {
  10. t.Fatalf("withServerBasePath: %v", err)
  11. }
  12. var doc map[string]any
  13. if err := json.Unmarshal(out, &doc); err != nil {
  14. t.Fatalf("unmarshal result: %v", err)
  15. }
  16. servers, ok := doc["servers"].([]any)
  17. if !ok || len(servers) != 1 {
  18. t.Fatalf("servers = %v, want one entry", doc["servers"])
  19. }
  20. srv, _ := servers[0].(map[string]any)
  21. if srv["url"] != "/test" {
  22. t.Errorf("server url = %v, want /test (trailing slash trimmed)", srv["url"])
  23. }
  24. if doc["openapi"] != "3.0.3" {
  25. t.Errorf("openapi field not preserved: %v", doc["openapi"])
  26. }
  27. if _, ok := doc["paths"].(map[string]any)["/p"]; !ok {
  28. t.Errorf("paths content not preserved verbatim")
  29. }
  30. }
  31. func TestWithServerBasePathInvalidJSON(t *testing.T) {
  32. if _, err := withServerBasePath([]byte("not json"), "/test/"); err == nil {
  33. t.Errorf("expected error on invalid spec, got nil")
  34. }
  35. }