diff --git a/internal/rest/resources/resources_test.go b/internal/rest/resources/resources_test.go new file mode 100644 index 00000000..b6728a07 --- /dev/null +++ b/internal/rest/resources/resources_test.go @@ -0,0 +1,79 @@ +package resources + +import ( + "testing" + + "github.com/canonical/microcluster/v3/rest" +) + +var validServers = map[string]rest.Server{ + "coreConsumer": { + CoreAPI: true, + Resources: []rest.Resources{ + { + PathPrefix: "core_consumer", + }, + }, + }, +} + +func TestValidateEndpointsValidServers(t *testing.T) { + err := ValidateEndpoints(validServers, "localhost:8000") + if err != nil { + t.Errorf("Valid server failed validation: %s", err) + } +} + +var invalidServers = map[string]rest.Server{ + "emptyResources": { + CoreAPI: true, + }, + "duplicate": { + Resources: []rest.Resources{ + { + PathPrefix: "dup", + Endpoints: []rest.Endpoint{ + { + Path: "duplicate", + }, + }, + }, + { + PathPrefix: "dup", + Endpoints: []rest.Endpoint{ + { + Path: "duplicate", + }, + }, + }, + }, + }, + "overlapCore": { + CoreAPI: true, + Resources: []rest.Resources{ + { + PathPrefix: "core", + }, + }, + }, + "overlapCoreMultipart": { + CoreAPI: true, + Resources: []rest.Resources{ + { + PathPrefix: "core/subpoint", + }, + }, + }, +} + +func TestValidateEndpointsInvalidServers(t *testing.T) { + for serverName, server := range invalidServers { + servers := map[string]rest.Server{ + serverName: server, + } + err := ValidateEndpoints(servers, "localhost:8000") + if err == nil { + t.Errorf("Invalid server %q passed validation", serverName) + } + } +}