Commit b2a5344a authored by Robert Speicher's avatar Robert Speicher

Add a simple `api` method to ApiHelpers, replacing api_prefix

See docs for usage
parent fba174e9
require 'spec_helper' require 'spec_helper'
describe Gitlab::API do describe Gitlab::API do
include ApiHelpers
let(:user) { Factory :user } let(:user) { Factory :user }
let!(:project) { Factory :project, owner: user } let!(:project) { Factory :project, owner: user }
let!(:issue) { Factory :issue, author: user, assignee: user, project: project } let!(:issue) { Factory :issue, author: user, assignee: user, project: project }
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::API do describe Gitlab::API do
include ApiHelpers
let(:user) { Factory :user } let(:user) { Factory :user }
let!(:project) { Factory :project, owner: user } let!(:project) { Factory :project, owner: user }
let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' } let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' }
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::API do describe Gitlab::API do
include ApiHelpers
let(:user) { Factory :user } let(:user) { Factory :user }
describe "GET /users" do describe "GET /users" do
......
...@@ -27,7 +27,6 @@ RSpec.configure do |config| ...@@ -27,7 +27,6 @@ RSpec.configure do |config|
config.mock_with :rspec config.mock_with :rspec
config.include LoginHelpers, type: :request config.include LoginHelpers, type: :request
config.include ApiHelpers, type: :request
# If you're not using ActiveRecord, or you'd prefer not to run each of your # If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false # examples within a transaction, remove the following line or assign false
......
module ApiHelpers module ApiHelpers
def api_prefix # Public: Prepend a request path with the path to the API
"/api/#{Gitlab::API::VERSION}" #
# 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/#{Gitlab::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 end
def json_response def json_response
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment