Module: Turbo::Train::TestHelper

Defined in:
lib/turbo/train/test_helper.rb

Instance Method Summary collapse

Instance Method Details

#after_teardownObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/turbo/train/test_helper.rb', line 21

def after_teardown
  test_server = case ENV.fetch('TURBO_TRAIN_TEST_SERVER', 'mercure').to_sym
                when :mercure
                  Turbo::Train.mercure_server
                when :fanout
                  Turbo::Train.fanout_server
                when :anycable
                  Turbo::Train.anycable_server
                else
                  raise "Unknown test server: #{ENV['TURBO_TRAIN_TEST_SERVER']}"
                end

  super
  Turbo::Train.instance_variable_set(:@server, test_server)
end

#assert_body_match(r) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/turbo/train/test_helper.rb', line 62

def assert_body_match(r)
  if Turbo::Train.server.real_server.is_a?(Turbo::Train::FanoutServer)
    assert_match "Published\n", r.body
  elsif Turbo::Train.server.real_server.is_a?(Turbo::Train::MercureServer)
    assert_match /urn:uuid:.*/, r.body
  elsif Turbo::Train.server.real_server.is_a?(Turbo::Train::AnycableServer)
    assert_match '', r.body
  else
    raise "Unknown server type"
  end
end

#assert_broadcast_on(stream, data, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/turbo/train/test_helper.rb', line 37

def assert_broadcast_on(stream, data, &block)
  signed_stream_name = Turbo::Train.signed_stream_name(stream)

  new_messages = Turbo::Train.server.broadcasts(signed_stream_name)
  if block_given?
    old_messages = new_messages
    Turbo::Train.server.clear_messages(signed_stream_name)

    assert_nothing_raised(&block)
    new_messages = Turbo::Train.server.broadcasts(signed_stream_name)
    Turbo::Train.server.clear_messages(signed_stream_name)

    # Restore all sent messages
    (old_messages + new_messages).each { |m| Turbo::Train.server.broadcasts(signed_stream_name) << m }
  end

  message = new_messages.find { |msg| msg == data }
  if message.nil?
    puts "signed_stream_name => #{signed_stream_name}"
    puts "channels_data: #{Turbo::Train.server.channels_data.inspect}}"
  end

  assert message, "No messages sent with #{data} to #{Turbo::Train.stream_name_from(stream)}"
end

#assert_code_ok(r) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/turbo/train/test_helper.rb', line 74

def assert_code_ok(r)
  if Turbo::Train.server.real_server.is_a?(Turbo::Train::FanoutServer)
    assert_equal r.code, '200'
  elsif Turbo::Train.server.real_server.is_a?(Turbo::Train::MercureServer)
    assert_equal r.code, '200'
  elsif Turbo::Train.server.real_server.is_a?(Turbo::Train::AnycableServer)
    assert_equal r.code, '201'
  else
    raise "Unknown server type"
  end
end

#assert_response_from_fanout_server(r) ⇒ Object



91
92
93
94
# File 'lib/turbo/train/test_helper.rb', line 91

def assert_response_from_fanout_server(r)
  assert_equal r.code, '200'
  assert_match "Published\n", r.body
end

#assert_response_from_mercure_server(r) ⇒ Object



86
87
88
89
# File 'lib/turbo/train/test_helper.rb', line 86

def assert_response_from_mercure_server(r)
  assert_equal r.code, '200'
  assert_match /urn:uuid:.*/, r.body
end

#before_setupObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/turbo/train/test_helper.rb', line 5

def before_setup
  test_server = case ENV.fetch('TURBO_TRAIN_TEST_SERVER', 'mercure').to_sym
                when :mercure
                  Turbo::Train::TestServer.new(Turbo::Train.mercure_server, Turbo::Train.configuration)
                when :fanout
                  Turbo::Train::TestServer.new(Turbo::Train.fanout_server, Turbo::Train.configuration)
                when :anycable
                  Turbo::Train::TestServer.new(Turbo::Train.anycable_server, Turbo::Train.configuration)
                else
                  raise "Unknown test server: #{ENV['TURBO_TRAIN_TEST_SERVER']}"
                end

  Turbo::Train.instance_variable_set(:@server, test_server)
  super
end