PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/support/ssl_test_helper.rb

http://github.com/jnunemaker/httparty
Ruby | 47 lines | 39 code | 8 blank | 0 comment | 4 complexity | b033198170bce16879deaf53446182f6 MD5 | raw file
 1require 'pathname'
 2
 3module HTTParty
 4  module SSLTestHelper
 5    def ssl_verify_test(mode, ca_basename, server_cert_filename, options = {}, &block)
 6      options = {
 7        format:  :json,
 8        timeout: 30
 9      }.merge(options)
10
11      if mode
12        ca_path = File.expand_path("../../fixtures/ssl/generated/#{ca_basename}", __FILE__)
13        raise ArgumentError.new("#{ca_path} does not exist") unless File.exist?(ca_path)
14        options[mode] = ca_path
15      end
16
17      begin
18        test_server = SSLTestServer.new(
19            rsa_key: File.read(File.expand_path("../../fixtures/ssl/generated/server.key", __FILE__)),
20            cert:    File.read(File.expand_path("../../fixtures/ssl/generated/#{server_cert_filename}", __FILE__)))
21
22        test_server.start
23
24        if mode
25          ca_path = File.expand_path("../../fixtures/ssl/generated/#{ca_basename}", __FILE__)
26          raise ArgumentError.new("#{ca_path} does not exist") unless File.exist?(ca_path)
27          return HTTParty.get("https://localhost:#{test_server.port}/", options, &block)
28        else
29          return HTTParty.get("https://localhost:#{test_server.port}/", options, &block)
30        end
31      ensure
32        test_server.stop if test_server
33      end
34
35      test_server = SSLTestServer.new({
36        rsa_key: path.join('server.key').read,
37        cert:    path.join(server_cert_filename).read
38      })
39
40      test_server.start
41
42      HTTParty.get("https://localhost:#{test_server.port}/", options, &block)
43    ensure
44      test_server.stop if test_server
45    end
46  end
47end