summaryrefslogtreecommitdiff
path: root/test/net/connection_test.rb
blob: 380f680ac34fd229ee06c2ec0225f59e51aaa72b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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