diff --git a/cmd/test.go b/cmd/test.go index 110ddecc..8ae83351 100644 --- a/cmd/test.go +++ b/cmd/test.go @@ -30,6 +30,21 @@ import ( var ( runnerChoices = map[string]bool{"HTTP": true, "SOAP_HTTP": true, "SOAP_UI": true, "POSTMAN": true, "OPEN_API_SCHEMA": true, "ASYNC_API_SCHEMA": true, "GRPC_PROTOBUF": true, "GRAPHQL_SCHEMA": true} + + httpTestEndpointSchemes = []string{"http", "https"} + asyncTestEndpointSchemes = []string{ + "amqp", "googlepubsub", "ibmmq", "kafka", "mqtt", "nats", "sns", "sqs", "ws", + } + testEndpointSchemesByRunner = map[string][]string{ + "HTTP": httpTestEndpointSchemes, + "SOAP_HTTP": httpTestEndpointSchemes, + "SOAP_UI": httpTestEndpointSchemes, + "POSTMAN": httpTestEndpointSchemes, + "OPEN_API_SCHEMA": httpTestEndpointSchemes, + "GRAPHQL_SCHEMA": httpTestEndpointSchemes, + "GRPC_PROTOBUF": httpTestEndpointSchemes, + "ASYNC_API_SCHEMA": asyncTestEndpointSchemes, + } ) func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command { @@ -66,15 +81,15 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command { if len(testEndpoint) == 0 || strings.HasPrefix(testEndpoint, "-") { return errors.Wrapf(errors.KindUsage, "missing required argument: (e.g. 'http://localhost:8080/api')") } - if err := validateTestEndpoint(testEndpoint); err != nil { - return err - } if len(runnerType) == 0 || strings.HasPrefix(runnerType, "-") { return errors.Wrapf(errors.KindUsage, "missing required argument: (e.g. 'HTTP', 'POSTMAN', 'OPEN_API_SCHEMA')") } if _, validChoice := runnerChoices[runnerType]; !validChoice { return errors.Wrapf(errors.KindUsage, " should be one of: HTTP, SOAP_HTTP, SOAP_UI, POSTMAN, OPEN_API_SCHEMA, ASYNC_API_SCHEMA, GRPC_PROTOBUF, GRAPHQL_SCHEMA") } + if err := validateTestEndpoint(testEndpoint, runnerType); err != nil { + return err + } // Validate presence and values of flags. if !strings.HasSuffix(waitFor, "milli") && !strings.HasSuffix(waitFor, "sec") && !strings.HasSuffix(waitFor, "min") { @@ -182,10 +197,22 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command { return testCmd } -func validateTestEndpoint(testEndpoint string) error { +func validateTestEndpoint(testEndpoint, runnerType string) error { + schemes, validRunner := testEndpointSchemesByRunner[runnerType] + if !validRunner { + return errors.Wrapf(errors.KindUsage, "unsupported runner %q for test endpoint validation", runnerType) + } + u, err := url.Parse(testEndpoint) - if err != nil || u.Host == "" || (strings.ToLower(u.Scheme) != "http" && strings.ToLower(u.Scheme) != "https") { - return errors.Wrapf(errors.KindUsage, "invalid test endpoint %q: must be a valid http:// or https:// URL", testEndpoint) + if err == nil && u.Host != "" { + for _, scheme := range schemes { + if strings.EqualFold(u.Scheme, scheme) { + return nil + } + } } - return nil + + return errors.Wrapf(errors.KindUsage, + "invalid test endpoint %q for runner %q: must be a URL with a host and one of these schemes: %s", + testEndpoint, runnerType, strings.Join(schemes, ", ")) } diff --git a/cmd/testDryRun.go b/cmd/testDryRun.go index d681f70f..4f56f562 100644 --- a/cmd/testDryRun.go +++ b/cmd/testDryRun.go @@ -128,7 +128,7 @@ func rewriteLocalEndpoint(testEndpoint string) (string, int, bool) { } portStr := u.Port() if portStr == "" { - if u.Scheme == "https" { + if strings.EqualFold(u.Scheme, "https") { portStr = "443" } else { portStr = "80" diff --git a/cmd/testDryRun_test.go b/cmd/testDryRun_test.go index f3a0013a..ef80e1bd 100644 --- a/cmd/testDryRun_test.go +++ b/cmd/testDryRun_test.go @@ -75,7 +75,12 @@ func TestRewriteLocalEndpoint(t *testing.T) { {"loopback IP with path", "http://127.0.0.1:8080/v1", "http://host.testcontainers.internal:8080/v1", 8080, true}, {"localhost no port", "http://localhost", "http://host.testcontainers.internal:80", 80, true}, {"https localhost no port", "https://localhost", "https://host.testcontainers.internal:443", 443, true}, + {"uppercase https localhost no port", "HTTPS://localhost", "https://host.testcontainers.internal:443", 443, true}, + {"kafka localhost", "kafka://localhost:9092/user-signedup", "kafka://host.testcontainers.internal:9092/user-signedup", 9092, true}, + {"mqtt loopback IP", "mqtt://127.0.0.1:1883/topic", "mqtt://host.testcontainers.internal:1883/topic", 1883, true}, + {"ws localhost", "ws://localhost:8081/ws", "ws://host.testcontainers.internal:8081/ws", 8081, true}, {"remote host untouched", "https://api.example.com/v2", "https://api.example.com/v2", 0, false}, + {"remote kafka host untouched", "kafka://broker.example.com:9092/topic", "kafka://broker.example.com:9092/topic", 0, false}, {"other IP untouched", "http://192.168.1.10:3000", "http://192.168.1.10:3000", 0, false}, } for _, tt := range tests { diff --git a/cmd/test_test.go b/cmd/test_test.go index 3ab60822..31e72fb5 100644 --- a/cmd/test_test.go +++ b/cmd/test_test.go @@ -18,6 +18,7 @@ package cmd import ( "os" "os/exec" + "strings" "testing" "github.com/microcks/microcks-cli/pkg/errors" @@ -63,30 +64,69 @@ func TestTestCommandMissingRunnerWithGlobalFlagsDoesNotPanic(t *testing.T) { } func TestValidateTestEndpoint(t *testing.T) { + httpSchemes := strings.Join(httpTestEndpointSchemes, ", ") + asyncSchemes := strings.Join(asyncTestEndpointSchemes, ", ") + tests := []struct { - name string - endpoint string - wantError bool + name string + endpoint string + runner string + wantError bool + wantSchemes string }{ - {name: "http URL", endpoint: "http://example.com/api"}, - {name: "https URL", endpoint: "https://example.com/api"}, - {name: "uppercase HTTP scheme", endpoint: "HTTP://example.com/api"}, - {name: "file URL", endpoint: "file:///etc/passwd", wantError: true}, - {name: "ftp URL", endpoint: "ftp://example.com/api", wantError: true}, - {name: "scheme-less host and port", endpoint: "localhost:8080", wantError: true}, - {name: "malformed URL", endpoint: "http://[::1", wantError: true}, - {name: "missing host", endpoint: "http://", wantError: true}, + {name: "http URL", endpoint: "http://example.com/api", runner: "HTTP"}, + {name: "https URL", endpoint: "https://example.com/api", runner: "POSTMAN"}, + {name: "uppercase HTTP scheme", endpoint: "HTTP://example.com/api", runner: "HTTP"}, + {name: "gRPC HTTP URL", endpoint: "http://localhost:9090", runner: "GRPC_PROTOBUF"}, + {name: "kafka async endpoint", endpoint: "kafka://my-cluster-kafka-bootstrap:9092/user-signedup", runner: "ASYNC_API_SCHEMA"}, + {name: "mqtt async endpoint", endpoint: "mqtt://broker:1883/topic", runner: "ASYNC_API_SCHEMA"}, + {name: "ws async endpoint", endpoint: "ws://localhost:8081/ws", runner: "ASYNC_API_SCHEMA"}, + {name: "amqp async endpoint", endpoint: "amqp://broker:5672/my-vhost/t/my-exchange", runner: "ASYNC_API_SCHEMA"}, + {name: "nats async endpoint", endpoint: "nats://broker:4222/user-signedup", runner: "ASYNC_API_SCHEMA"}, + {name: "googlepubsub async endpoint", endpoint: "googlepubsub://my-project/user-signups", runner: "ASYNC_API_SCHEMA"}, + {name: "sqs async endpoint", endpoint: "sqs://eu-west-3/user-signups", runner: "ASYNC_API_SCHEMA"}, + {name: "sns async endpoint", endpoint: "sns://eu-west-3/user-signups", runner: "ASYNC_API_SCHEMA"}, + {name: "ibmmq async endpoint", endpoint: "ibmmq://mqbroker:1414/QM1/queue/DEV.QUEUE.1", runner: "ASYNC_API_SCHEMA"}, + {name: "uppercase async scheme", endpoint: "KAFKA://broker:9092/topic", runner: "ASYNC_API_SCHEMA"}, + {name: "kafka URL with HTTP runner", endpoint: "kafka://broker:9092/topic", runner: "HTTP", wantError: true, wantSchemes: httpSchemes}, + {name: "http URL with async runner", endpoint: "http://example.com/api", runner: "ASYNC_API_SCHEMA", wantError: true, wantSchemes: asyncSchemes}, + {name: "gRPC host and port without scheme", endpoint: "localhost:9090", runner: "GRPC_PROTOBUF", wantError: true, wantSchemes: httpSchemes}, + {name: "file URL with host", endpoint: "file://example.com/etc/passwd", runner: "ASYNC_API_SCHEMA", wantError: true, wantSchemes: asyncSchemes}, + {name: "ftp URL", endpoint: "ftp://example.com/api", runner: "ASYNC_API_SCHEMA", wantError: true, wantSchemes: asyncSchemes}, + {name: "scheme-less host and port", endpoint: "localhost:8080", runner: "HTTP", wantError: true, wantSchemes: httpSchemes}, + {name: "malformed URL", endpoint: "http://[::1", runner: "HTTP", wantError: true, wantSchemes: httpSchemes}, + {name: "missing host", endpoint: "http://", runner: "HTTP", wantError: true, wantSchemes: httpSchemes}, + {name: "missing async host", endpoint: "kafka://", runner: "ASYNC_API_SCHEMA", wantError: true, wantSchemes: asyncSchemes}, + {name: "unsupported runner", endpoint: "http://example.com/api", runner: "UNKNOWN", wantError: true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := validateTestEndpoint(tt.endpoint) + err := validateTestEndpoint(tt.endpoint, tt.runner) if tt.wantError { assert.Equal(t, errors.KindUsage, errors.KindOf(err)) assert.Error(t, err) + if tt.wantSchemes != "" { + assert.Contains(t, err.Error(), tt.wantSchemes) + } return } assert.NoError(t, err) }) } } + +func TestValidateTestEndpointRejectsFileAndSchemeLessForEveryRunner(t *testing.T) { + for runner := range runnerChoices { + t.Run(runner+"/file", func(t *testing.T) { + err := validateTestEndpoint("file:///etc/passwd", runner) + assert.Equal(t, errors.KindUsage, errors.KindOf(err)) + assert.Error(t, err) + }) + t.Run(runner+"/scheme-less", func(t *testing.T) { + err := validateTestEndpoint("localhost:8080", runner) + assert.Equal(t, errors.KindUsage, errors.KindOf(err)) + assert.Error(t, err) + }) + } +} diff --git a/documentation/cmd/test.md b/documentation/cmd/test.md index 7212b684..7034a82a 100644 --- a/documentation/cmd/test.md +++ b/documentation/cmd/test.md @@ -36,6 +36,13 @@ microcks test get --output json One of: `HTTP`|`SOAP_HTTP`|`SOAP_UI`|`POSTMAN`|`OPEN_API_SCHEMA`|`ASYNC_API_SCHEMA`|`GRPC_PROTOBUF`|`GRAPHQL_SCHEMA` +### Test Endpoint schemes +`HTTP`, `SOAP_HTTP`, `SOAP_UI`, `POSTMAN`, `OPEN_API_SCHEMA`, `GRAPHQL_SCHEMA`, and +`GRPC_PROTOBUF` require an `http://` or `https://` URL. `ASYNC_API_SCHEMA` accepts +`amqp://`, `googlepubsub://`, `ibmmq://`, `kafka://`, `mqtt://`, `nats://`, +`sns://`, `sqs://`, and `ws://`. Endpoints must include a host; schemes such as +`file://` and scheme-less values such as `localhost:8080` are rejected. + ### Options | Flag | Description | | ---------------------- | ----------------------------------------------------------------------------------- |