lib/avst-wizard.rb in avst-wizard-0.0.23 vs lib/avst-wizard.rb in avst-wizard-0.0.24

- old
+ new

@@ -22,16 +22,17 @@ module AvstWizard class AvstWizard attr_writer :atl_token - def initialize(url, required_config = {}) + def initialize(url, required_config = {}, url_required_part=nil) @url = url @cookie = "" @current_url = "" @atl_token = "" @required_config = required_config + @url_required_part = url_required_part end # Does GET requests to url, follows redirects, stores cookies and xsrf.token if present def get_stage_and_fetch_cookie(url = @url , limit = 10) # You should choose better exception. @@ -48,34 +49,52 @@ end response = Net::HTTP.start(url.host, url.port, use_ssl: use_ssl, verify_mode: OpenSSL::SSL::VERIFY_NONE) { |http| http.request(req) } if response['set-cookie'] @cookie = response['set-cookie'].split('; ')[0] response['set-cookie'].split(';').each do |part| - if ((part.include? "atl.xsrf.token") and (part.match(/atl.xsrf.token=(.*)/))) + if ((part and part.include? "atl.xsrf.token") and (part.match(/atl.xsrf.token=(.*)/))) # parse only the token @atl_token = part.match(/atl.xsrf.token=(.*)/).captures[0] break end end puts "Found new cookie #{get_cookie}".yellow end if response['location'] - puts "Redirected to: #{response['location']}".yellow + redirection_url = compose_redirection_url(response['location']) + puts "Redirected to: #{redirection_url}".yellow else @current_url = url.request_uri puts "Ended in: #{@current_url}".yellow end case response when Net::HTTPSuccess then response.code.to_i - when Net::HTTPRedirection then get_stage_and_fetch_cookie(response['location'], limit - 1) + when Net::HTTPRedirection then get_stage_and_fetch_cookie(redirection_url, limit - 1) else puts response.body puts response.code.to_s response.code.to_i end end + def compose_redirection_url(location) + # in case response['location'] is not full url we need to compose it + # if it does contain base_url we assume it is ok + if location.include? @url + location + else + # in Jira 7.1.7 location is databaseSetup.jspa not secure/databaseSetup.jspa + if @url_required_part and !location.include? "/#{@url_required_part}/" + "#{@url}/#{@url_required_part}/#{location}" + else + # if required url part is present prepend url + # TODO: better check with regexp + "#{@url}#{location}" + end + end + end + # add atl_token to cookie in case it is present def get_cookie resp = @cookie if @atl_token != "" resp = resp + "; atl.xsrf.token=#{@atl_token}" @@ -163,11 +182,12 @@ puts "Response: #{response.inspect}".yellow puts "Location: #{response['location'].to_s}".yellow puts "Current : #{@url}#{@current_url}".yellow # puts "BODY: #{response.body}" redirected = true - if response['location'] and response['location'] != "#{@url}#{@current_url}" - @current_url = URI.parse(response['location'].to_s).request_uri + if response['location'] and !"#{@url}/#{@current_url}".include? response['location'] + redirection_url = compose_redirection_url(response['location']) + @current_url = URI.parse(redirection_url).request_uri else puts "Was not redirected, staying on page...".yellow redirected = false end puts "Redirected to: #{@current_url.to_s}".yellow