Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

If there is no network connectivity detected, rspec only runs example…

…s which do not make real network connections
  • Loading branch information...
commit 351bac68bdb13bc6954753f5567b92d42744519c 1 parent 161225c
@bblimke authored
Showing with 33 additions and 1 deletion.
  1. +25 −0 spec/network_connection.rb
  2. +8 −1 spec/spec_helper.rb
View
25 spec/network_connection.rb
@@ -0,0 +1,25 @@
+module NetworkConnection
+ def self.connect_to(host, port, timeout=nil)
+ addr = Socket.getaddrinfo(host, nil)
+ sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
+
+ if timeout
+ secs = Integer(timeout)
+ usecs = Integer((timeout - secs) * 1_000_000)
+ optval = [secs, usecs].pack("l_2")
+ sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
+ sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
+ end
+ sock.connect(Socket.pack_sockaddr_in(port, addr[0][3]))
+ sock
+ end
+
+ def self.is_network_available?
+ begin
+ self.connect_to("192.0.32.10", 80, 5)
+ true
+ rescue
+ false
+ end
+ end
+end
View
9 spec/spec_helper.rb
@@ -12,11 +12,17 @@
require 'webmock/rspec'
+require 'network_connection'
+
require 'json'
RSpec.configure do |config|
config.include WebMock::API
- if ENV["NO_CONNECTION"]
+ unless NetworkConnection.is_network_available?
+ warn("No network connectivity. Only examples which do not make real network connections will run.")
+ no_network_connection = true
+ end
+ if ENV["NO_CONNECTION"] || no_network_connection
config.filter_run_excluding :net_connect => true
end
end
@@ -58,3 +64,4 @@ def client_specific_request_string(string)
end
string
end
+
Please sign in to comment.
Something went wrong with that request. Please try again.