ENV["RACK_ENV"] = "test"
#require File.join(File.dirname(__FILE__), '..', '..', 'config.ru')

require "rubygems"
require 'test/unit'
require 'rack/test'
require "json"

=begin
  USERNAME = '<username>'
  PASSWORD = '<password>'
  HOST = '<host>'
  PORT = 7070
=end

class RequestSender
  require "httpclient"
  require "yaml"

  include Test::Unit::Assertions

  @last_res = nil

  # 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)
    @username = @config["username"]
    @password = @config["password"]
    @server_id = nil
    @task_id = nil
  end

  def default_headers
    {
      "REMOTE_USER" => @config["username"]
    }
  end

  def host
    @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={}
    user_without_privileges do
      get(path, query, headers)
    end
  end

  def post path, query, headers={}
    post_body(path, JSON.pretty_generate(query), headers)
  end

  def post_body path, body, headers={}
    submit do |http|
      http.receive_timeout = 0 #!!! bring out to appropriate server step
      http.post(create_url(path), body, default_headers.merge(headers))
    end
  end

  def post_without_privileges path, query, headers={}
    user_without_privileges do
      post_body(path, query, headers)
    end
  end

  def put path, query, headers={}
    put_body(path, JSON.pretty_generate(query), headers)
  end

  def put_body path, body, headers={}
    submit do |http|
      http.receive_timeout = 0 #!!! bring out to appropriate server step
      http.put(create_url(path), body, default_headers.merge(headers))
    end
  end

  def put_without_privileges path, query="", headers={}
    user_without_privileges do
      put_body(path, query, headers)
    end
  end

  def delete path, query, headers={}
    delete_body(path, JSON.pretty_generate(query), headers)
  end

  def delete_body path, body, headers={}
    submit do |http|
      http.delete(create_url(path), body, default_headers.merge(headers))
    end
  end

  def delete_without_privileges path, query={}, headers={}
    user_without_privileges do
      delete(path, query, headers)
    end
  end

  def submit
    http = HTTPClient.new
    http.set_auth(nil, @username, @password)
    res = yield http
    @last_res = res
  end

  def store_parameter name, value
    instance_variable_set("@#{name}", value)
    puts "stored in #{name}: #{value}"
  end

  def get_parameter name
    instance_variable_get("@#{name}")
  end

  def user_without_privileges
    buf_u = @username
    buf_p = @password
    @username = @username_without_privileges
    @password = @password_without_privileges
    yield
    @username = buf_u
    @password = buf_p
  end

  def config path
    YAML.load_file(path)
  end
end

test_app = RequestSender.new

World do
  test_app
end
