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/command_line_tool.rb

Defined Under Namespace

Modules: EnvMethods Classes: CommandLineTool, Config

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Methods included from EnvMethods

included

Methods included from EnvMethods::ClassMethods

#nginx_address, #nginx_executable, #nginx_host, #nginx_port, #nginx_tests_tmp_dir, #nginx_workers

Instance Method Details

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



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

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



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

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

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



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

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



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

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.exists?(config.error_log)}"
end

#open_socket(host, port) ⇒ Object



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

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

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



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

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



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

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

#start_server(config) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/nginx_test_helper.rb', line 75

def start_server(config)
  error_message = ""
  unless config.configuration[:disable_start_stop_server]
    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}") unless status.exitstatus == 0
  end
  error_message
end

#stop_server(config) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/nginx_test_helper.rb', line 87

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}") unless status.exitstatus == 0
  end
  error_message
end

#time_diff_milli(start, finish) ⇒ Object



63
64
65
# File 'lib/nginx_test_helper.rb', line 63

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

#time_diff_sec(start, finish) ⇒ Object



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

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