Skip to content

Commit e9e8ce1

Browse files
committed
Tests for param handling
1 parent f17e0b0 commit e9e8ce1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

spec/convert_api/client_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,36 @@
1010
expect(subject['FileId']).to be_instance_of(String)
1111
end
1212
end
13+
14+
describe '#post' do
15+
let(:file) { 'https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt' }
16+
let(:path) { 'convert/txt/to/pdf/converter/openoffice' }
17+
let(:options) { {} }
18+
let(:mock_response) { OpenStruct.new(code: 200, body: '{}') }
19+
20+
subject{ client.post(path, params, options) }
21+
22+
context 'with normal parameters' do
23+
let(:params) { { File: file } }
24+
let(:uri_with_secret) { "/#{path}?Secret=#{ConvertApi.config.api_secret}" }
25+
26+
it 'makes a post request with no extra URL parameters' do
27+
expect(Net::HTTP::Post).to(receive(:new).with(uri_with_secret, described_class::DEFAULT_HEADERS).and_call_original)
28+
expect_any_instance_of(Net::HTTP).to(receive(:request).and_return(mock_response))
29+
expect(subject).to be_an_instance_of(Hash)
30+
end
31+
end
32+
33+
context 'with parameters that MUST be passed via URL' do
34+
let(:webhook) { 'https://www.convertapi.com/fake-webhook' }
35+
let(:params) { { File: file, WebHook: webhook } }
36+
let(:uri_with_selected_params) { "/#{path}?#{URI.encode_www_form({ WebHook: webhook, Secret: ConvertApi.config.api_secret})}" }
37+
38+
it 'makes a post request that passes the required parameters via URL' do
39+
expect(Net::HTTP::Post).to(receive(:new).with(uri_with_selected_params, described_class::DEFAULT_HEADERS).and_call_original)
40+
expect_any_instance_of(Net::HTTP).to(receive(:request).and_return(mock_response))
41+
expect(subject).to be_an_instance_of(Hash)
42+
end
43+
end
44+
end
1345
end

0 commit comments

Comments
 (0)