|
| 1 | +package upstream |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "go.uber.org/zap" |
| 12 | + |
| 13 | + "mcpproxy-go/internal/config" |
| 14 | +) |
| 15 | + |
| 16 | +func TestClient_Connect_SSE_NotSupported(t *testing.T) { |
| 17 | + // Create a test config with SSE protocol |
| 18 | + cfg := &config.ServerConfig{ |
| 19 | + Name: "test-sse-server", |
| 20 | + URL: "http://localhost:8080/sse", |
| 21 | + Protocol: "sse", |
| 22 | + Enabled: true, |
| 23 | + Created: time.Now(), |
| 24 | + } |
| 25 | + |
| 26 | + // Create test logger |
| 27 | + logger, err := zap.NewDevelopment() |
| 28 | + require.NoError(t, err) |
| 29 | + |
| 30 | + // Create client with all required parameters |
| 31 | + client, err := NewClient("test-client", cfg, logger, nil, nil) |
| 32 | + require.NoError(t, err) |
| 33 | + require.NotNil(t, client) |
| 34 | + |
| 35 | + // Attempt to connect - should fail at connection, not at transport creation |
| 36 | + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 37 | + defer cancel() |
| 38 | + |
| 39 | + err = client.Connect(ctx) |
| 40 | + |
| 41 | + // Verify the error is about connection failure, not SSE not supported |
| 42 | + require.Error(t, err) |
| 43 | + assert.NotContains(t, err.Error(), "SSE transport is not supported") |
| 44 | + // Should be a connection error since there's no actual SSE server |
| 45 | + assert.True(t, |
| 46 | + strings.Contains(err.Error(), "connection") || |
| 47 | + strings.Contains(err.Error(), "dial") || |
| 48 | + strings.Contains(err.Error(), "refused") || |
| 49 | + strings.Contains(err.Error(), "timeout"), |
| 50 | + "Error should be about connection failure, not SSE support") |
| 51 | +} |
| 52 | + |
| 53 | +func TestClient_DetermineTransportType_SSE(t *testing.T) { |
| 54 | + cfg := &config.ServerConfig{ |
| 55 | + Protocol: "sse", |
| 56 | + URL: "http://localhost:8080/sse", |
| 57 | + } |
| 58 | + |
| 59 | + logger, err := zap.NewDevelopment() |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + client, err := NewClient("test-client", cfg, logger, nil, nil) |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + // Test that determineTransportType returns "sse" for SSE protocol |
| 66 | + transportType := client.determineTransportType() |
| 67 | + assert.Equal(t, "sse", transportType) |
| 68 | +} |
| 69 | + |
| 70 | +func TestClient_Connect_SSE_ErrorContainsAlternatives(t *testing.T) { |
| 71 | + cfg := &config.ServerConfig{ |
| 72 | + Name: "test-sse-server", |
| 73 | + URL: "http://localhost:8080/sse", |
| 74 | + Protocol: "sse", |
| 75 | + Enabled: true, |
| 76 | + Created: time.Now(), |
| 77 | + } |
| 78 | + |
| 79 | + logger, err := zap.NewDevelopment() |
| 80 | + require.NoError(t, err) |
| 81 | + |
| 82 | + client, err := NewClient("test-client", cfg, logger, nil, nil) |
| 83 | + require.NoError(t, err) |
| 84 | + |
| 85 | + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 86 | + defer cancel() |
| 87 | + |
| 88 | + err = client.Connect(ctx) |
| 89 | + |
| 90 | + require.Error(t, err) |
| 91 | + |
| 92 | + // Verify that the error is about connection failure, not SSE not supported |
| 93 | + errorMsg := err.Error() |
| 94 | + assert.NotContains(t, errorMsg, "SSE transport is not supported") |
| 95 | + assert.NotContains(t, errorMsg, "streamable-http") |
| 96 | + |
| 97 | + // Should be a connection error since there's no actual SSE server |
| 98 | + assert.True(t, |
| 99 | + strings.Contains(errorMsg, "connection") || |
| 100 | + strings.Contains(errorMsg, "dial") || |
| 101 | + strings.Contains(errorMsg, "refused") || |
| 102 | + strings.Contains(errorMsg, "timeout"), |
| 103 | + "Error should be about connection failure, not SSE support") |
| 104 | +} |
| 105 | + |
| 106 | +func TestClient_Connect_WorkingTransports(t *testing.T) { |
| 107 | + tests := []struct { |
| 108 | + name string |
| 109 | + protocol string |
| 110 | + url string |
| 111 | + command string |
| 112 | + args []string |
| 113 | + shouldConnect bool |
| 114 | + errorContains string |
| 115 | + }{ |
| 116 | + { |
| 117 | + name: "SSE protocol should work (until actual connection)", |
| 118 | + protocol: "sse", |
| 119 | + url: "http://localhost:8080/sse", |
| 120 | + shouldConnect: false, // Will fail at actual connection, but transport creation should work |
| 121 | + errorContains: "", // Won't check error for SSE as it depends on server availability |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "HTTP protocol should work (until actual connection)", |
| 125 | + protocol: "http", |
| 126 | + url: "http://localhost:8080", |
| 127 | + shouldConnect: false, // Will fail at actual connection, but transport creation should work |
| 128 | + errorContains: "", // Won't check error for HTTP as it depends on server availability |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "Streamable-HTTP protocol should work (until actual connection)", |
| 132 | + protocol: "streamable-http", |
| 133 | + url: "http://localhost:8080", |
| 134 | + shouldConnect: false, // Will fail at actual connection, but transport creation should work |
| 135 | + errorContains: "", // Won't check error for streamable-http as it depends on server availability |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + for _, tt := range tests { |
| 140 | + t.Run(tt.name, func(t *testing.T) { |
| 141 | + cfg := &config.ServerConfig{ |
| 142 | + Name: "test-server", |
| 143 | + Protocol: tt.protocol, |
| 144 | + URL: tt.url, |
| 145 | + Command: tt.command, |
| 146 | + Args: tt.args, |
| 147 | + Enabled: true, |
| 148 | + Created: time.Now(), |
| 149 | + } |
| 150 | + |
| 151 | + logger, err := zap.NewDevelopment() |
| 152 | + require.NoError(t, err) |
| 153 | + |
| 154 | + client, err := NewClient("test-client", cfg, logger, nil, nil) |
| 155 | + require.NoError(t, err) |
| 156 | + |
| 157 | + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 158 | + defer cancel() |
| 159 | + |
| 160 | + err = client.Connect(ctx) |
| 161 | + |
| 162 | + if tt.shouldConnect { |
| 163 | + assert.NoError(t, err) |
| 164 | + } else if tt.errorContains != "" { |
| 165 | + require.Error(t, err) |
| 166 | + assert.Contains(t, err.Error(), tt.errorContains) |
| 167 | + } |
| 168 | + }) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func TestClient_Headers_Support(t *testing.T) { |
| 173 | + tests := []struct { |
| 174 | + name string |
| 175 | + protocol string |
| 176 | + url string |
| 177 | + headers map[string]string |
| 178 | + expectErr bool |
| 179 | + }{ |
| 180 | + { |
| 181 | + name: "SSE with headers", |
| 182 | + protocol: "sse", |
| 183 | + url: "http://localhost:8080/sse", |
| 184 | + headers: map[string]string{ |
| 185 | + "Authorization": "Bearer token123", |
| 186 | + "X-Custom": "custom-value", |
| 187 | + }, |
| 188 | + expectErr: true, // Will fail at connection, but headers should be processed |
| 189 | + }, |
| 190 | + { |
| 191 | + name: "Streamable-HTTP with headers", |
| 192 | + protocol: "streamable-http", |
| 193 | + url: "http://localhost:8080", |
| 194 | + headers: map[string]string{ |
| 195 | + "Authorization": "Bearer token456", |
| 196 | + "Content-Type": "application/json", |
| 197 | + }, |
| 198 | + expectErr: true, // Will fail at connection, but headers should be processed |
| 199 | + }, |
| 200 | + { |
| 201 | + name: "SSE without headers", |
| 202 | + protocol: "sse", |
| 203 | + url: "http://localhost:8080/sse", |
| 204 | + headers: nil, |
| 205 | + expectErr: true, // Will fail at connection |
| 206 | + }, |
| 207 | + { |
| 208 | + name: "Streamable-HTTP without headers", |
| 209 | + protocol: "streamable-http", |
| 210 | + url: "http://localhost:8080", |
| 211 | + headers: nil, |
| 212 | + expectErr: true, // Will fail at connection |
| 213 | + }, |
| 214 | + } |
| 215 | + |
| 216 | + for _, tt := range tests { |
| 217 | + t.Run(tt.name, func(t *testing.T) { |
| 218 | + cfg := &config.ServerConfig{ |
| 219 | + Name: "test-headers-server", |
| 220 | + Protocol: tt.protocol, |
| 221 | + URL: tt.url, |
| 222 | + Headers: tt.headers, |
| 223 | + Enabled: true, |
| 224 | + Created: time.Now(), |
| 225 | + } |
| 226 | + |
| 227 | + logger, err := zap.NewDevelopment() |
| 228 | + require.NoError(t, err) |
| 229 | + |
| 230 | + client, err := NewClient("test-client", cfg, logger, nil, nil) |
| 231 | + require.NoError(t, err) |
| 232 | + require.NotNil(t, client) |
| 233 | + |
| 234 | + // Test that headers are stored in config |
| 235 | + assert.Equal(t, tt.headers, client.config.Headers) |
| 236 | + |
| 237 | + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 238 | + defer cancel() |
| 239 | + |
| 240 | + err = client.Connect(ctx) |
| 241 | + |
| 242 | + if tt.expectErr { |
| 243 | + require.Error(t, err) |
| 244 | + // Should not be a "not supported" error |
| 245 | + assert.NotContains(t, err.Error(), "not supported") |
| 246 | + } else { |
| 247 | + assert.NoError(t, err) |
| 248 | + } |
| 249 | + }) |
| 250 | + } |
| 251 | +} |
0 commit comments