api_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package guerrilla
  2. import (
  3. "github.com/flashmob/go-guerrilla/backends"
  4. "github.com/flashmob/go-guerrilla/log"
  5. "io/ioutil"
  6. "testing"
  7. "time"
  8. )
  9. // Test Starting smtp without setting up logger / backend
  10. func TestSMTP(t *testing.T) {
  11. smtp := SMTP{}
  12. err := smtp.Start()
  13. if err != nil {
  14. t.Error(err)
  15. }
  16. // it should set to stderr automatically
  17. if smtp.config.LogFile != log.OutputStderr.String() {
  18. t.Error("smtp.config.LogFile is not", log.OutputStderr.String())
  19. }
  20. if len(smtp.config.AllowedHosts) == 0 {
  21. t.Error("smtp.config.AllowedHosts len should be 1, not 0", smtp.config.AllowedHosts)
  22. }
  23. if smtp.config.LogLevel != "debug" {
  24. t.Error("smtp.config.LogLevel expected'debug', it is", smtp.config.LogLevel)
  25. }
  26. if len(smtp.config.Servers) != 1 {
  27. t.Error("len(smtp.config.Servers) should be 1, got", len(smtp.config.Servers))
  28. }
  29. time.Sleep(time.Second * 2)
  30. smtp.Shutdown()
  31. }
  32. // Suppressing log output
  33. func TestSMTPNoLog(t *testing.T) {
  34. // configure a default server with no log output
  35. cfg := &AppConfig{LogFile: log.OutputOff.String()}
  36. smtp := SMTP{config: cfg}
  37. err := smtp.Start()
  38. if err != nil {
  39. t.Error(err)
  40. }
  41. time.Sleep(time.Second * 2)
  42. smtp.Shutdown()
  43. }
  44. // our custom server
  45. func TestSMTPCustomServer(t *testing.T) {
  46. cfg := &AppConfig{LogFile: log.OutputStdout.String()}
  47. sc := ServerConfig{
  48. ListenInterface: "127.0.0.1:2526",
  49. IsEnabled: true,
  50. }
  51. cfg.Servers = append(cfg.Servers, sc)
  52. smtp := SMTP{config: cfg}
  53. err := smtp.Start()
  54. if err != nil {
  55. t.Error("start error", err)
  56. } else {
  57. time.Sleep(time.Second * 2)
  58. smtp.Shutdown()
  59. }
  60. }
  61. // with a backend config
  62. func TestSMTPCustomBackend(t *testing.T) {
  63. cfg := &AppConfig{LogFile: log.OutputStdout.String()}
  64. sc := ServerConfig{
  65. ListenInterface: "127.0.0.1:2526",
  66. IsEnabled: true,
  67. }
  68. cfg.Servers = append(cfg.Servers, sc)
  69. bcfg := backends.BackendConfig{
  70. "save_workers_size": 3,
  71. "process_stack": "HeadersParser|Header|Hasher|Debugger",
  72. "log_received_mails": true,
  73. }
  74. cfg.BackendConfig = bcfg
  75. smtp := SMTP{config: cfg}
  76. err := smtp.Start()
  77. if err != nil {
  78. t.Error("start error", err)
  79. } else {
  80. time.Sleep(time.Second * 2)
  81. smtp.Shutdown()
  82. }
  83. }
  84. // with a config from a json file
  85. func TestSMTPLoadFile(t *testing.T) {
  86. json := `{
  87. "log_file" : "./tests/testlog",
  88. "log_level" : "debug",
  89. "pid_file" : "/var/run/go-guerrilla.pid",
  90. "allowed_hosts": ["spam4.me","grr.la"],
  91. "backend_config" :
  92. {
  93. "log_received_mails" : true,
  94. "process_stack": "HeadersParser|Header|Hasher|Debugger",
  95. "save_workers_size": 3
  96. },
  97. "servers" : [
  98. {
  99. "is_enabled" : true,
  100. "host_name":"mail.guerrillamail.com",
  101. "max_size": 100017,
  102. "private_key_file":"config_test.go",
  103. "public_key_file":"config_test.go",
  104. "timeout":160,
  105. "listen_interface":"127.0.0.1:2526",
  106. "start_tls_on":false,
  107. "tls_always_on":false,
  108. "max_clients": 2
  109. }
  110. ]
  111. }
  112. `
  113. err := ioutil.WriteFile("goguerrilla.conf.api", []byte(json), 0644)
  114. if err != nil {
  115. t.Error("could not write guerrilla.conf.api", err)
  116. return
  117. }
  118. /*
  119. cfg := &AppConfig{LogFile: log.OutputStdout.String()}
  120. sc := ServerConfig{
  121. ListenInterface: "127.0.0.1:2526",
  122. IsEnabled: true,
  123. }
  124. cfg.Servers = append(cfg.Servers, sc)
  125. bcfg := backends.BackendConfig{
  126. "save_workers_size": 3,
  127. "process_stack": "HeadersParser|Header|Hasher|Debugger",
  128. "log_received_mails": true,
  129. }
  130. cfg.BackendConfig = bcfg
  131. smtp := SMTP{config: cfg}
  132. */
  133. smtp := SMTP{}
  134. err = smtp.ReadConfig("goguerrilla.conf.api")
  135. if err != nil {
  136. t.Error("ReadConfig error", err)
  137. return
  138. }
  139. err = smtp.Start()
  140. if err != nil {
  141. t.Error("start error", err)
  142. return
  143. } else {
  144. time.Sleep(time.Second * 2)
  145. smtp.Shutdown()
  146. }
  147. }