|
174 | 174 | expect(options[:form][:scope]).to eq(scope) |
175 | 175 | end |
176 | 176 | end |
| 177 | + |
| 178 | + context "when token endpoint returns OAuth error response" do |
| 179 | + let(:oauth_error_response) do |
| 180 | + { |
| 181 | + "error" => "invalid_request", |
| 182 | + "error_description" => "Missing required parameter: grant_type", |
| 183 | + "error_uri" => "https://example.com/docs/oauth-errors#invalid_request" |
| 184 | + } |
| 185 | + end |
| 186 | + |
| 187 | + before do |
| 188 | + response = instance_double(HTTPX::Response, status: 400, body: oauth_error_response.to_json) |
| 189 | + allow(http_client).to receive(:post).and_return(response) |
| 190 | + end |
| 191 | + |
| 192 | + it "raises TransportError with OAuth error details from RFC 6749 section 5.2 format" do |
| 193 | + expect do |
| 194 | + manager.exchange_client_credentials(server_metadata, client_info_with_secret, scope, server_url) |
| 195 | + end.to raise_error(RubyLLM::MCP::Errors::TransportError) { |error| |
| 196 | + expect(error.message).to include("OAuth error 'invalid_request'") |
| 197 | + expect(error.message).to include("Missing required parameter: grant_type") |
| 198 | + expect(error.code).to eq(400) |
| 199 | + expect(error.error).to eq("invalid_request") |
| 200 | + } |
| 201 | + end |
| 202 | + end |
| 203 | + |
| 204 | + context "when token endpoint returns 401 invalid_client error response" do |
| 205 | + let(:oauth_error_response) do |
| 206 | + { |
| 207 | + "error" => "invalid_client", |
| 208 | + "error_description" => "Client authentication failed" |
| 209 | + } |
| 210 | + end |
| 211 | + |
| 212 | + before do |
| 213 | + response = instance_double(HTTPX::Response, status: 401, body: oauth_error_response.to_json) |
| 214 | + allow(http_client).to receive(:post).and_return(response) |
| 215 | + end |
| 216 | + |
| 217 | + it "raises TransportError with RFC 6749 invalid_client semantics" do |
| 218 | + expect do |
| 219 | + manager.exchange_client_credentials(server_metadata, client_info_with_secret, scope, server_url) |
| 220 | + end.to raise_error(RubyLLM::MCP::Errors::TransportError) { |error| |
| 221 | + expect(error.message).to include("OAuth error 'invalid_client'") |
| 222 | + expect(error.message).to include("Client authentication failed") |
| 223 | + expect(error.code).to eq(401) |
| 224 | + expect(error.error).to eq("invalid_client") |
| 225 | + } |
| 226 | + end |
| 227 | + end |
| 228 | + |
| 229 | + context "when token endpoint returns HTTP 200 with OAuth error payload" do |
| 230 | + let(:oauth_error_response) do |
| 231 | + { |
| 232 | + "error" => "invalid_client", |
| 233 | + "error_description" => "Client authentication failed." |
| 234 | + } |
| 235 | + end |
| 236 | + |
| 237 | + before do |
| 238 | + response = instance_double(HTTPX::Response, status: 200, body: oauth_error_response.to_json) |
| 239 | + allow(http_client).to receive(:post).and_return(response) |
| 240 | + end |
| 241 | + |
| 242 | + it "raises TransportError instead of creating a token with missing access_token" do |
| 243 | + expect do |
| 244 | + manager.exchange_client_credentials(server_metadata, client_info_with_secret, scope, server_url) |
| 245 | + end.to raise_error(RubyLLM::MCP::Errors::TransportError, /OAuth error 'invalid_client'/) |
| 246 | + end |
| 247 | + end |
| 248 | + |
| 249 | + context "when token endpoint returns success status but no access token" do |
| 250 | + let(:incomplete_response) do |
| 251 | + { |
| 252 | + "token_type" => "Bearer", |
| 253 | + "expires_in" => 3600 |
| 254 | + } |
| 255 | + end |
| 256 | + |
| 257 | + before do |
| 258 | + response = instance_double(HTTPX::Response, status: 200, body: incomplete_response.to_json) |
| 259 | + allow(http_client).to receive(:post).and_return(response) |
| 260 | + end |
| 261 | + |
| 262 | + it "raises a clear TransportError for invalid token payload" do |
| 263 | + expect do |
| 264 | + manager.exchange_client_credentials(server_metadata, client_info_with_secret, scope, server_url) |
| 265 | + end.to raise_error(RubyLLM::MCP::Errors::TransportError, /missing access_token/) |
| 266 | + end |
| 267 | + end |
177 | 268 | end |
178 | 269 |
|
179 | 270 | describe "#refresh_token" do |
|
265 | 356 | expect(logger).to have_received(:warn).with(/Invalid token refresh response/) |
266 | 357 | end |
267 | 358 | end |
| 359 | + |
| 360 | + context "when refresh response contains OAuth error fields" do |
| 361 | + let(:oauth_error_response) do |
| 362 | + { |
| 363 | + "error" => "invalid_grant", |
| 364 | + "error_description" => "Refresh token is expired" |
| 365 | + } |
| 366 | + end |
| 367 | + |
| 368 | + before do |
| 369 | + response = instance_double(HTTPX::Response, status: 200, body: oauth_error_response.to_json) |
| 370 | + allow(http_client).to receive(:post).and_return(response) |
| 371 | + end |
| 372 | + |
| 373 | + it "returns nil and logs warning" do |
| 374 | + result = manager.refresh_token(server_metadata, client_info, token, server_url) |
| 375 | + |
| 376 | + expect(result).to be_nil |
| 377 | + expect(logger).to have_received(:warn).with( |
| 378 | + /Token refresh failed: OAuth error 'invalid_grant'/ |
| 379 | + ) |
| 380 | + end |
| 381 | + end |
| 382 | + |
| 383 | + context "when refresh endpoint returns non-200 with OAuth error payload" do |
| 384 | + let(:oauth_error_response) do |
| 385 | + { |
| 386 | + "error" => "invalid_grant", |
| 387 | + "error_description" => "Refresh token is expired" |
| 388 | + } |
| 389 | + end |
| 390 | + |
| 391 | + before do |
| 392 | + response = instance_double(HTTPX::Response, status: 400, body: oauth_error_response.to_json) |
| 393 | + allow(http_client).to receive(:post).and_return(response) |
| 394 | + end |
| 395 | + |
| 396 | + it "returns nil and logs OAuth error details" do |
| 397 | + result = manager.refresh_token(server_metadata, client_info, token, server_url) |
| 398 | + |
| 399 | + expect(result).to be_nil |
| 400 | + expect(logger).to have_received(:warn).with( |
| 401 | + /Token refresh failed: OAuth error 'invalid_grant': Refresh token is expired/ |
| 402 | + ) |
| 403 | + end |
| 404 | + end |
268 | 405 | end |
269 | 406 | end |
0 commit comments