Class: Cybertrain::Test::SentryReceiver

Inherits:
HttpReceiver show all
Defined in:
cybertrain/test/sentry_receiver.rb

Overview

A receiver for the Sentry sender: accepts up to max envelopes (50 by default), one connection each, and answers each with status (200: {"id":"..."}, as Sentry does; another status, such as 429, tests a Sentry that refuses).

Examples:

require "cybertrain/test/sentry_receiver"

test "a failed charge is reported" do
  sentry = Cybertrain::Test::SentryReceiver.new
  sentry.start
  Cybertrain.config.sentry_dsn = sentry.dsn
  Cybertrain.config.http_allow_private = true
  post "/orders", params: { "card" => "declined" }
  env = sentry.received_events(1)[0]
  assert_equal "PaymentError", env.sentry_field("exception.values.0.type")
end

Instance Attribute Summary

Attributes inherited from Receiver

#port

Instance Method Summary collapse

Methods inherited from HttpReceiver

#received_request, #received_requests, #respond_with, #url

Methods inherited from Receiver

#serving?, #start, #stop

Constructor Details

#initialize(status: 200, max: 50, accept_window: 10.0) ⇒ SentryReceiver

Returns a new instance of SentryReceiver.

Parameters:

  • status (Integer) (defaults to: 200) —

    the answer to every envelope

  • max (Integer) (defaults to: 50) —

    how many envelopes (connections) to accept

  • accept_window (Numeric) (defaults to: 10.0) —

    seconds to wait for each connection



179
180
181
182
# File 'cybertrain/test/sentry_receiver.rb', line 179

def initialize(status: 200, max: 50, accept_window: 10.0)
  super(status: status, headers: SentryReceiver.reply_headers, body: REPLY, max: max,
        accept_window: accept_window)
end

Instance Method Details

#dsn(project = "1", prefix = "") ⇒ String

A DSN that sends to this receiver, once started: "http://public@127.0.0.1:<port><prefix>/<project>".

Parameters:

  • project (String) (defaults to: "1") —

    the project id (digits)

  • prefix (String) (defaults to: "") —

    a path before it ("/sentry"), or “”

Returns:

  • (String)


198
199
200
# File 'cybertrain/test/sentry_receiver.rb', line 198

def dsn(project = "1", prefix = "")
  "http://public@127.0.0.1:#{@port}#{prefix}/#{project}"
end

#received_events(count, wait = 5.0) ⇒ Array<SentryEnvelope>

The envelopes that arrived, waiting at most wait seconds for count of them; fewer when the time ran out.

Parameters:

  • count (Integer)
  • wait (Numeric) (defaults to: 5.0)

Returns:



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'cybertrain/test/sentry_receiver.rb', line 208

def received_events(count, wait = 5.0)
  out = Array.new(0) { SentryEnvelope.new("", "", "", "") }
  received_requests(count, wait).each do |req|
    path = req.http_path
    auth = req.http_header("X-Sentry-Auth")
    type = req.http_header("Content-Type")
    body = req.http_body
    # The bytes as read are binary under CRuby (the preview); the
    # envelope is UTF-8 JSON text, so compare it as such (the
    # compiled String is the same bytes either way).
    unless body.encoding.to_s == "UTF-8"
      body = body.dup
      body.force_encoding("UTF-8")
    end
    out << SentryEnvelope.new(path, auth, type, body)
  end
  out
end