Module: NginxTestHelper

Includes:
EnvMethods
Defined in:
lib/nginx_test_helper.rb,
lib/nginx_test_helper/config.rb,
lib/nginx_test_helper/version.rb,
lib/nginx_test_helper/env_methods.rb,
lib/nginx_test_helper/http_matchers.rb,
lib/nginx_test_helper/command_line_tool.rb

Defined Under Namespace

Modules: EnvMethods, HttpMatchers Classes: CommandLineTool, Config

Constant Summary collapse

VERSION =
"0.4.2"

Instance Method Summary collapse

Methods included from EnvMethods

included

Methods included from EnvMethods::ClassMethods

#nginx_address, #nginx_event_type, #nginx_executable, #nginx_host, #nginx_port, #nginx_tests_core_dir, #nginx_tests_cores_dir, #nginx_tests_tmp_dir, #nginx_workers

Instance Method Details

#get_in_socket(url, socket, options = {}) ⇒ Object



35
36
37
38
39
# File 'lib/nginx_test_helper.rb', line 35

def get_in_socket(url, socket, options={})
  options = {:use_http_1_0 => false, :host_header => "localhost"}.merge(options)
  socket.print("GET #{url} HTTP/1.#{options[:use_http_1_0] ? "0" : "1\r\nHost: #{options[:host_header]}"}\r\n\r\n")
  read_response_on_socket(socket, options[:wait_for])
end

#headersObject



75
76
77
# File 'lib/nginx_test_helper.rb', line 75

def headers
  {'accept' => 'text/html'}
end

#nginx_run_server(configuration = {}, options = {}, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/nginx_test_helper.rb', line 14

def nginx_run_server(configuration={}, options={}, &block)
  config = Config.new(config_id, configuration)
  start_server(config)
  Timeout::timeout(options[:timeout] || 5) do
    block.call(config)
  end
ensure
  stop_server(config) unless config.nil?
end

#nginx_test_configuration(configuration = {}) ⇒ Object



24
25
26
27
28
29
# File 'lib/nginx_test_helper.rb', line 24

def nginx_test_configuration(configuration={})
  config = Config.new(config_id, configuration)
  stderr_msg = start_server(config)
  stop_server(config)
  "#{stderr_msg}\n#{File.read(config.error_log) if File.exist?(config.error_log)}"
end

#open_socket(host, port) ⇒ Object



31
32
33
# File 'lib/nginx_test_helper.rb', line 31

def open_socket(host, port)
  TCPSocket.open(host, port)
end

#post_in_socket(url, body, socket, options = {}) ⇒ Object



41
42
43
44
45
# File 'lib/nginx_test_helper.rb', line 41

def post_in_socket(url, body, socket, options={})
  options = {:use_http_1_0 => false, :host_header => "localhost"}.merge(options)
  socket.print("POST #{url} HTTP/1.#{options[:use_http_1_0] ? "0" : "1\r\nHost: #{options[:host_header]}"}\r\nContent-Length: #{body.size}\r\n\r\n#{body}")
  read_response_on_socket(socket, options[:wait_for])
end

#read_response_on_socket(socket, wait_for = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nginx_test_helper.rb', line 47

def read_response_on_socket(socket, wait_for=nil)
  will_retry = false
  response ||= socket.readpartial(1)
  while (tmp = socket.read_nonblock(256))
    response += tmp
  end
rescue Errno::EAGAIN => e
  unless(wait_for.nil? || response.include?(wait_for))
    will_retry = true
    IO.select([socket])
    retry
  end
ensure
  unless will_retry
    fail("Any response") if response.nil?
    headers, body = response.split("\r\n\r\n", 2)
    return headers, body
  end
end

#start_server(config) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nginx_test_helper.rb', line 79

def start_server(config)
  error_message = ""
  unless config.configuration[:disable_start_stop_server]
    working_dir = nginx_tests_core_dir(config.config_id)
    FileUtils.mkdir_p working_dir
    Dir.chdir working_dir do
      status = POpen4::popen4("#{ config.nginx_executable } -c #{ config.configuration_filename }") do |stdout, stderr, stdin, pid|
        error_message = stderr.read.strip unless stderr.eof
        return error_message unless error_message.nil?
      end
      fail("Server doesn't started - #{error_message}") if (status.nil? || status.exitstatus != 0)
    end
  end
  error_message
end

#stop_server(config) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/nginx_test_helper.rb', line 95

def stop_server(config)
  error_message = ""
  unless config.configuration[:disable_start_stop_server]
    status = POpen4::popen4("#{ config.nginx_executable } -c #{ config.configuration_filename } -s stop") do |stdout, stderr, stdin, pid|
      error_message = stderr.read.strip unless stderr.eof
      return error_message unless error_message.nil?
    end
    fail("Server doesn't stoped - #{error_message}") if (status.nil? || status.exitstatus != 0)
  end
  error_message
end

#time_diff_milli(start, finish) ⇒ Object



67
68
69
# File 'lib/nginx_test_helper.rb', line 67

def time_diff_milli(start, finish)
   ((finish - start) * 1000.0).to_i
end

#time_diff_sec(start, finish) ⇒ Object



71
72
73
# File 'lib/nginx_test_helper.rb', line 71

def time_diff_sec(start, finish)
   (finish - start).to_i
end