diff options
author | elijah <elijah@riseup.net> | 2012-12-24 02:02:24 -0800 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2012-12-24 02:02:24 -0800 |
commit | 457c2bf135be68b3c49ff20d3f23a6f8507aeda5 (patch) | |
tree | 73afc8568c6a83706111b19d35ba04d0cd94b2e0 /test/test_helper.rb |
initial commit
Diffstat (limited to 'test/test_helper.rb')
-rw-r--r-- | test/test_helper.rb | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..031a90f --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,75 @@ +$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib') + +require 'rubygems' +require 'minitest/autorun' +require 'webmock/minitest' +require 'nickserver' + +class MiniTest::Unit::TestCase + # Add global extensions to the test case class here + + def setup + # by default, mock all non-localhost network connections + WebMock.disable_net_connect!(:allow_localhost => true) + end + + def file_content(filename) + (@file_contents ||= {})[filename] ||= File.read("%s/files/%s" % [File.dirname(__FILE__), filename]) + end + + def real_network + if ENV['REAL_NET'] == 'true' + WebMock.allow_net_connect! + yield + WebMock.disable_net_connect! + end + end + + def stub_vindex_response(uid, opts = {}) + options = {:status => 200, :body => ""}.merge(opts) + stub_http_request(:get, Nickserver::Config.sks_url).with( + :query => {:op => 'vindex', :search => uid, :exact => 'on', :options => 'mr', :fingerprint => 'on'} + ).to_return(options) + end + + def stub_get_response(key_id, opts = {}) + options = {:status => 200, :body => ""}.merge(opts) + stub_http_request(:get, Nickserver::Config.sks_url).with( + :query => {:op => 'get', :search => "0x"+key_id, :exact => 'on', :options => 'mr'} + ).to_return(options) + end + + #def without_webmock + # WebMock.disable_net_connect!(:allow_localhost => true) + # yield + # WebMock.disable_net_connect! + #end +end + +# +# a simple EM connection with callbacks for various lifecycle stages, +# useful for testing +# +class TestSocketClient < EventMachine::Connection + attr_writer :onopen, :onclose, :onmessage + attr_reader :data + + def initialize + @state = :new + @data = [] + end + + def receive_data(data) + @data << data + if @state == :new + @onopen.call if @onopen + @state = :open + else + @onmessage.call(data) if @onmessage + end + end + + def unbind + @onclose.call if @onclose + end +end |