debian-mirror-gitlab/spec/support/api_helpers.rb

52 lines
1.2 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module ApiHelpers
# Public: Prepend a request path with the path to the API
#
# path - Path to append
# user - User object - If provided, automatically appends private_token query
# string for authenticated requests
#
# Examples
#
# >> api('/issues')
# => "/api/v2/issues"
#
# >> api('/issues', User.last)
# => "/api/v2/issues?private_token=..."
#
# >> api('/issues?foo=bar', User.last)
# => "/api/v2/issues?foo=bar&private_token=..."
#
# Returns the relative path to the requested API resource
def api(path, user = nil)
"/api/#{API::API.version}#{path}" +
# Normalize query string
(path.index('?') ? '' : '?') +
# Append private_token if given a User object
2016-09-13 17:45:13 +05:30
if user.respond_to?(:private_token)
"&private_token=#{user.private_token}"
else
''
end
2014-09-02 18:07:02 +05:30
end
2015-09-25 12:07:36 +05:30
def ci_api(path, user = nil)
"/ci/api/v1/#{path}" +
# Normalize query string
(path.index('?') ? '' : '?') +
# Append private_token if given a User object
2016-09-13 17:45:13 +05:30
if user.respond_to?(:private_token)
"&private_token=#{user.private_token}"
else
''
end
2015-09-25 12:07:36 +05:30
end
2014-09-02 18:07:02 +05:30
def json_response
2015-09-11 14:41:01 +05:30
@_json_response ||= JSON.parse(response.body)
2014-09-02 18:07:02 +05:30
end
end