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
This commit is contained in:
Anna Buianova 2019-11-14 16:58:29 +03:00 committed by Ben Halpern
parent 880f60beff
commit 41ab013050
2 changed files with 12 additions and 2 deletions

View file

@ -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

View file

@ -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)