-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathurl_input_source_spec.rb
More file actions
112 lines (91 loc) · 4.02 KB
/
url_input_source_spec.rb
File metadata and controls
112 lines (91 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# frozen_string_literal: true
require 'mindee'
require_relative '../../http/mock_http_response'
describe Mindee::Input::Source::URLInputSource do
let(:valid_url) { 'https://validurl/some/file.jpg' }
let(:valid_url_no_filename) { 'https://validurl/some/' }
let(:invalid_url) { 'http://invalidurl/some/file.jpg' }
let(:output_dir) { "#{ROOT_DATA_DIR}/output/" }
describe '#initialize' do
context 'with valid URL' do
it 'creates a new instance' do
input = described_class.new('https://platform.mindee.com')
expect(input.url).to eq('https://platform.mindee.com')
end
end
context 'with invalid URL' do
it 'raises an error for invalid URLs' do
expect { described_class.new(invalid_url) }.to raise_error(Mindee::Errors::MindeeInputError)
end
end
end
describe '#as_local_input_source' do
let(:url_input_source) { described_class.new(valid_url) }
let(:url_input_source_no_filename) { described_class.new(valid_url_no_filename) }
before do
allow(Net::HTTP).to receive(:start).and_return(mock_response)
end
context 'when download is successful' do
let(:mock_response) { MockHTTPResponse.new('1.1', '200', 'OK', 'file content') }
it 'returns a BytesInputSource' do
result = url_input_source.as_local_input_source(filename: 'file.pdf')
expect(result).to be_a(Mindee::Input::Source::BytesInputSource)
expect(result.filename).to eq('file.pdf')
expect(result.io_stream).to be_a(StringIO)
expect(result.io_stream.read).to eq('file content')
end
it 'uses a custom filename when provided' do
result = url_input_source.as_local_input_source(filename: 'custom.pdf')
expect(result.filename).to eq('custom.pdf')
end
it 'handles authentication' do
result = url_input_source.as_local_input_source(username: 'user', password: 'pass')
expect(result).to be_a(Mindee::Input::Source::BytesInputSource)
end
end
context 'when download fails' do
let(:mock_response) { MockHTTPResponse.new('1.1', '404', 'Not Found', '') }
it 'raises an error' do
expect do
url_input_source.as_local_input_source
end.to raise_error(Mindee::Errors::MindeeAPIError, %r{Failed to download file})
end
end
end
describe '#write_to_file' do
let(:url_input_source) { described_class.new(valid_url) }
let(:url_input_source_no_filename) { described_class.new(valid_url_no_filename) }
before do
allow(Net::HTTP).to receive(:start).and_return(mock_response)
allow(File).to receive(:write)
end
context 'when download is successful' do
let(:mock_response) { MockHTTPResponse.new('1.1', '200', 'OK', 'file content') }
it 'generates a valid filename when not provided' do
output_file_path = url_input_source_no_filename.write_to_file(output_dir)
expect(output_file_path).to match(%r{mindee_temp_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}_[a-z0-9]{8}\.tmp})
end
it 'saves the file with the provided filename' do
result = url_input_source.write_to_file('/tmp', filename: 'file.pdf')
expect(result).to eq('/tmp/file.pdf')
expect(File).to have_received(:write).with('/tmp/file.pdf', 'file content')
end
it 'uses a custom filename when provided' do
result = url_input_source.write_to_file('/tmp', filename: 'custom.pdf')
expect(result).to eq('/tmp/custom.pdf')
end
it 'handles authentication' do
result = url_input_source_no_filename.write_to_file('/tmp', username: 'user', password: 'pass')
expect(result).to match(%r{/tmp/mindee_temp_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}_[a-z0-9]{8}\.tmp})
end
end
context 'when download fails' do
let(:mock_response) { MockHTTPResponse.new('1.1', '404', 'Not Found', '') }
it 'raises an error' do
expect do
url_input_source.write_to_file('/tmp')
end.to raise_error(Mindee::Errors::MindeeAPIError, %r{Failed to download file})
end
end
end
end