fluke/devops-service/features/support/env.rb

104 lines
2.2 KiB
Ruby
Raw Normal View History

2014-06-02 18:31:34 +04:00
ENV["RACK_ENV"] = "test"
#require File.join(File.dirname(__FILE__), '..', '..', 'config.ru')
2014-06-23 13:58:14 +04:00
require "rubygems"
2014-06-02 18:31:34 +04:00
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
2014-06-23 13:58:14 +04:00
require "httpclient"
require "yaml"
2014-06-02 18:31:34 +04:00
@last_res = nil
$test_hash = Hash.new
2014-06-20 13:21:42 +04:00
# config:
# host=<host>
# port=<port>
# username=<user>
# password=<psw>
def initialize
2014-06-23 13:58:14 +04:00
file = ENV["CONFIG"] || "./features/support/config.yml"
2014-06-20 13:21:42 +04:00
abort("File does not exist: #{File.absolute_path(file)}") unless File.exists?(file)
2014-06-23 13:58:14 +04:00
@config = config(file)
2014-07-02 11:45:02 +04:00
end
def default_headers
{
"REMOTE_USER" => @config["username"]
2014-06-23 13:58:14 +04:00
}
2014-06-20 13:21:42 +04:00
end
2014-06-02 18:31:34 +04:00
def host
2014-06-20 13:21:42 +04:00
"http://#{@config["host"]}:#{@config["port"]}"
2014-06-02 18:31:34 +04:00
end
2014-06-23 13:58:14 +04:00
def create_url path
host + @config["path_prefix"] + path
end
2014-06-02 18:31:34 +04:00
def last_response
@last_res
end
2014-06-23 13:58:14 +04:00
def get path, query, headers={}
2014-06-02 18:31:34 +04:00
submit do |http|
2014-07-02 11:45:02 +04:00
http.get(create_url(path), query, default_headers.merge(headers))
2014-06-02 18:31:34 +04:00
end
end
2014-07-02 11:45:02 +04:00
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
2014-06-23 13:58:14 +04:00
def post path, query, headers={}
2014-06-02 18:31:34 +04:00
submit do |http|
http.receive_timeout = 0 #!!! bring out to appropriate server step
2014-07-02 11:45:02 +04:00
http.post(create_url(path), JSON.pretty_generate(query), default_headers.merge(headers))
2014-06-02 18:31:34 +04:00
end
end
2014-06-23 13:58:14 +04:00
def delete path, query, headers={}
2014-06-02 18:31:34 +04:00
submit do |http|
2014-07-02 11:45:02 +04:00
http.delete(create_url(path), JSON.pretty_generate(query), default_headers.merge(headers))
2014-06-02 18:31:34 +04:00
end
end
def submit
http = HTTPClient.new
2014-06-20 13:21:42 +04:00
http.set_auth(nil, @config["username"], @config["password"])
2014-06-02 18:31:34 +04:00
res = yield http
@last_res = res
end
2014-06-23 13:58:14 +04:00
def config path
YAML.load_file(path)
end
2014-06-02 18:31:34 +04:00
end
World do
#MyWorld.new
RequestSender.new
end