Skip to content

Commit

Permalink
Support basic auth in solr extract service
Browse files Browse the repository at this point in the history
  • Loading branch information
Julie Allinson committed Feb 28, 2018
1 parent 33195dc commit 71b8aaf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
19 changes: 13 additions & 6 deletions lib/hydra/derivatives/processors/full_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@ def extract
# TODO: this pulls the whole file into memory. We should stream it from Fedora instead
# @return [String] the result of calling the extract service
def fetch
req = Net::HTTP.new(uri.host, uri.port)
req.use_ssl = true if check_for_ssl
resp = req.post(uri.to_s, file_content, request_headers)
resp = http_request
raise "Solr Extract service was unsuccessful. '#{uri}' returned code #{resp.code} for #{source_path}\n#{resp.body}" unless resp.code == '200'
file_content.rewind if file_content.respond_to?(:rewind)
resp.body.force_encoding(resp.type_params['charset']) if resp.type_params['charset']
resp.body
end

if resp.type_params['charset']
resp.body.force_encoding(resp.type_params['charset'])
# Send the request to the extract service
# @return [Net::HttpResponse] the result of calling the extract service
def http_request
Net::HTTP.start(uri.host, uri.port) do |http|
req = Net::HTTP::Post.new(uri.request_uri, request_headers)
req.use_ssl = true if check_for_ssl
req.basic_auth uri.user, uri.password unless uri.password.nil?
req.body = file_content
http.request req
end
resp.body
end

def file_content
Expand Down
51 changes: 46 additions & 5 deletions spec/processors/full_text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,27 @@
describe "fetch" do
subject { processor.send(:fetch) }

let(:request) { double }
let(:response_body) { 'returned by Solr' }
let(:uri) { URI('https://example.com:99/solr/update') }
let(:req) do
Net::HTTP::Post.new(
'/solr/update',
"Content-Type" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Content-Length" => "24244"
)
end

before do
allow(processor).to receive(:uri).and_return(uri)
allow(Net::HTTP).to receive(:new).with('example.com', 99).and_return(request)
end

context "when that is successful" do
let(:resp) { double(code: '200', type_params: {}, body: response_body) }

it "calls the extraction service" do
expect(processor).to receive(:check_for_ssl)
expect(request).to receive(:post).with('https://example.com:99/solr/update', String, "Content-Type" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Content-Length" => "24244").and_return(resp)
expect(Net::HTTP).to receive(:start).with('example.com', 99).and_yield(req)
expect(Net::HTTP::Post).to receive(:new).with('/solr/update', "Content-Type" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Content-Length" => "24244").and_return(req)
expect(req).to receive(:request).and_return(resp)
expect(subject).to eq response_body
end
end
Expand All @@ -44,7 +50,9 @@

it "calls the extraction service" do
expect(processor).to receive(:check_for_ssl)
expect(request).to receive(:post).with('https://example.com:99/solr/update', String, "Content-Type" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Content-Length" => "24244").and_return(resp)
expect(Net::HTTP).to receive(:start).with('example.com', 99).and_yield(req)
expect(Net::HTTP::Post).to receive(:new).with('/solr/update', "Content-Type" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Content-Length" => "24244").and_return(req)
expect(req).to receive(:request).and_return(resp)
expect(subject).to eq response_utf8
end
end
Expand All @@ -54,10 +62,43 @@

it "raises an error" do
expect(processor).to receive(:check_for_ssl)
expect(request).to receive(:post).with('https://example.com:99/solr/update', String, "Content-Type" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Content-Length" => "24244").and_return(resp)
expect(Net::HTTP).to receive(:start).with('example.com', 99).and_yield(req)
expect(Net::HTTP::Post).to receive(:new).with('/solr/update', "Content-Type" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Content-Length" => "24244").and_return(req)
expect(req).to receive(:request).and_return(resp)
expect { subject }.to raise_error(RuntimeError, %r{^Solr Extract service was unsuccessful. 'https://example\.com:99/solr/update' returned code 500})
end
end

context "with basic_auth" do
let(:resp) { double(code: '200', type_params: {}, body: response_body) }
let(:uri) { URI('https://user:password@example.com:99/solr/update') }

it "calls the extraction service with basic auth" do
expect(processor).to receive(:check_for_ssl)
expect(Net::HTTP).to receive(:start).with('example.com', 99).and_yield(req)
expect(Net::HTTP::Post).to receive(:new).with('/solr/update', "Content-Type" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Content-Length" => "24244").and_return(req)
expect(req).to receive(:basic_auth).with('user', 'password')
expect(req).to receive(:request).and_return(resp)
expect(subject).to eq response_body
end
end

context "with basic_auth and incorrect credentials" do
let(:resp) { double(code: '401', type_params: {}, body: response_body) }
let(:uri) { URI('https://user:password@example.com:99/solr/update') }

before do
allow(req).to receive(:basic_auth).with('user', 'incorrectpassword')
end

it "raised a 401 error" do
req.basic_auth('user', 'incorrectpassword')
expect(processor).to receive(:check_for_ssl)
expect(Net::HTTP).to receive(:start).with('example.com', 99).and_yield(req)
expect(req).to receive(:request).and_return(resp)
expect { subject }.to raise_error(RuntimeError, %r{^Solr Extract service was unsuccessful. 'https://user:password@example.com:99/solr/update' returned code 401})
end
end
end

describe "uri" do
Expand Down

0 comments on commit 71b8aaf

Please sign in to comment.