class RestWorld
the step definitions are executed in an instance of world so we can add helper methods for use in the step definitions.
Public Class Methods
app()
click to toggle source
the entry point to the rack application to be tested
# File lib/blix/rest/cucumber/world.rb, line 6 def self.app @_app ||= Rack::Builder.parse_file('config.ru').first end
request()
click to toggle source
a dummy request to sent to the server
# File lib/blix/rest/cucumber/world.rb, line 11 def self.request @_req ||= Rack::MockRequest.new(app) end
Public Instance Methods
add_token_to_path(path,token)
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 177 def add_token_to_path(path,token) return unless token if path.include?('?') path + "&token=" + token else path + "?token=" + token end end
add_token_to_request()
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 165 def add_token_to_request return if @_request.include?('token=') if @_user token = @_user.get(:token) || "token12345678-#{@_user.get(:login)}" @_request = if @_request.include?('?') @_request + "&token=#{token}" else @_request + "?token=#{token}" end end end
after_user_create(user, hash)
click to toggle source
a hook that is called before creating a user
# File lib/blix/rest/cucumber/world.rb, line 266 def after_user_create(user, hash); end
before_parse_body(json)
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 104 def before_parse_body(json); end
before_parse_path(path)
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 102 def before_parse_path(path); end
before_user_create(user, hash)
click to toggle source
a hook that is called before creating a user
# File lib/blix/rest/cucumber/world.rb, line 263 def before_user_create(user, hash); end
explain()
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 95 def explain puts "request ==> #{@_verb} #{@_request}" puts "cookies ==> #{cookies.join('; ')}" if cookies.length > 0 puts "body ==> #{@_body}" if @_body puts "response ==> #{@_response.inspect}" end
handle_response(raw_response)
click to toggle source
save the response for furthur enquiries and store any cookies.
# File lib/blix/rest/cucumber/world.rb, line 215 def handle_response(raw_response) @_auth = nil @_response = Response.new(raw_response) # add cookies to the cookie jar. #unless @_current_user=="guest" if cookie = @_response.header["Set-Cookie"] parts = cookie.split(';') cookies << parts[0].strip end #end end
parse_body(json)
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 159 def parse_body(json) json = json.dup before_parse_body(json) parse_json(json) end
parse_json(json)
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 131 def parse_json(json) # replace original format json = json.gsub /:@([a-z0-9_]+)/ do |str| str = str[2..-1] id = store[str] raise ":#{str} has not been stored" unless id if id.is_a?(String) ":\"#{id}\"" else ":#{id}" end end # replace alternative format json = json.gsub /#\{([a-z0-9_]+)\}/ do |str| str = str[2..-2] id = store[str] raise ":#{str} has not been stored" unless id if id.is_a?(String) "\"#{id}\"" else "#{id}" end end end
parse_path(path)
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 106 def parse_path(path) path = path.dup before_parse_path(path) path = path.gsub /\/(@[a-z0-9_]+)/ do |str| str = str[2..-1] id = store[str] raise ":#{str} has not been stored" unless id if id[0] == '/' "#{id}" else "/#{id}" end end # and the query part path.gsub /\=(@[a-z0-9_]+)/ do |str| str = str[2..-1] id = store[str] raise ":#{str} has not been stored" unless id "=#{id}" end end
parse_user(user)
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 227 def parse_user(user) user.split(' ')[-1] end
rack_request_headers()
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 186 def rack_request_headers env = {} env['REMOTE_ADDR'] = '10.0.0.1' env['HTTP_COOKIE'] = cookies.join('; ') env["HTTP_AUTHORIZATION"] = @_auth if @_auth env["HTTP_HOST"] = @_host if @_host env end
request()
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 195 def request RestWorld.request end
send_request(verb, username, path, json)
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 231 def send_request(verb, username, path, json) username = parse_user(username) @_verb = verb @_body = json && parse_body(json) @_request = parse_path(path) @_current_user = username if username == 'guest' @_user = nil else @_user = users[username] raise "user :#{username} has not been initialized" unless @_user pw = @_user.get(:pw) add_token_to_request set_auth_headers(username) end case verb when 'GET' handle_response(request.get(@_request, rack_request_headers)) when 'OPTIONS' handle_response(request.options(@_request, rack_request_headers)) when 'POST' handle_response(request.post(@_request, rack_request_headers.merge(input: @_body))) when 'PUT' handle_response(request.put(@_request, rack_request_headers.merge(input: @_body))) when 'DELETE' handle_response(request.delete(@_request, rack_request_headers.merge(input: @_body))) end end
set_auth_headers(user)
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 203 def set_auth_headers(user) raise "invalid user name:#{user}" unless u = users[user] pw = u.get(:pw) raise "iuser name:#{user} has no password!" unless pw str = user + ":" + pw str = Base64.encode64(str) str = "Basic " + str #Rack::MockRequest::DEFAULT_ENV["HTTP_AUTHORIZATION"] = str @_auth = str end
set_host(name)
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 199 def set_host(name) @_host = name end
store()
click to toggle source
store general information here
# File lib/blix/rest/cucumber/world.rb, line 83 def store @_store ||= {} end
tokens()
click to toggle source
store current user tokens here
# File lib/blix/rest/cucumber/world.rb, line 78 def tokens @_tokens ||= {} end
users()
click to toggle source
store current user information here
# File lib/blix/rest/cucumber/world.rb, line 73 def users @_users ||= {} end
valid_data()
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 91 def valid_data @_response && @_response.data || raise("no valid data returned from service:#{@_response.error}") end
valid_response()
click to toggle source
# File lib/blix/rest/cucumber/world.rb, line 87 def valid_response @_response || raise('no valid response from service') end