Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/gitlab/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Gitlab
class Request
include HTTParty
format :json
maintain_method_across_redirects true
headers 'Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded'
parser(proc { |body, _| parse(body) })

Expand Down
69 changes: 69 additions & 0 deletions spec/gitlab/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,73 @@
}.merge(described_class.headers))).to have_been_made.times(3)
end
end

describe 'redirection' do
it 'redirect GET' do
http_endpoint = 'http://example.com/api/v4'
https_endpoint = 'https://example.com:443/api/v4'
http_path = "#{http_endpoint}/version"
https_path = "#{https_endpoint}/version"
token = 'token'
@request.private_token = token
@request.endpoint = http_endpoint

allow(@request).to receive(:httparty)

stub_request(:get, http_path)
.to_return(
status: [301, 'Moved Permanently'],
headers: { location: https_path }
)
stub_request(:get, https_path)
.to_return(status: 200)
@request.get('/version')
expect(a_request(:get, http_path).with(headers: {
'PRIVATE_TOKEN' => token
}.merge(described_class.headers))).to have_been_made
expect(a_request(:get, https_path).with(headers: {
'PRIVATE_TOKEN' => token
}.merge(described_class.headers))).to have_been_made
end

it 'redirect PUT' do
http_endpoint = 'http://example.com/api/v4'
https_endpoint = 'https://example.com:443/api/v4'
http_path = "#{http_endpoint}/application/settings"
https_path = "#{https_endpoint}/application/settings"
token = 'token'
body = 'signup_enabled=true'
@request.private_token = token
@request.endpoint = http_endpoint

allow(@request).to receive(:httparty)

stub_request(:put, http_path)
.with(body: body)
.to_return(
status: [301, 'Moved Permanently'],
headers: { location: https_path },
)
stub_request(:put, https_path)
.with(body: body)
.to_return(
status: 200,
body: '{}'
)

# simulate Gitlab.edit_application_settings(signup_enabled: true)
@request.put('/application/settings', :body => body)

expect(a_request(:put, http_path).with(
body: body,
headers: {
'PRIVATE_TOKEN' => token
}.merge(described_class.headers))).to have_been_made
expect(a_request(:put, https_path).with(
body: body,
headers: {
'PRIVATE_TOKEN' => token
}.merge(described_class.headers))).to have_been_made
end
end
end