aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlban Peignier2018-04-18 21:46:11 +0200
committerAlban Peignier2018-04-18 21:49:18 +0200
commit35ef1a2abe0a664b259c99538bb95f04ef6408d4 (patch)
tree4aaee79b2cd674b67308c248926e24b221a75cc9
parentfe3c330cacef3d4367c9a051e3858551986b2c14 (diff)
downloadchouette-core-35ef1a2abe0a664b259c99538bb95f04ef6408d4.tar.bz2
Manage download uri in Import::Gtfs (with or without schema or port). Refs #6368
-rw-r--r--app/models/import/gtfs.rb15
-rw-r--r--spec/models/import/gtfs_spec.rb49
2 files changed, 58 insertions, 6 deletions
diff --git a/app/models/import/gtfs.rb b/app/models/import/gtfs.rb
index f017c8ee5..a20c468c1 100644
--- a/app/models/import/gtfs.rb
+++ b/app/models/import/gtfs.rb
@@ -56,7 +56,7 @@ class Import::Gtfs < Import::Base
attr_accessor :download_host
def download_host
- @download_host ||= Rails.application.config.rails_host.gsub("http://","")
+ @download_host ||= Rails.application.config.rails_host
end
def local_temp_directory
@@ -79,11 +79,20 @@ class Import::Gtfs < Import::Base
Rails.application.routes.url_helpers.download_workbench_import_path(workbench, id, token: token_download)
end
+ def download_uri
+ @download_uri ||=
+ begin
+ host = download_host
+ host = "http://#{host}" unless host =~ %r{https?://}
+ URI.join(host, download_path)
+ end
+ end
+
def download_local_file
local_temp_file do |file|
begin
- Net::HTTP.start(download_host) do |http|
- http.request_get(download_path) do |response|
+ Net::HTTP.start(download_uri.host, download_uri.port) do |http|
+ http.request_get(download_uri.request_uri) do |response|
response.read_body do |segment|
file.write segment
end
diff --git a/spec/models/import/gtfs_spec.rb b/spec/models/import/gtfs_spec.rb
index b4b23be00..96b93dc62 100644
--- a/spec/models/import/gtfs_spec.rb
+++ b/spec/models/import/gtfs_spec.rb
@@ -262,10 +262,53 @@ RSpec.describe Import::Gtfs do
end
end
- describe "#download_host" do
- it "should return host defined by Rails.application.config.rails_host (without http:// schema)" do
- allow(Rails.application.config).to receive(:rails_host).and_return("http://download_host")
+ describe "#download_uri" do
+ let(:import) { Import::Gtfs.new }
+
+ before do
+ allow(import).to receive(:download_path).and_return("/download_path")
+ end
+
+ context "when download_host is 'front'" do
+ before { allow(import).to receive(:download_host).and_return("front") }
+ it "returns http://front/download_path" do
+ expect(import.download_uri.to_s).to eq('http://front/download_path')
+ end
+ end
+
+ context "when download_host is 'front:3000'" do
+ before { allow(import).to receive(:download_host).and_return("front:3000") }
+ it "returns http://front:3000/download_path" do
+ expect(import.download_uri.to_s).to eq('http://front:3000/download_path')
+ end
+ end
+
+ context "when download_host is 'http://front:3000'" do
+ before { allow(import).to receive(:download_host).and_return("http://front:3000") }
+ it "returns http://front:3000/download_path" do
+ expect(import.download_uri.to_s).to eq('http://front:3000/download_path')
+ end
+ end
+
+ context "when download_host is 'https://front:3000'" do
+ before { allow(import).to receive(:download_host).and_return("https://front:3000") }
+ it "returns https://front:3000/download_path" do
+ expect(import.download_uri.to_s).to eq('https://front:3000/download_path')
+ end
+ end
+ context "when download_host is 'http://front'" do
+ before { allow(import).to receive(:download_host).and_return("http://front") }
+ it "returns http://front/download_path" do
+ expect(import.download_uri.to_s).to eq('http://front/download_path')
+ end
+ end
+
+ end
+
+ describe "#download_host" do
+ it "should return host defined by Rails.application.config.rails_host" do
+ allow(Rails.application.config).to receive(:rails_host).and_return("download_host")
expect(Import::Gtfs.new.download_host).to eq("download_host")
end
end