| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/ipfs/kubo/config" |
| 8 | "github.com/ipfs/kubo/test/cli/harness" |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | ) |
| 11 | |
| 12 | func TestWebUI(t *testing.T) { |
| 13 | t.Parallel() |
| 14 | |
| 15 | t.Run("NoFetch=true shows not available error", func(t *testing.T) { |
| 16 | t.Parallel() |
| 17 | node := harness.NewT(t).NewNode().Init() |
| 18 | |
| 19 | node.UpdateConfig(func(cfg *config.Config) { |
| 20 | cfg.Gateway.NoFetch = true |
| 21 | }) |
| 22 | |
| 23 | node.StartDaemon("--offline") |
| 24 | |
| 25 | apiClient := node.APIClient() |
| 26 | resp := apiClient.Get("/webui/") |
| 27 | |
| 28 | // Should return 503 Service Unavailable when WebUI is not in local store |
| 29 | assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) |
| 30 | |
| 31 | // Check response contains helpful information |
| 32 | body := resp.Body |
| 33 | assert.Contains(t, body, "IPFS WebUI Not Available") |
| 34 | assert.Contains(t, body, "Gateway.NoFetch=true") |
| 35 | assert.Contains(t, body, "ipfs pin add") |
| 36 | assert.Contains(t, body, "ipfs dag import") |
| 37 | assert.Contains(t, body, "https://github.com/ipfs/ipfs-webui/releases") |
| 38 | }) |
| 39 | |
| 40 | t.Run("DeserializedResponses=false shows incompatible error", func(t *testing.T) { |
| 41 | t.Parallel() |
| 42 | node := harness.NewT(t).NewNode().Init() |
| 43 | |
| 44 | node.UpdateConfig(func(cfg *config.Config) { |
| 45 | cfg.Gateway.DeserializedResponses = config.False |
| 46 | }) |
| 47 | |
| 48 | node.StartDaemon() |
| 49 | |
| 50 | apiClient := node.APIClient() |
| 51 | resp := apiClient.Get("/webui/") |
| 52 | |
| 53 | // Should return 503 Service Unavailable |
| 54 | assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) |
| 55 | |
| 56 | // Check response contains incompatibility message |
| 57 | body := resp.Body |
| 58 | assert.Contains(t, body, "IPFS WebUI Incompatible") |
| 59 | assert.Contains(t, body, "Gateway.DeserializedResponses=false") |
| 60 | assert.Contains(t, body, "WebUI requires deserializing IPFS responses") |
| 61 | assert.Contains(t, body, "Gateway.DeserializedResponses=true") |
| 62 | }) |
| 63 | |
| 64 | t.Run("Both NoFetch=true and DeserializedResponses=false shows incompatible error", func(t *testing.T) { |
| 65 | t.Parallel() |
| 66 | node := harness.NewT(t).NewNode().Init() |
| 67 | |
| 68 | node.UpdateConfig(func(cfg *config.Config) { |
| 69 | cfg.Gateway.NoFetch = true |
| 70 | cfg.Gateway.DeserializedResponses = config.False |
| 71 | }) |
| 72 | |
| 73 | node.StartDaemon("--offline") |
| 74 | |
| 75 | apiClient := node.APIClient() |
| 76 | resp := apiClient.Get("/webui/") |
| 77 | |
| 78 | // Should return 503 Service Unavailable |
| 79 | assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) |
| 80 | |
| 81 | // DeserializedResponses=false takes priority |
| 82 | body := resp.Body |
| 83 | assert.Contains(t, body, "IPFS WebUI Incompatible") |
| 84 | assert.Contains(t, body, "Gateway.DeserializedResponses=false") |
| 85 | // Should NOT mention NoFetch since DeserializedResponses check comes first |
| 86 | assert.NotContains(t, body, "NoFetch") |
| 87 | }) |
| 88 | } |