summaryrefslogtreecommitdiff
path: root/test/net/connection_test.rb
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-07-06 13:54:36 -0600
committermo khan <mo@mokhan.ca>2025-07-06 13:54:36 -0600
commitd36b6e4f7c99b96aee01656e2ca57312d77a55b6 (patch)
treee875bc14c907cbca92a8c36d9f15d4c946fceed0 /test/net/connection_test.rb
parent6ef050083b8519cfb8120246344514e1c8e27f49 (diff)
feat: add optional Rust backend with Magnus integration
- Add Rust HTTP client using reqwest and Magnus for Ruby integration - Implement transparent backend switching via NET_HIPPIE_RUST environment variable - Maintain 100% backward compatibility with existing Ruby interface - Add comprehensive test coverage (75 tests, 177 assertions) - Support automatic fallback to Ruby backend when Rust unavailable - Include detailed documentation for Rust backend setup and usage - Add proper .gitignore for Rust build artifacts - Update gemspec to support native extensions Performance benefits: - Faster HTTP requests using Rust's optimized reqwest library - Better concurrency with Tokio async runtime - Lower memory usage with zero-cost abstractions - Type safety with compile-time guarantees 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'test/net/connection_test.rb')
-rw-r--r--test/net/connection_test.rb108
1 files changed, 108 insertions, 0 deletions
diff --git a/test/net/connection_test.rb b/test/net/connection_test.rb
new file mode 100644
index 0000000..380f680
--- /dev/null
+++ b/test/net/connection_test.rb
@@ -0,0 +1,108 @@
+# frozen_string_literal: true
+
+require 'test_helper'
+
+class ConnectionTest < Minitest::Test
+ def test_initialize_with_http_scheme
+ connection = Net::Hippie::Connection.new('http', 'example.com', 80)
+ backend = connection.instance_variable_get(:@backend)
+ refute backend.instance_variable_get(:@http).use_ssl?
+ end
+
+ def test_initialize_with_https_scheme
+ connection = Net::Hippie::Connection.new('https', 'example.com', 443)
+ backend = connection.instance_variable_get(:@backend)
+ assert backend.instance_variable_get(:@http).use_ssl?
+ end
+
+ def test_initialize_with_custom_timeouts
+ options = { read_timeout: 30, open_timeout: 15 }
+ connection = Net::Hippie::Connection.new('https', 'example.com', 443, options)
+ backend = connection.instance_variable_get(:@backend)
+ http = backend.instance_variable_get(:@http)
+ assert_equal 30, http.read_timeout
+ assert_equal 15, http.open_timeout
+ end
+
+ def test_initialize_with_custom_verify_mode
+ options = { verify_mode: OpenSSL::SSL::VERIFY_NONE }
+ connection = Net::Hippie::Connection.new('https', 'example.com', 443, options)
+ backend = connection.instance_variable_get(:@backend)
+ http = backend.instance_variable_get(:@http)
+ assert_equal OpenSSL::SSL::VERIFY_NONE, http.verify_mode
+ end
+
+ def test_initialize_with_client_certificate
+ private_key = OpenSSL::PKey::RSA.new(2048)
+ certificate = OpenSSL::X509::Certificate.new
+ certificate.not_after = certificate.not_before = Time.now
+ certificate.public_key = private_key.public_key
+ certificate.sign(private_key, OpenSSL::Digest::SHA256.new)
+
+ options = {
+ certificate: certificate.to_pem,
+ key: private_key.export
+ }
+ connection = Net::Hippie::Connection.new('https', 'example.com', 443, options)
+ backend = connection.instance_variable_get(:@backend)
+ http = backend.instance_variable_get(:@http)
+ assert_equal certificate.to_pem, http.cert.to_pem
+ assert_equal private_key.export, http.key.export
+ end
+
+ def test_initialize_with_client_certificate_and_passphrase
+ private_key = OpenSSL::PKey::RSA.new(2048)
+ passphrase = 'test_passphrase'
+ certificate = OpenSSL::X509::Certificate.new
+ certificate.not_after = certificate.not_before = Time.now
+ certificate.public_key = private_key.public_key
+ certificate.sign(private_key, OpenSSL::Digest::SHA256.new)
+
+ options = {
+ certificate: certificate.to_pem,
+ key: private_key.export(OpenSSL::Cipher.new('AES-256-CBC'), passphrase),
+ passphrase: passphrase
+ }
+ connection = Net::Hippie::Connection.new('https', 'example.com', 443, options)
+ backend = connection.instance_variable_get(:@backend)
+ http = backend.instance_variable_get(:@http)
+ assert_equal certificate.to_pem, http.cert.to_pem
+ assert_equal private_key.export, http.key.export
+ end
+
+ def test_run_executes_request
+ WebMock.stub_request(:get, 'https://example.com/test')
+ .to_return(status: 200, body: 'success')
+
+ connection = Net::Hippie::Connection.new('https', 'example.com', 443)
+ request = Net::HTTP::Get.new('/test')
+ response = connection.run(request)
+
+ assert_equal Net::HTTPOK, response.class
+ assert_equal 'success', response.body
+ end
+
+ def test_build_url_for_absolute_path
+ connection = Net::Hippie::Connection.new('https', 'example.com', 443)
+ url = connection.build_url_for('https://other.com/path')
+ assert_equal 'https://other.com/path', url
+ end
+
+ def test_build_url_for_relative_path_https
+ connection = Net::Hippie::Connection.new('https', 'example.com', 443)
+ url = connection.build_url_for('/api/v1/users')
+ assert_equal 'https://example.com/api/v1/users', url
+ end
+
+ def test_build_url_for_relative_path_http
+ connection = Net::Hippie::Connection.new('http', 'example.com', 80)
+ url = connection.build_url_for('/api/v1/users')
+ assert_equal 'http://example.com/api/v1/users', url
+ end
+
+ def test_build_url_for_http_url
+ connection = Net::Hippie::Connection.new('https', 'example.com', 443)
+ url = connection.build_url_for('http://other.com/path')
+ assert_equal 'http://other.com/path', url
+ end
+end \ No newline at end of file