File: connection_tests.rb

package info (click to toggle)
ruby-excon 0.112.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,232 kB
  • sloc: ruby: 7,855; makefile: 5
file content (95 lines) | stat: -rw-r--r-- 3,072 bytes parent folder | download
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
Shindo.tests('Excon Connection') do
  env_init

  with_rackup('basic.ru') do
    tests('#socket connects, sets data[:remote_ip]').returns('127.0.0.1') do
      connection = Excon::Connection.new(
        :host             => '127.0.0.1',
        :hostname         => '127.0.0.1',
        :nonblock         => false,
        :port             => 9292,
        :scheme           => 'http',
        :ssl_verify_peer  => false
      )
      connection.send(:socket) # creates/connects socket
      connection.data[:remote_ip]
    end

    tests("persistent connections") do
      connection = Excon.new('http://127.0.0.1:9292', persistent: true)

      response_body = connection.request(path: '/foo', method: 'get').body
      test("successful uninterrupted request") do
        connection.request(path: '/foo', method: 'get').body == 'foo'
      end

      begin
        # simulate an interrupted connection which leaves data behind
        Timeout::timeout(0.0000000001) do
          connection.request(path: '/foo', method: 'get')
        end
      rescue Timeout::Error
        nil
      end

      test("resets connection after interrupt") do
        response = connection.request(path: '/bar', method: 'get')
        response.body == 'bar'
      end

      if ::Process.respond_to?(:fork)
        connection_id = connection.send(:socket).object_id
        test("fork safety") do
          read, write = IO.pipe
          pid = fork do
            connection_id = connection.send(:socket).object_id
            write.write(Marshal.dump(connection_id))
            write.close
            exit!(0)
          end
          Process.waitpid(pid)
          child_connection_id = Marshal.load(read)
          child_connection_id != connection_id
        end
      end
    end
  end

  tests("inspect redaction") do
    cases = [
      ['user & pass', 'http://user1:pass1@foo.com/', 'Basic dXNlcjE6cGFzczE='],
      ['email & pass', 'http://foo%40bar.com:pass1@foo.com/', 'Basic Zm9vQGJhci5jb206cGFzczE='],
      ['user no pass', 'http://three_user@foo.com/', 'Basic dGhyZWVfdXNlcjo='],
      ['pass no user', 'http://:derppass@foo.com/', 'Basic OmRlcnBwYXNz']
    ]
    cases.each do |desc,url,auth_header|
      conn = Excon.new(url, :proxy => url)

      test("authorization/proxy-authorization headers concealed for #{desc}") do
        !conn.inspect.include?(auth_header)
      end

      if conn.data[:password]
        test("password param concealed for #{desc}") do
          !conn.inspect.include?(conn.data[:password])
        end

        test("password param not mutated for #{desc}") do
          conn.data[:password] == URI.parse(url).password
        end
      end

      if conn.data[:proxy] && conn.data[:proxy][:password]
        test("proxy password param concealed for proxy: #{desc}") do
          !conn.inspect.include?(conn.data[:proxy][:password])
        end

        test("proxy password param not mutated for proxy: #{desc}") do
          conn.data[:proxy][:password] == URI.parse(url).password
        end
      end
    end
  end

  env_restore
end