From 41ab01305093e235a253d7f86039b5f791b17d6d Mon Sep 17 00:00:00 2001 From: Anna Buianova Date: Thu, 14 Nov 2019 16:58:29 +0300 Subject: [PATCH] Normalize podcast url before checking for reachability (#4806) * Normalize podcast url before checking for reachability * Added to_s when normalizing episode url and simplified tests --- app/services/podcasts/get_media_url.rb | 1 + spec/services/podcasts/get_media_url_spec.rb | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/services/podcasts/get_media_url.rb b/app/services/podcasts/get_media_url.rb index ebb355e86..f4bcb0afc 100644 --- a/app/services/podcasts/get_media_url.rb +++ b/app/services/podcasts/get_media_url.rb @@ -33,6 +33,7 @@ module Podcasts end def url_reachable?(url) + url = Addressable::URI.parse(url).normalize.to_s HTTParty.head(url).code == 200 rescue Net::OpenTimeout, SystemCallError false diff --git a/spec/services/podcasts/get_media_url_spec.rb b/spec/services/podcasts/get_media_url_spec.rb index da8116e14..54bbf6f36 100644 --- a/spec/services/podcasts/get_media_url_spec.rb +++ b/spec/services/podcasts/get_media_url_spec.rb @@ -1,8 +1,8 @@ require "rails_helper" RSpec.describe Podcasts::GetMediaUrl do - let(:https_url) { "https://hello.example.com" } - let(:http_url) { "http://hello.example.com" } + let(:https_url) { "https://hello.example.com/" } + let(:http_url) { "http://hello.example.com/" } it "https, reachable" do stub_request(:head, https_url).to_return(status: 200) @@ -12,6 +12,15 @@ RSpec.describe Podcasts::GetMediaUrl do expect(result.url).to eq(https_url) end + it "normalizes url" do + url = "https://hello.example.com/hi%20there.mp3" + stub_request(:head, url).to_return(status: 200) + result = described_class.call(url) + expect(result.https).to be true + expect(result.reachable).to be true + expect(result.url).to eq(url) + end + it "https, unrechable" do stub_request(:head, https_url).to_return(status: 404) result = described_class.call(https_url)