| 
									
										
										
										
											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" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 15:53:28 +03:00
										 |  |  | =begin | 
					
						
							| 
									
										
										
										
											2014-06-02 18:31:34 +04:00
										 |  |  |   USERNAME = '<username>' | 
					
						
							|  |  |  |   PASSWORD = '<password>' | 
					
						
							|  |  |  |   HOST = '<host>' | 
					
						
							|  |  |  |   PORT = 7070
 | 
					
						
							| 
									
										
										
										
											2015-07-28 15:53:28 +03:00
										 |  |  | =end | 
					
						
							| 
									
										
										
										
											2014-06-02 18:31:34 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class RequestSender | 
					
						
							| 
									
										
										
										
											2014-06-23 13:58:14 +04:00
										 |  |  |   require "httpclient" | 
					
						
							|  |  |  |   require "yaml" | 
					
						
							| 
									
										
										
										
											2014-06-02 18:31:34 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-10 13:08:59 +03:00
										 |  |  |   include Test::Unit::Assertions | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-02 18:31:34 +04:00
										 |  |  |   @last_res = nil | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     @username = @config["username"] | 
					
						
							|  |  |  |     @password = @config["password"] | 
					
						
							|  |  |  |     @server_id = nil | 
					
						
							|  |  |  |     @task_id = nil | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     @host ||= "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={} | 
					
						
							| 
									
										
										
										
											2014-07-03 13:54:10 +04:00
										 |  |  |     user_without_privileges do | 
					
						
							|  |  |  |       get(path, query, headers) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-07-02 11:45:02 +04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 13:58:14 +04:00
										 |  |  |   def post path, query, headers={} | 
					
						
							| 
									
										
										
										
											2014-07-03 13:26:29 +04:00
										 |  |  |     post_body(path, JSON.pretty_generate(query), headers) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def post_body path, body, headers={} | 
					
						
							| 
									
										
										
										
											2014-06-02 18:31:34 +04:00
										 |  |  |     submit do |http| | 
					
						
							|  |  |  |       http.receive_timeout = 0 #!!! bring out to appropriate server step | 
					
						
							| 
									
										
										
										
											2014-07-03 13:26:29 +04:00
										 |  |  |       http.post(create_url(path), body, default_headers.merge(headers)) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-15 13:05:20 +04:00
										 |  |  |   def post_without_privileges path, query, headers={} | 
					
						
							| 
									
										
										
										
											2014-07-15 12:57:21 +04:00
										 |  |  |     user_without_privileges do | 
					
						
							| 
									
										
										
										
											2014-07-15 13:05:20 +04:00
										 |  |  |       post_body(path, query, headers) | 
					
						
							| 
									
										
										
										
											2014-07-15 12:57:21 +04:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-03 13:54:10 +04:00
										 |  |  |   def put path, query, headers={} | 
					
						
							|  |  |  |     put_body(path, JSON.pretty_generate(query), headers) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-03 13:26:29 +04:00
										 |  |  |   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)) | 
					
						
							| 
									
										
										
										
											2014-06-02 18:31:34 +04:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-16 13:32:59 +04:00
										 |  |  |   def put_without_privileges path, query="", headers={} | 
					
						
							| 
									
										
										
										
											2014-07-03 13:54:10 +04:00
										 |  |  |     user_without_privileges do | 
					
						
							| 
									
										
										
										
											2014-07-16 13:32:59 +04:00
										 |  |  |       put_body(path, query, headers) | 
					
						
							| 
									
										
										
										
											2014-07-03 13:54:10 +04:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 13:58:14 +04:00
										 |  |  |   def delete path, query, headers={} | 
					
						
							| 
									
										
										
										
											2014-07-03 13:26:29 +04:00
										 |  |  |     delete_body(path, JSON.pretty_generate(query), headers) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def delete_body path, body, headers={} | 
					
						
							| 
									
										
										
										
											2014-06-02 18:31:34 +04:00
										 |  |  |     submit do |http| | 
					
						
							| 
									
										
										
										
											2014-07-03 13:26:29 +04:00
										 |  |  |       http.delete(create_url(path), body, default_headers.merge(headers)) | 
					
						
							| 
									
										
										
										
											2014-06-02 18:31:34 +04:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-03 13:54:10 +04:00
										 |  |  |   def delete_without_privileges path, query={}, headers={} | 
					
						
							|  |  |  |     user_without_privileges do | 
					
						
							|  |  |  |       delete(path, query, headers) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-02 18:31:34 +04:00
										 |  |  |   def submit | 
					
						
							|  |  |  |     http = HTTPClient.new | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     http.set_auth(nil, @username, @password) | 
					
						
							| 
									
										
										
										
											2014-06-02 18:31:34 +04:00
										 |  |  |     res = yield http | 
					
						
							|  |  |  |     @last_res = res | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-06-23 13:58:14 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |   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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-03 13:54:10 +04:00
										 |  |  |   def user_without_privileges | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     buf_u = @username | 
					
						
							|  |  |  |     buf_p = @password | 
					
						
							|  |  |  |     @username = @username_without_privileges | 
					
						
							|  |  |  |     @password = @password_without_privileges | 
					
						
							| 
									
										
										
										
											2014-07-03 13:54:10 +04:00
										 |  |  |     yield | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     @username = buf_u | 
					
						
							|  |  |  |     @password = buf_p | 
					
						
							| 
									
										
										
										
											2014-07-03 13:54:10 +04:00
										 |  |  |   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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | test_app = RequestSender.new | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-02 18:31:34 +04:00
										 |  |  | World do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |   test_app | 
					
						
							| 
									
										
										
										
											2014-06-02 18:31:34 +04:00
										 |  |  | end |