|
376 | 376 | end |
377 | 377 | end |
378 | 378 |
|
| 379 | + describe 'request paths with stubbed nodes' do |
| 380 | + let(:endpoints) do |
| 381 | + [ |
| 382 | + '/collections/c/documents/..', |
| 383 | + '/collections/c/documents/.', |
| 384 | + '/collections/../documents/x', |
| 385 | + '/collections/c/documents/a+b', |
| 386 | + '/collections/c/documents/a%20b', |
| 387 | + '/collections/c/documents/a%2Fb' |
| 388 | + ] |
| 389 | + end |
| 390 | + let(:expected_targets) { endpoints.map { |endpoint| "#{endpoint}?q=a+b" } } |
| 391 | + let(:request_targets) { [] } |
| 392 | + |
| 393 | + before do |
| 394 | + stub_request(:any, /node0:8108/) |
| 395 | + .to_return(status: 200, body: JSON.dump('ok' => true), headers: { 'Content-Type' => 'application/json' }) |
| 396 | + |
| 397 | + # WebMock normalizes the URI it records and drops dot segments. Read the |
| 398 | + # request target that Faraday hands to Net::HTTP instead (the 4th argument). |
| 399 | + allow(Net::HTTPGenericRequest).to receive(:new).and_wrap_original do |original, *args| |
| 400 | + request_targets << args[3] |
| 401 | + original.call(*args) |
| 402 | + end |
| 403 | + end |
| 404 | + |
| 405 | + def request_targets_for(keep_alive:, protocol: 'http') |
| 406 | + client = Typesense::Client.new( |
| 407 | + api_key: 'abcd', |
| 408 | + nodes: [{ host: 'node0', port: 8108, protocol: protocol }], |
| 409 | + log_level: Logger::ERROR, |
| 410 | + keep_alive_connections: keep_alive |
| 411 | + ) |
| 412 | + api_call = described_class.new(client.configuration) |
| 413 | + endpoints.each { |endpoint| api_call.delete(endpoint, 'q' => 'a b') } |
| 414 | + request_targets |
| 415 | + end |
| 416 | + |
| 417 | + it 'sends the endpoint and query unchanged with one-shot connections' do |
| 418 | + expect(request_targets_for(keep_alive: false)).to eq(expected_targets) |
| 419 | + end |
| 420 | + |
| 421 | + it 'sends the endpoint and query unchanged with keep-alive connections' do |
| 422 | + expect(request_targets_for(keep_alive: true)).to eq(expected_targets) |
| 423 | + end |
| 424 | + |
| 425 | + it 'sends the endpoint and query unchanged with keep-alive connections and an upper-case protocol' do |
| 426 | + expect(request_targets_for(keep_alive: true, protocol: 'HTTP')).to eq(expected_targets) |
| 427 | + end |
| 428 | + end |
| 429 | + |
| 430 | + describe 'keep-alive requests on a real Typesense server' do |
| 431 | + let(:client) do |
| 432 | + Typesense::Client.new( |
| 433 | + api_key: 'xyz', |
| 434 | + nodes: [{ host: 'localhost', port: 8108, protocol: 'http' }], |
| 435 | + log_level: Logger::ERROR, |
| 436 | + keep_alive_connections: true |
| 437 | + ) |
| 438 | + end |
| 439 | + let(:collection) { client.collections['api_call_dot_segments'] } |
| 440 | + |
| 441 | + def delete_collection |
| 442 | + collection.delete |
| 443 | + rescue Typesense::Error::ObjectNotFound |
| 444 | + # Already gone |
| 445 | + end |
| 446 | + |
| 447 | + before do |
| 448 | + delete_collection |
| 449 | + client.collections.create('name' => 'api_call_dot_segments', 'fields' => [{ 'name' => 'title', 'type' => 'string' }]) |
| 450 | + %w[x ..].each { |id| collection.documents.create('id' => id, 'title' => id) } |
| 451 | + end |
| 452 | + |
| 453 | + after { delete_collection } |
| 454 | + |
| 455 | + it 'deletes only the document whose id is ".."' do |
| 456 | + expect(collection.documents['..'].delete['id']).to eq('..') |
| 457 | + expect(collection.documents['x'].retrieve['id']).to eq('x') |
| 458 | + end |
| 459 | + end |
| 460 | + |
379 | 461 | describe 'keep-alive disabled (default)' do |
380 | 462 | it 'is off by default on the configuration' do |
381 | 463 | expect(typesense.configuration.keep_alive_connections).to be(false) |
|
0 commit comments