rs
1# frozen_string_literal: true
2
3require 'test_helper'
4
5class ConnectionTest < Minitest::Test
6 def test_initialize_with_http_scheme
7 connection = Net::Hippie::Connection.new('http', 'example.com', 80)
8 backend = connection.instance_variable_get(:@backend)
9 refute backend.instance_variable_get(:@http).use_ssl?
10 end
11
12 def test_initialize_with_https_scheme
13 connection = Net::Hippie::Connection.new('https', 'example.com', 443)
14 backend = connection.instance_variable_get(:@backend)
15 assert backend.instance_variable_get(:@http).use_ssl?
16 end
17
18 def test_initialize_with_custom_timeouts
19 options = { read_timeout: 30, open_timeout: 15 }
20 connection = Net::Hippie::Connection.new('https', 'example.com', 443, options)
21 backend = connection.instance_variable_get(:@backend)
22 http = backend.instance_variable_get(:@http)
23 assert_equal 30, http.read_timeout
24 assert_equal 15, http.open_timeout
25 end
26
27 def test_initialize_with_custom_verify_mode
28 options = { verify_mode: OpenSSL::SSL::VERIFY_NONE }
29 connection = Net::Hippie::Connection.new('https', 'example.com', 443, options)
30 backend = connection.instance_variable_get(:@backend)
31 http = backend.instance_variable_get(:@http)
32 assert_equal OpenSSL::SSL::VERIFY_NONE, http.verify_mode
33 end
34
35 def test_initialize_with_client_certificate
36 private_key = OpenSSL::PKey::RSA.new(2048)
37 certificate = OpenSSL::X509::Certificate.new
38 certificate.not_after = certificate.not_before = Time.now
39 certificate.public_key = private_key.public_key
40 certificate.sign(private_key, OpenSSL::Digest::SHA256.new)
41
42 options = {
43 certificate: certificate.to_pem,
44 key: private_key.export
45 }
46 connection = Net::Hippie::Connection.new('https', 'example.com', 443, options)
47 backend = connection.instance_variable_get(:@backend)
48 http = backend.instance_variable_get(:@http)
49 assert_equal certificate.to_pem, http.cert.to_pem
50 assert_equal private_key.export, http.key.export
51 end
52
53 def test_initialize_with_client_certificate_and_passphrase
54 private_key = OpenSSL::PKey::RSA.new(2048)
55 passphrase = 'test_passphrase'
56 certificate = OpenSSL::X509::Certificate.new
57 certificate.not_after = certificate.not_before = Time.now
58 certificate.public_key = private_key.public_key
59 certificate.sign(private_key, OpenSSL::Digest::SHA256.new)
60
61 options = {
62 certificate: certificate.to_pem,
63 key: private_key.export(OpenSSL::Cipher.new('AES-256-CBC'), passphrase),
64 passphrase: passphrase
65 }
66 connection = Net::Hippie::Connection.new('https', 'example.com', 443, options)
67 backend = connection.instance_variable_get(:@backend)
68 http = backend.instance_variable_get(:@http)
69 assert_equal certificate.to_pem, http.cert.to_pem
70 assert_equal private_key.export, http.key.export
71 end
72
73 def test_run_executes_request
74 WebMock.stub_request(:get, 'https://example.com/test')
75 .to_return(status: 200, body: 'success')
76
77 connection = Net::Hippie::Connection.new('https', 'example.com', 443)
78 request = Net::HTTP::Get.new('/test')
79 response = connection.run(request)
80
81 assert_equal Net::HTTPOK, response.class
82 assert_equal 'success', response.body
83 end
84
85 def test_build_url_for_absolute_path
86 connection = Net::Hippie::Connection.new('https', 'example.com', 443)
87 url = connection.build_url_for('https://other.com/path')
88 assert_equal 'https://other.com/path', url
89 end
90
91 def test_build_url_for_relative_path_https
92 connection = Net::Hippie::Connection.new('https', 'example.com', 443)
93 url = connection.build_url_for('/api/v1/users')
94 assert_equal 'https://example.com/api/v1/users', url
95 end
96
97 def test_build_url_for_relative_path_http
98 connection = Net::Hippie::Connection.new('http', 'example.com', 80)
99 url = connection.build_url_for('/api/v1/users')
100 assert_equal 'http://example.com/api/v1/users', url
101 end
102
103 def test_build_url_for_http_url
104 connection = Net::Hippie::Connection.new('https', 'example.com', 443)
105 url = connection.build_url_for('http://other.com/path')
106 assert_equal 'http://other.com/path', url
107 end
108end