1 USING: smtp tools.test io.streams.string io.sockets
2 io.sockets.secure threads smtp.server kernel sequences
3 namespaces logging accessors assocs sorting smtp.private
4 concurrency.promises system ;
7 : with-test-smtp-config ( quot -- )
10 "p" get mock-smtp-server
13 "localhost" "p" get ?promise <inet> >>server
15 os unix? [ t >>tls? ] when
17 ] dip with-variable ; inline
19 { 0 0 } [ [ ] with-smtp-connection ] must-infer-as
21 [ "hello\nworld" validate-address ] must-fail
23 [ "slava@factorcode.org" ]
24 [ "slava@factorcode.org" validate-address ] unit-test
26 [ "aGVsbG8Kd29ybGQ=\r\n.\r\n" ] [
27 T{ email { body "hello\nworld" } } [ send-body ] with-string-writer
30 [ { "500 syntax error" } <response> check-response ]
31 [ smtp-error? ] must-fail-with
33 [ ] [ { "220 success" } <response> check-response ] unit-test
35 [ T{ response f 220 { "220 success" } } ] [
36 "220 success" [ receive-response ] with-string-reader
41 "220-a multiline response"
46 "220-a multiline response\r\n250-another line\r\n220 the end"
47 [ receive-response ] with-string-reader
51 "220-a multiline response\r\n250-another line\r\n220 the end"
52 [ get-ok ] with-string-reader
56 "Subject:\r\nsecurity hole" validate-header
61 { "Content-Transfer-Encoding" "base64" }
62 { "Content-Type" "text/plain; charset=UTF-8" }
63 { "From" "Doug <erg@factorcode.org>" }
64 { "MIME-Version" "1.0" }
65 { "Subject" "Factor rules" }
66 { "To" "Slava <slava@factorcode.org>, Ed <dharmatech@factorcode.org>" }
68 { "slava@factorcode.org" "dharmatech@factorcode.org" }
73 "Factor rules" >>subject
75 "Slava <slava@factorcode.org>"
76 "Ed <dharmatech@factorcode.org>"
78 "Doug <erg@factorcode.org>" >>from
80 email>headers sort-keys [
81 drop { "Date" "Message-Id" } member? not
84 [ to>> [ extract-email ] map ]
85 [ from>> extract-email ] tri
86 ] with-test-smtp-config
90 <secure-config> f >>verify [
93 "Hi guys\nBye guys" >>body
94 "Factor rules" >>subject
96 "Slava <slava@factorcode.org>"
97 "Ed <dharmatech@factorcode.org>"
99 "Doug <erg@factorcode.org>" >>from
101 ] with-test-smtp-config
102 ] with-secure-context