diff --git a/lib/jira/resource/attachment.rb b/lib/jira/resource/attachment.rb index 2e0bb523..a65001ed 100644 --- a/lib/jira/resource/attachment.rb +++ b/lib/jira/resource/attachment.rb @@ -42,9 +42,9 @@ def self.meta(client) # @param [Hash] headers Any additional headers to call Jira. # @yield |file| # @yieldparam [IO] file The IO object streaming the download. - def download_file(headers = {}, &) + def download_file(headers = {}, &block) default_headers = client.options[:default_headers] - URI.open(content, default_headers.merge(headers), &) + URI.parse(content).open(default_headers.merge(headers), &block) end # Downloads the file contents as a string object. @@ -56,9 +56,7 @@ def download_file(headers = {}, &) # @param [Hash] headers Any additional headers to call Jira. # @return [String,NilClass] The file contents. def download_contents(headers = {}) - download_file(headers) do |file| - file.read - end + download_file(headers, &:read) end def save!(attrs, path = url) diff --git a/spec/jira/resource/attachment_spec.rb b/spec/jira/resource/attachment_spec.rb index d6e35b82..0dde1697 100644 --- a/spec/jira/resource/attachment_spec.rb +++ b/spec/jira/resource/attachment_spec.rb @@ -2,7 +2,7 @@ describe JIRA::Resource::Attachment do subject(:attachment) do - described_class.new( + JIRA::Resource::Attachment.new( client, issue: JIRA::Resource::Issue.new(client), attrs: { 'author' => { 'foo' => 'bar' } } @@ -35,7 +35,7 @@ end describe '.meta' do - subject { described_class.meta(client) } + subject { JIRA::Resource::Attachment.meta(client) } let(:response) do double( @@ -60,38 +60,37 @@ end context 'there is an attachment on an issue' do + let(:client) do + JIRA::Client.new(username: 'username', password: 'password', auth_type: :basic, use_ssl: false) + end + let(:attachment_file_contents) { 'file contents' } + let(:attachment_url) { 'https://localhost:2990/secure/attachment/32323/myfile.txt' } subject(:attachment) do - described_class.new( + JIRA::Resource::Attachment.new( client, issue: JIRA::Resource::Issue.new(client), attrs: { 'author' => { 'foo' => 'bar' }, 'content' => attachment_url } ) end - let(:client) do - JIRA::Client.new(username: 'username', password: 'password', auth_type: :basic, use_ssl: false) + before(:each) do + stub_request(:get, attachment_url).to_return(body: attachment_file_contents) end - let(:attachment_file_contents) { 'file contents' } - let(:file_target) { double(read: :attachment_file_contents) } - let(:attachment_url) { 'https:jirahost/secure/attachment/32323/myfile.txt' } describe '.download_file' do it 'passes file object to block' do - expect(URI).to receive(:open).with(attachment_url, anything).and_yield(file_target) + expect(URI).to receive(:parse).with(attachment_url).and_call_original attachment.download_file do |file| - expect(file).to eq(file_target) + expect(file.read).to eq(attachment_file_contents) end end end describe '.download_contents' do it 'downloads the file contents as a string' do - expect(URI).to receive(:open).with(attachment_url, anything).and_return(attachment_file_contents) - - result_str = attachment.download_contents - - expect(result_str).to eq(attachment_file_contents) + expect(URI).to receive(:parse).with(attachment_url).and_call_original + expect(attachment.download_contents).to eq(attachment_file_contents) end end end @@ -105,12 +104,12 @@ double( body: [ { - id: 10_001, - self: 'http://www.example.com/jira/rest/api/2.0/attachments/10000', - filename: file_name, - created: '2017-07-19T12:23:06.572+0000', - size: file_size, - mimeType: file_mime_type + "id": 10_001, + "self": 'http://www.example.com/jira/rest/api/2.0/attachments/10000', + "filename": file_name, + "created": '2017-07-19T12:23:06.572+0000', + "size": file_size, + "mimeType": file_mime_type } ].to_json ) @@ -131,30 +130,34 @@ expect(attachment.mimeType).to eq file_mime_type expect(attachment.size).to eq file_size end - context 'when using custom client headers' do subject(:bearer_attachment) do - described_class.new( + JIRA::Resource::Attachment.new( bearer_client, issue: JIRA::Resource::Issue.new(bearer_client), attrs: { 'author' => { 'foo' => 'bar' } } ) end - - let(:default_headers_given) do - { 'authorization' => 'Bearer 83CF8B609DE60036A8277BD0E96135751BBC07EB234256D4B65B893360651BF2' } - end + let(:default_headers_given) { + { + 'authorization' => 'Bearer 83CF8B609DE60036A8277BD0E96135751BBC07EB234256D4B65B893360651BF2' + } + } let(:bearer_client) do JIRA::Client.new(username: 'username', password: 'password', auth_type: :basic, use_ssl: false, - default_headers: default_headers_given) + default_headers: default_headers_given ) end let(:merged_headers) do - { 'Accept' => 'application/json', 'X-Atlassian-Token' => 'nocheck' }.merge(default_headers_given) + { + 'Accept' => 'application/json', + 'X-Atlassian-Token' => 'nocheck' + }.merge(default_headers_given) end it 'passes the custom headers' do - expect(bearer_client.request_client).to receive(:request_multipart).with(anything, anything, - merged_headers).and_return(response) + expect(bearer_client.request_client).to receive(:request_multipart) + .with(anything, anything, merged_headers) + .and_return(response) bearer_attachment.save('file' => path_to_file) end @@ -190,31 +193,32 @@ context 'when using custom client headers' do subject(:bearer_attachment) do - described_class.new( + JIRA::Resource::Attachment.new( bearer_client, issue: JIRA::Resource::Issue.new(bearer_client), attrs: { 'author' => { 'foo' => 'bar' } } ) end - - let(:default_headers_given) do - { 'authorization' => 'Bearer 83CF8B609DE60036A8277BD0E96135751BBC07EB234256D4B65B893360651BF2' } - end + let(:default_headers_given) { { 'authorization' => 'Bearer 83CF8B609DE60036A8277BD0E96135751BBC07EB234256D4B65B893360651BF2' } } let(:bearer_client) do JIRA::Client.new(username: 'username', password: 'password', auth_type: :basic, use_ssl: false, - default_headers: default_headers_given) + default_headers: default_headers_given ) end let(:merged_headers) do - { 'Accept' => 'application/json', 'X-Atlassian-Token' => 'nocheck' }.merge(default_headers_given) + { + 'Accept' => 'application/json', + 'X-Atlassian-Token' => 'nocheck' + }.merge(default_headers_given) end it 'passes the custom headers' do - expect(bearer_client.request_client).to receive(:request_multipart).with(anything, anything, - merged_headers).and_return(response) + expect(bearer_client.request_client).to receive(:request_multipart) + .with(anything, anything, merged_headers) + .and_return(response) bearer_attachment.save!('file' => path_to_file) end end end end -end +end \ No newline at end of file