fluke/devops-service/features/support/env.rb
2014-07-02 11:45:02 +04:00

104 lines
2.2 KiB
Ruby

ENV["RACK_ENV"] = "test"
#require File.join(File.dirname(__FILE__), '..', '..', 'config.ru')
require "rubygems"
require 'test/unit'
require 'rack/test'
require "json"
USERNAME = '<username>'
PASSWORD = '<password>'
HOST = '<host>'
PORT = 7070
class MyWorld
include Rack::Test::Methods
@@app = nil
def app
@@app ||= eval("Rack::Builder.new {( " + File.read(File.dirname(__FILE__) + '/../../config.ru') + "\n )}")
end
end
class RequestSender
require "httpclient"
require "yaml"
@last_res = nil
$test_hash = Hash.new
# config:
# host=<host>
# port=<port>
# username=<user>
# password=<psw>
def initialize
file = ENV["CONFIG"] || "./features/support/config.yml"
abort("File does not exist: #{File.absolute_path(file)}") unless File.exists?(file)
@config = config(file)
end
def default_headers
{
"REMOTE_USER" => @config["username"]
}
end
def host
"http://#{@config["host"]}:#{@config["port"]}"
end
def create_url path
host + @config["path_prefix"] + path
end
def last_response
@last_res
end
def get path, query, headers={}
submit do |http|
http.get(create_url(path), query, default_headers.merge(headers))
end
end
def get_without_privileges path, query={}, headers={}
buf_u = @config["username"]
buf_p = @config["password"]
@config["username"] = @config["username_without_privileges"]
@config["password"] = @config["password_without_privileges"]
get(path, query, headers)
@config["username"] = buf_u
@config["password"] = buf_p
end
def post path, query, headers={}
submit do |http|
http.receive_timeout = 0 #!!! bring out to appropriate server step
http.post(create_url(path), JSON.pretty_generate(query), default_headers.merge(headers))
end
end
def delete path, query, headers={}
submit do |http|
http.delete(create_url(path), JSON.pretty_generate(query), default_headers.merge(headers))
end
end
def submit
http = HTTPClient.new
http.set_auth(nil, @config["username"], @config["password"])
res = yield http
@last_res = res
end
def config path
YAML.load_file(path)
end
end
World do
#MyWorld.new
RequestSender.new
end