Class: Cybertrain::Test::ReceivedMessage

Inherits:
Object
  • Object
show all
Defined in:
cybertrain/test/smtp_receiver.rb

Overview

What one SMTP connection carried.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#envelope_from ⇒ String (readonly)

Returns the MAIL FROM address, without the angle brackets.

Returns:

  • (String) —

    the MAIL FROM address, without the angle brackets



17
18
19
# File 'cybertrain/test/smtp_receiver.rb', line 17

def envelope_from
  @envelope_from
end

#envelope_recipients ⇒ Array<String> (readonly)

Returns the RCPT TO addresses the receiver accepted.

Returns:

  • (Array<String>) —

    the RCPT TO addresses the receiver accepted



21
22
23
# File 'cybertrain/test/smtp_receiver.rb', line 21

def envelope_recipients
  @envelope_recipients
end

#message_data ⇒ String (readonly)

Returns the message as it was sent after DATA: headers, a blank line, the body, CRLF line endings, dot-stuffing undone, the final “.” line removed; “” when no message was accepted.

Returns:

  • (String) —

    the message as it was sent after DATA: headers, a blank line, the body, CRLF line endings, dot-stuffing undone, the final “.” line removed; “” when no message was accepted



27
28
29
# File 'cybertrain/test/smtp_receiver.rb', line 27

def message_data
  @message_data
end

#transcript ⇒ Array<String> (readonly)

Returns every command line the client sent, in order (“EHLO localhost”, “MAIL FROM:a@example.com”, …, “QUIT”); the message itself is in #message_data, not here.

Returns:

  • (Array<String>) —

    every command line the client sent, in order (“EHLO localhost”, “MAIL FROM:a@example.com”, …, “QUIT”); the message itself is in #message_data, not here



33
34
35
# File 'cybertrain/test/smtp_receiver.rb', line 33

def transcript
  @transcript
end

Instance Method Details

#delivered? ⇒ Boolean

Whether a message was accepted (the 250 after the data was sent).

Returns:

  • (Boolean)


45
46
47
# File 'cybertrain/test/smtp_receiver.rb', line 45

def delivered?
  !@message_data.empty?
end

#message_body ⇒ String

The message body: what follows the blank line after the headers.

Returns:

  • (String)


52
53
54
55
56
57
# File 'cybertrain/test/smtp_receiver.rb', line 52

def message_body
  at = @message_data.byteindex("\r\n\r\n")
  return "" if at.nil?

  @message_data.byteslice(at + 4, @message_data.bytesize - at - 4)
end

#message_header(name) ⇒ String

The value of the first message header named name (any case), unfolded; “” when there is none.

Parameters:

  • name (String)

Returns:

  • (String)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'cybertrain/test/smtp_receiver.rb', line 64

def message_header(name)
  wanted = name.to_s.downcase
  data = @message_data
  stop = data.byteindex("\r\n\r\n")
  stop = data.bytesize if stop.nil?
  pos = 0
  found = false
  value = String.new
  while pos < stop
    eol = data.byteindex("\r\n", pos)
    eol = stop if eol.nil? || eol > stop
    line = data.byteslice(pos, eol - pos)
    pos = eol + 2
    continued = line.start_with?(" ") || line.start_with?("\t")
    if found
      break unless continued

      value << " " << line.strip
    elsif !continued
      colon = line.byteindex(":")
      if !colon.nil? && line.byteslice(0, colon).downcase == wanted
        found = true
        value << line.byteslice(colon + 1, line.bytesize - colon - 1).strip
      end
    end
  end
  value
end

#message_parts ⇒ Hash{String => String}

The message read back by Mailer::Message.parse: "subject" decoded, "text" and "html" decoded from base64 (or as sent), CRLF line endings kept; "html" is “” for a plain-text message.

Examples:

parts = smtp.received_message.message_parts
assert_includes parts["html"], "<p>Welcome"

Returns:

  • (Hash{String => String})


102
103
104
# File 'cybertrain/test/smtp_receiver.rb', line 102

def message_parts
  Mailer::Message.parse(@message_data)
end