Skip to content

Commit

Permalink
Resource sync should use the correct protocol
Browse files Browse the repository at this point in the history
Fixes #2703
  • Loading branch information
jcoyne committed Sep 19, 2016
1 parent b9e8c24 commit daa665e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
3 changes: 2 additions & 1 deletion app/controllers/sufia/resource_sync_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def resource_list

def build_resource_list
Sufia::ResourceSync::ResourceListWriter.new(capability_list_url: sufia.capability_list_url,
resource_host: request.host).write
resource_host: request.host,
resource_protocol: request.protocol).write
end

def build_capability_list
Expand Down
9 changes: 6 additions & 3 deletions lib/sufia/resource_sync/resource_list_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ module ResourceSync
# items, so if we require more than that, we must have multiple Resource
# lists and add a Resource List Index to point to all of them.
class ResourceListWriter
attr_reader :resource_host, :capability_list_url
attr_reader :resource_host, :resource_protocol, :capability_list_url

def initialize(resource_host:, capability_list_url:)
def initialize(resource_host:, resource_protocol: 'http', capability_list_url:)
@resource_host = resource_host
@resource_protocol = resource_protocol
@capability_list_url = capability_list_url
end

Expand Down Expand Up @@ -58,7 +59,9 @@ def build_resources(xml, doc_set)
def build_resource(xml, doc)
xml.url do
key = doc.fetch('has_model_ssim', []).first.constantize.model_name.singular_route_key
xml.loc routes.send(key + "_url", doc['id'], host: resource_host)
xml.loc routes.send(key + "_url", doc['id'],
host: resource_host,
protocol: resource_protocol)
xml.lastmod doc['system_modified_dtsi']
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/sufia/resource_sync_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
let(:capability_list) { Sufia::Engine.routes.url_helpers.capability_list_url(host: 'test.host') }

it "is successful" do
allow(Sufia::ResourceSync::ResourceListWriter).to receive(:new).with(capability_list_url: capability_list, resource_host: "test.host").and_return(writer)
allow(Sufia::ResourceSync::ResourceListWriter).to receive(:new).with(capability_list_url: capability_list, resource_host: "test.host", resource_protocol: 'http://').and_return(writer)
expect(writer).to receive(:write).and_return(document)
get :resource_list
expect(response.content_type).to eq 'application/xml'
Expand Down
33 changes: 25 additions & 8 deletions spec/lib/sufia/resource_sync/resource_list_writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,33 @@
let!(:file_set) { create(:file_set, :public) }
let(:capability_list) { 'http://example.com/capabilityList.xml' }

subject { described_class.new(resource_host: 'example.com', capability_list_url: capability_list).write }
let(:xml) { Nokogiri::XML.parse(subject) }
subject { described_class.new(resource_host: 'example.com',
resource_protocol: protocol,
capability_list_url: capability_list).write }

it "has a list of resources" do
capability = xml.xpath('//rs:ln/@href', 'rs' => "http://www.openarchives.org/rs/terms/").text
expect(capability).to eq capability_list
expect(query(1)).to eq "http://example.com/collections/#{public_collection.id}"
expect(query(2)).to eq "http://example.com/concern/generic_works/#{public_work.id}"
expect(query(3)).to eq "http://example.com/concern/file_sets/#{file_set.id}"
expect(xml.xpath('//x:url', 'x' => sitemap).count).to eq 3
context "with http" do
let(:protocol) { 'http' }
it "has a list of resources" do
capability = xml.xpath('//rs:ln/@href', 'rs' => "http://www.openarchives.org/rs/terms/").text
expect(capability).to eq capability_list
expect(query(1)).to eq "http://example.com/collections/#{public_collection.id}"
expect(query(2)).to eq "http://example.com/concern/generic_works/#{public_work.id}"
expect(query(3)).to eq "http://example.com/concern/file_sets/#{file_set.id}"
expect(xml.xpath('//x:url', 'x' => sitemap).count).to eq 3
end
end

context "with https" do
let(:protocol) { 'https' }
it "has a list of resources" do
capability = xml.xpath('//rs:ln/@href', 'rs' => "http://www.openarchives.org/rs/terms/").text
expect(capability).to eq capability_list
expect(query(1)).to eq "https://example.com/collections/#{public_collection.id}"
expect(query(2)).to eq "https://example.com/concern/generic_works/#{public_work.id}"
expect(query(3)).to eq "https://example.com/concern/file_sets/#{file_set.id}"
expect(xml.xpath('//x:url', 'x' => sitemap).count).to eq 3
end
end

def query(n)
Expand Down

0 comments on commit daa665e

Please sign in to comment.