1# frozen_string_literal: true 2require 'net/smtp' 3require 'test/unit' 4 5module Net 6 class SMTP 7 class TestResponse < Test::Unit::TestCase 8 def test_capabilities 9 res = Response.parse("250-ubuntu-desktop\n250-PIPELINING\n250-SIZE 10240000\n250-VRFY\n250-ETRN\n250-STARTTLS\n250-ENHANCEDSTATUSCODES\n250 DSN\n") 10 11 capabilities = res.capabilities 12 %w{ PIPELINING SIZE VRFY STARTTLS ENHANCEDSTATUSCODES DSN}.each do |str| 13 assert capabilities.key?(str), str 14 end 15 end 16 17 def test_capabilities_default 18 res = Response.parse("250-ubuntu-desktop\n250-PIPELINING\n250 DSN\n") 19 assert_equal [], res.capabilities['PIPELINING'] 20 end 21 22 def test_capabilities_value 23 res = Response.parse("250-ubuntu-desktop\n250-SIZE 1234\n250 DSN\n") 24 assert_equal ['1234'], res.capabilities['SIZE'] 25 end 26 27 def test_capabilities_multi 28 res = Response.parse("250-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n") 29 assert_equal %w{1 2 3}, res.capabilities['SIZE'] 30 end 31 32 def test_bad_string 33 res = Response.parse("badstring") 34 assert_equal({}, res.capabilities) 35 end 36 37 def test_success? 38 res = Response.parse("250-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n") 39 assert res.success? 40 assert !res.continue? 41 end 42 43 # RFC 2821, Section 4.2.1 44 def test_continue? 45 res = Response.parse("3yz-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n") 46 assert !res.success? 47 assert res.continue? 48 end 49 50 def test_status_type_char 51 res = Response.parse("3yz-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n") 52 assert_equal '3', res.status_type_char 53 54 res = Response.parse("250-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n") 55 assert_equal '2', res.status_type_char 56 end 57 58 def test_message 59 res = Response.parse("250-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n") 60 assert_equal "250-ubuntu-desktop\n", res.message 61 end 62 63 def test_server_busy_exception 64 res = Response.parse("400 omg busy") 65 assert_equal Net::SMTPServerBusy, res.exception_class 66 res = Response.parse("410 omg busy") 67 assert_equal Net::SMTPServerBusy, res.exception_class 68 end 69 70 def test_syntax_error_exception 71 res = Response.parse("500 omg syntax error") 72 assert_equal Net::SMTPSyntaxError, res.exception_class 73 74 res = Response.parse("501 omg syntax error") 75 assert_equal Net::SMTPSyntaxError, res.exception_class 76 end 77 78 def test_authentication_exception 79 res = Response.parse("530 omg auth error") 80 assert_equal Net::SMTPAuthenticationError, res.exception_class 81 82 res = Response.parse("531 omg auth error") 83 assert_equal Net::SMTPAuthenticationError, res.exception_class 84 end 85 86 def test_fatal_error 87 res = Response.parse("510 omg fatal error") 88 assert_equal Net::SMTPFatalError, res.exception_class 89 90 res = Response.parse("511 omg fatal error") 91 assert_equal Net::SMTPFatalError, res.exception_class 92 end 93 94 def test_default_exception 95 res = Response.parse("250 omg fatal error") 96 assert_equal Net::SMTPUnknownError, res.exception_class 97 end 98 end 99 end 100end 101