diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/gitlab_net_spec.rb | 42 | ||||
-rw-r--r-- | spec/httpunix_spec.rb | 72 | ||||
-rw-r--r-- | spec/spec_helper.rb | 3 |
3 files changed, 14 insertions, 103 deletions
diff --git a/spec/gitlab_net_spec.rb b/spec/gitlab_net_spec.rb index 3d38231..beb2373 100644 --- a/spec/gitlab_net_spec.rb +++ b/spec/gitlab_net_spec.rb @@ -16,19 +16,20 @@ describe GitlabNet, vcr: true do it 'should return 200 code for gitlab check' do VCR.use_cassette("check-ok") do result = gitlab_net.check - result.code.should == '200' + result.status.should == 200 end end it 'adds the secret_token to request' do VCR.use_cassette("check-ok") do - Net::HTTP::Get.any_instance.should_receive(:set_form_data).with(hash_including(secret_token: 'a123')) + Excon::Connection.any_instance.should_receive(:request).with(hash_including(body: 'secret_token=a123')). + and_return(Excon::Response.new) gitlab_net.check end end it "raises an exception if the connection fails" do - Net::HTTP.any_instance.stub(:request).and_raise(StandardError) + Excon::Connection.any_instance.stub(:request).and_raise(StandardError) expect { gitlab_net.check }.to raise_error(GitlabNet::ApiUnreachableError) end end @@ -44,14 +45,15 @@ describe GitlabNet, vcr: true do it 'adds the secret_token to request' do VCR.use_cassette("discover-ok") do - Net::HTTP::Get.any_instance.should_receive(:set_form_data).with(hash_including(secret_token: 'a123')) + Excon::Connection.any_instance.should_receive(:request).with(hash_including(body: 'secret_token=a123')). + and_return(Excon::Response.new) gitlab_net.discover('key-126') end end it "raises an exception if the connection fails" do VCR.use_cassette("discover-ok") do - Net::HTTP.any_instance.stub(:request).and_raise(StandardError) + Excon::Connection.any_instance.stub(:request).and_raise(StandardError) expect { gitlab_net.discover('key-126') }.to raise_error(GitlabNet::ApiUnreachableError) end end @@ -149,7 +151,8 @@ describe GitlabNet, vcr: true do it 'adds the secret_token to the request' do VCR.use_cassette("allowed-pull") do - Net::HTTP::Post.any_instance.should_receive(:set_form_data).with(hash_including(secret_token: 'a123')) + Excon::Connection.any_instance.should_receive(:request).with(hash_including(body: 'secret_token=a123')). + and_return(Excon::Response.new) gitlab_net.check_access('git-receive-pack', 'gitlab/gitlabhq.git', 'key-126', changes, 'ssh') end end @@ -222,7 +225,7 @@ describe GitlabNet, vcr: true do end it "raises an exception if the connection fails" do - Net::HTTP.any_instance.stub(:request).and_raise(StandardError) + Excon::Connection.any_instance.stub(:request).and_raise(StandardError) expect { gitlab_net.check_access('git-upload-pack', 'gitlab/gitlabhq.git', 'user-1', changes, 'ssh') }.to raise_error(GitlabNet::ApiUnreachableError) @@ -238,34 +241,13 @@ describe GitlabNet, vcr: true do end describe :http_client_for do - subject { gitlab_net.send :http_client_for, URI('https://localhost/') } + subject { gitlab_net.send(:http_client_for, 'https://localhost/').data } before do gitlab_net.stub :cert_store gitlab_net.send(:config).stub(:http_settings) { {'self_signed_cert' => true} } end - its(:verify_mode) { should eq(OpenSSL::SSL::VERIFY_NONE) } - end - - describe :http_request_for do - let(:get) do - double(Net::HTTP::Get).tap do |get| - Net::HTTP::Get.stub(:new) { get } - end - end - let(:user) { 'user' } - let(:password) { 'password' } - let(:url) { URI 'http://localhost/' } - subject { gitlab_net.send :http_request_for, :get, url } - - before do - gitlab_net.send(:config).http_settings.stub(:[]).with('user') { user } - gitlab_net.send(:config).http_settings.stub(:[]).with('password') { password } - get.should_receive(:basic_auth).with(user, password).once - get.should_receive(:set_form_data).with(hash_including(secret_token: 'a123')).once - end - - it { should_not be_nil } + it { expect(subject[:ssl_verify_peer]).to eq(false) } end describe :cert_store do diff --git a/spec/httpunix_spec.rb b/spec/httpunix_spec.rb deleted file mode 100644 index 0fcff4a..0000000 --- a/spec/httpunix_spec.rb +++ /dev/null @@ -1,72 +0,0 @@ -require_relative 'spec_helper' -require_relative '../lib/httpunix' -require 'webrick' - -describe URI::HTTPUNIX do - describe :parse do - uri = URI::parse('http+unix://%2Fpath%2Fto%2Fsocket/img.jpg') - subject { uri } - - it { should be_an_instance_of(URI::HTTPUNIX) } - its(:scheme) { should eq('http+unix') } - its(:hostname) { should eq('/path/to/socket') } - its(:path) { should eq('/img.jpg') } - end -end - - -# like WEBrick::HTTPServer, but listens on UNIX socket -class HTTPUNIXServer < WEBrick::HTTPServer - def listen(address, port) - socket = Socket.unix_server_socket(address) - socket.autoclose = false - server = UNIXServer.for_fd(socket.fileno) - socket.close - @listeners << server - end - - # Workaround: - # https://bugs.ruby-lang.org/issues/10956 - # Affecting Ruby 2.2 - # Fix for 2.2 is at https://github.com/ruby/ruby/commit/ab0a64e1 - # However, this doesn't work with 2.1. The following should work for both: - def start(&block) - @shutdown_pipe = IO.pipe - shutdown_pipe = @shutdown_pipe - super(&block) - end - - def cleanup_shutdown_pipe(shutdown_pipe) - @shutdown_pipe = nil - return if !shutdown_pipe - super(shutdown_pipe) - end -end - -def tmp_socket_path - File.join(ROOT_PATH, 'tmp', 'socket') -end - -describe Net::HTTPUNIX do - # "hello world" over unix socket server in background thread - FileUtils.mkdir_p(File.dirname(tmp_socket_path)) - server = HTTPUNIXServer.new(:BindAddress => tmp_socket_path) - server.mount_proc '/' do |req, resp| - resp.body = "Hello World (at #{req.path})" - end - Thread.start { server.start } - - it "talks via HTTP ok" do - VCR.turned_off do - begin - WebMock.allow_net_connect! - http = Net::HTTPUNIX.new(tmp_socket_path) - expect(http.get('/').body).to eq('Hello World (at /)') - expect(http.get('/path').body).to eq('Hello World (at /path)') - - ensure - WebMock.disable_net_connect! - end - end - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 612af76..ce03fba 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,9 +10,10 @@ end require 'vcr' require 'webmock' +require_relative '../lib/gitlab_excon' VCR.configure do |c| c.cassette_library_dir = 'spec/vcr_cassettes' - c.hook_into :webmock + c.hook_into :excon c.configure_rspec_metadata! end |