2014-06-02 18:31:34 +04:00
|
|
|
ENV["RACK_ENV"] = "test"
|
|
|
|
|
#require File.join(File.dirname(__FILE__), '..', '..', 'config.ru')
|
|
|
|
|
|
|
|
|
|
require 'test/unit'
|
|
|
|
|
require 'rack/test'
|
|
|
|
|
require "json"
|
|
|
|
|
require 'httpclient'
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
file = "tests.conf"
|
|
|
|
|
abort("File does not exist: #{File.absolute_path(file)}") unless File.exists?(file)
|
|
|
|
|
@config = {}
|
|
|
|
|
File.open(file).each_line do |l|
|
|
|
|
|
b = l.split("=")
|
|
|
|
|
@config[ b[0] ] = b[1].strip
|
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
def last_response
|
|
|
|
|
@last_res
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get path, query, headers
|
|
|
|
|
submit do |http|
|
|
|
|
|
http.get(host + path, query, headers)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def post path, query, headers
|
|
|
|
|
submit do |http|
|
|
|
|
|
http.receive_timeout = 0 #!!! bring out to appropriate server step
|
|
|
|
|
http.post(host + path, JSON.pretty_generate(query), headers)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def delete path, query, headers
|
|
|
|
|
submit do |http|
|
|
|
|
|
http.delete(host + path, JSON.pretty_generate(query), headers)
|
|
|
|
|
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
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
World do
|
|
|
|
|
#MyWorld.new
|
|
|
|
|
RequestSender.new
|
|
|
|
|
end
|