| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "download_strategy" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe CurlPostDownloadStrategy do | 
					
						
							|  |  |  |   subject(:strategy) { described_class.new(url, name, version, **specs) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let(:name) { "foo" } | 
					
						
							|  |  |  |   let(:url) { "https://example.com/foo.tar.gz" } | 
					
						
							|  |  |  |   let(:version) { "1.2.3" } | 
					
						
							|  |  |  |   let(:specs) { {} } | 
					
						
							| 
									
										
										
										
											2023-03-31 21:35:58 +02:00
										 |  |  |   let(:head_response) do | 
					
						
							|  |  |  |     <<~HTTP | 
					
						
							|  |  |  |       HTTP/1.1 200\r | 
					
						
							|  |  |  |       Content-Disposition: attachment; filename="foo.tar.gz" | 
					
						
							|  |  |  |     HTTP | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "#fetch" do | 
					
						
							|  |  |  |     before do | 
					
						
							| 
									
										
										
										
											2023-03-31 21:35:58 +02:00
										 |  |  |       allow(strategy).to receive(:system_command) | 
					
						
							|  |  |  |         .with( | 
					
						
							|  |  |  |           /curl/, | 
					
						
							|  |  |  |           hash_including(args: array_including("--head")), | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         .twice | 
					
						
							|  |  |  |         .and_return(instance_double( | 
					
						
							|  |  |  |                       SystemCommand::Result, | 
					
						
							|  |  |  |                       success?:    true, | 
					
						
							|  |  |  |                       exit_status: instance_double(Process::Status, exitstatus: 0), | 
					
						
							|  |  |  |                       stdout:      head_response, | 
					
						
							|  |  |  |                     )) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |       strategy.temporary_path.dirname.mkpath | 
					
						
							|  |  |  |       FileUtils.touch strategy.temporary_path | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "with :using and :data specified" do | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |       let(:specs) do | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  |         { | 
					
						
							|  |  |  |           using: :post, | 
					
						
							|  |  |  |           data:  { | 
					
						
							|  |  |  |             form: "data", | 
					
						
							|  |  |  |             is:   "good", | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it "adds the appropriate curl args" do | 
					
						
							| 
									
										
										
										
											2023-03-03 22:13:41 +00:00
										 |  |  |         expect(strategy).to receive(:system_command) | 
					
						
							|  |  |  |           .with( | 
					
						
							|  |  |  |             /curl/, | 
					
						
							|  |  |  |             hash_including(args: array_including_cons("-d", "form=data").and(array_including_cons("-d", "is=good"))), | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |           .at_least(:once) | 
					
						
							|  |  |  |           .and_return(instance_double(SystemCommand::Result, success?: true, stdout: "", assert_success!: nil)) | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         strategy.fetch | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "with :using but no :data" do | 
					
						
							|  |  |  |       let(:specs) { { using: :post } } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "adds the appropriate curl args" do | 
					
						
							| 
									
										
										
										
											2023-03-03 22:13:41 +00:00
										 |  |  |         expect(strategy).to receive(:system_command) | 
					
						
							|  |  |  |           .with( | 
					
						
							|  |  |  |             /curl/, | 
					
						
							|  |  |  |             hash_including(args: array_including_cons("-X", "POST")), | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |           .at_least(:once) | 
					
						
							|  |  |  |           .and_return(instance_double(SystemCommand::Result, success?: true, stdout: "", assert_success!: nil)) | 
					
						
							| 
									
										
										
										
											2021-01-31 14:50:29 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         strategy.fetch | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |