github.com/gogf/gf@v1.16.9/net/gsmtp/gsmtp_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gsmtp_test
     8  
     9  import (
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/gogf/gf/net/gsmtp"
    14  )
    15  
    16  func TestAddress(t *testing.T) {
    17  	errMessage := "address is either empty or incorrect"
    18  
    19  	errValues := []string{
    20  		"",
    21  		":",
    22  		":25",
    23  		"localhost:",
    24  		"local.host:25:28",
    25  	}
    26  
    27  	for _, errValue := range errValues {
    28  		smtpConnection := gsmtp.New(errValue, "smtpUser@smtp.exmail.qq.com", "smtpPassword")
    29  		res := smtpConnection.SendMail("sender@local.host", "recipient1@domain.com;recipientN@anotherDomain.cn", "This is subject", "Hi! <br><br> This is body")
    30  		if !strings.Contains(res.Error(), errMessage) {
    31  			t.Errorf("Test failed on Address: %s", errValue)
    32  		}
    33  	}
    34  }
    35  
    36  func TestFrom(t *testing.T) {
    37  	errMessage := "from is invalid"
    38  
    39  	errValues := []string{
    40  		"",
    41  		"qwerty",
    42  		// "qwe@rty@com",
    43  		// "@rty",
    44  		// "qwe@",
    45  	}
    46  
    47  	for _, errValue := range errValues {
    48  		smtpConnection := gsmtp.New("smtp.exmail.qq.com", "smtpUser@smtp.exmail.qq.com", "smtpPassword")
    49  		res := smtpConnection.SendMail(errValue, "recipient1@domain.com;recipientN@anotherDomain.cn", "This is subject", "Hi! <br><br> This is body")
    50  		if !strings.Contains(res.Error(), errMessage) {
    51  			t.Errorf("Test failed on From: %s", errValue)
    52  		}
    53  	}
    54  
    55  }
    56  
    57  func TestTos(t *testing.T) {
    58  	errMessage := "tos if invalid"
    59  
    60  	errValues := []string{
    61  		"",
    62  		"qwerty",
    63  		"qwe;rty",
    64  		"qwe;rty;com",
    65  		// "qwe@rty@com",
    66  		// "@rty",
    67  		// "qwe@",
    68  	}
    69  
    70  	for _, errValue := range errValues {
    71  		smtpConnection := gsmtp.New("smtp.exmail.qq.com", "smtpUser@smtp.exmail.qq.com", "smtpPassword")
    72  		res := smtpConnection.SendMail("from@domain.com", errValue, "This is subject", "Hi! <br><br> This is body")
    73  		if !strings.Contains(res.Error(), errMessage) {
    74  			t.Errorf("Test failed on Tos: %s", errValue)
    75  		}
    76  	}
    77  
    78  }