Commit b2a5344a2d68922d5c6cb8de228fb9b41ce3efc4
1 parent
fba174e9
Exists in
master
and in
4 other branches
Add a simple `api` method to ApiHelpers, replacing api_prefix
See docs for usage
Showing
5 changed files
with
33 additions
and
3 deletions
Show diff stats
spec/requests/api/issues_spec.rb
spec/requests/api/projects_spec.rb
spec/requests/api/users_spec.rb
spec/spec_helper.rb
| ... | ... | @@ -27,7 +27,6 @@ RSpec.configure do |config| |
| 27 | 27 | config.mock_with :rspec |
| 28 | 28 | |
| 29 | 29 | config.include LoginHelpers, type: :request |
| 30 | - config.include ApiHelpers, type: :request | |
| 31 | 30 | |
| 32 | 31 | # If you're not using ActiveRecord, or you'd prefer not to run each of your |
| 33 | 32 | # examples within a transaction, remove the following line or assign false | ... | ... |
spec/support/api_helpers.rb
| 1 | 1 | module ApiHelpers |
| 2 | - def api_prefix | |
| 3 | - "/api/#{Gitlab::API::VERSION}" | |
| 2 | + # Public: Prepend a request path with the path to the API | |
| 3 | + # | |
| 4 | + # path - Path to append | |
| 5 | + # user - User object - If provided, automatically appends private_token query | |
| 6 | + # string for authenticated requests | |
| 7 | + # | |
| 8 | + # Examples | |
| 9 | + # | |
| 10 | + # >> api('/issues') | |
| 11 | + # => "/api/v2/issues" | |
| 12 | + # | |
| 13 | + # >> api('/issues', User.last) | |
| 14 | + # => "/api/v2/issues?private_token=..." | |
| 15 | + # | |
| 16 | + # >> api('/issues?foo=bar', User.last) | |
| 17 | + # => "/api/v2/issues?foo=bar&private_token=..." | |
| 18 | + # | |
| 19 | + # Returns the relative path to the requested API resource | |
| 20 | + def api(path, user = nil) | |
| 21 | + "/api/#{Gitlab::API::VERSION}#{path}" + | |
| 22 | + | |
| 23 | + # Normalize query string | |
| 24 | + (path.index('?') ? '' : '?') + | |
| 25 | + | |
| 26 | + # Append private_token if given a User object | |
| 27 | + (user.respond_to?(:private_token) ? | |
| 28 | + "&private_token=#{user.private_token}" : "") | |
| 4 | 29 | end |
| 5 | 30 | |
| 6 | 31 | def json_response | ... | ... |