Module: Spikard::Testing

Defined in:
lib/spikard/testing.rb,
lib/spikard/response.rb

Overview

Testing helpers that wrap the native Ruby extension.

Defined Under Namespace

Classes: InlineSseEvent, Response, SseEvent, SseStream, TestClient, WebSocketMessage, WebSocketTestConnection

Class Method Summary collapse

Class Method Details

.create_test_client(app, config: nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/spikard/testing.rb', line 10

def create_test_client(app, config: nil)
  unless defined?(Spikard::Native::TestClient)
    raise LoadError, 'Spikard native test client is not available. Build the native extension before running tests.'
  end

  # Allow generated apps to stash a test config
  if config.nil? && app.instance_variable_defined?(:@__spikard_test_config)
    config = app.instance_variable_get(:@__spikard_test_config)
  end

  # Use default config if none provided
  config ||= Spikard::ServerConfig.new

  routes_json = JSON.generate(app.)
  handlers = app.handler_map.transform_keys(&:to_sym)
  ws_handlers = app.websocket_handlers || {}
  sse_producers = app.sse_producers || {}
  dependencies = app.dependencies || {}
  native = Spikard::Native::TestClient.new(routes_json, handlers, config, ws_handlers, sse_producers, dependencies)
  TestClient.new(native)
end