Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ci/support/api_helpers.rb')
-rw-r--r--spec/ci/support/api_helpers.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/spec/ci/support/api_helpers.rb b/spec/ci/support/api_helpers.rb
new file mode 100644
index 00000000000..555980f2ea7
--- /dev/null
+++ b/spec/ci/support/api_helpers.rb
@@ -0,0 +1,35 @@
+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
+ (user.respond_to?(:private_token) ?
+ "&private_token=#{user.private_token}" : "")
+ end
+
+ def json_response
+ JSON.parse(response.body)
+ end
+
+end