|
| 1 | +// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package otlp |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestSplitEndpointPath(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + endpoint string |
| 18 | + wantHostPort string |
| 19 | + wantBasePath string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "host and port only", |
| 23 | + endpoint: "localhost:4318", |
| 24 | + wantHostPort: "localhost:4318", |
| 25 | + wantBasePath: "", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "hostname without port", |
| 29 | + endpoint: "otel-collector.local", |
| 30 | + wantHostPort: "otel-collector.local", |
| 31 | + wantBasePath: "", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "Langfuse endpoint with path", |
| 35 | + endpoint: "cloud.langfuse.com/api/public/otel", |
| 36 | + wantHostPort: "cloud.langfuse.com", |
| 37 | + wantBasePath: "/api/public/otel", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "LangSmith endpoint with port and path", |
| 41 | + endpoint: "smith.langchain.com:443/api/v1/otel", |
| 42 | + wantHostPort: "smith.langchain.com:443", |
| 43 | + wantBasePath: "/api/v1/otel", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "trailing slash stripped", |
| 47 | + endpoint: "cloud.langfuse.com/api/public/otel/", |
| 48 | + wantHostPort: "cloud.langfuse.com", |
| 49 | + wantBasePath: "/api/public/otel", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "host:port with trailing slash only", |
| 53 | + endpoint: "localhost:4318/", |
| 54 | + wantHostPort: "localhost:4318", |
| 55 | + wantBasePath: "", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "https scheme stripped before splitting", |
| 59 | + endpoint: "https://cloud.langfuse.com/api/public/otel", |
| 60 | + wantHostPort: "cloud.langfuse.com", |
| 61 | + wantBasePath: "/api/public/otel", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "http scheme stripped before splitting", |
| 65 | + endpoint: "http://localhost:4318", |
| 66 | + wantHostPort: "localhost:4318", |
| 67 | + wantBasePath: "", |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "https scheme with host only", |
| 71 | + endpoint: "https://api.honeycomb.io", |
| 72 | + wantHostPort: "api.honeycomb.io", |
| 73 | + wantBasePath: "", |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "empty string", |
| 77 | + endpoint: "", |
| 78 | + wantHostPort: "", |
| 79 | + wantBasePath: "", |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + t.Parallel() |
| 86 | + |
| 87 | + hostPort, basePath := splitEndpointPath(tt.endpoint) |
| 88 | + assert.Equal(t, tt.wantHostPort, hostPort) |
| 89 | + assert.Equal(t, tt.wantBasePath, basePath) |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments