128 lines
3.9 KiB
Go
128 lines
3.9 KiB
Go
//go:build standalone
|
|
|
|
package checker
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func formRequest(values url.Values) *httptest.ResponseRecorder {
|
|
_ = values
|
|
return httptest.NewRecorder()
|
|
}
|
|
|
|
func TestRenderForm_HasAllFields(t *testing.T) {
|
|
fields := (&smtpProvider{}).RenderForm()
|
|
got := map[string]bool{}
|
|
for _, f := range fields {
|
|
got[f.Id] = true
|
|
}
|
|
for _, want := range []string{"domain", "helo_name", "timeout", "test_null_sender", "test_postmaster", "test_open_relay", "test_probe_address"} {
|
|
if !got[want] {
|
|
t.Errorf("missing field %q", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseForm_Defaults(t *testing.T) {
|
|
v := url.Values{"domain": {"example.com"}}
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(v.Encode()))
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
opts, err := (&smtpProvider{}).ParseForm(r)
|
|
if err != nil {
|
|
t.Fatalf("ParseForm: %v", err)
|
|
}
|
|
if opts["domain"] != "example.com" {
|
|
t.Errorf("domain: %v", opts["domain"])
|
|
}
|
|
// Submitted form with no checkbox checks → false (per parseBool sentinel).
|
|
if opts["test_null_sender"].(bool) {
|
|
t.Error("expected test_null_sender=false on submitted form without checkbox")
|
|
}
|
|
}
|
|
|
|
func TestParseForm_TrimsAndStripsTrailingDot(t *testing.T) {
|
|
v := url.Values{"domain": {" example.com. "}}
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(v.Encode()))
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
opts, err := (&smtpProvider{}).ParseForm(r)
|
|
if err != nil {
|
|
t.Fatalf("ParseForm: %v", err)
|
|
}
|
|
if opts["domain"] != "example.com" {
|
|
t.Errorf("expected normalized domain, got %v", opts["domain"])
|
|
}
|
|
}
|
|
|
|
func TestParseForm_RejectsEmptyDomain(t *testing.T) {
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(""))
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
if _, err := (&smtpProvider{}).ParseForm(r); err == nil {
|
|
t.Fatal("expected error on empty domain")
|
|
}
|
|
}
|
|
|
|
func TestParseForm_RejectsNonNumericTimeout(t *testing.T) {
|
|
v := url.Values{"domain": {"example.com"}, "timeout": {"banana"}}
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(v.Encode()))
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
if _, err := (&smtpProvider{}).ParseForm(r); err == nil {
|
|
t.Fatal("expected error on non-numeric timeout")
|
|
}
|
|
}
|
|
|
|
func TestParseForm_PassesThroughCheckboxes(t *testing.T) {
|
|
v := url.Values{
|
|
"domain": {"example.com"},
|
|
"timeout": {"5"},
|
|
"helo_name": {"mx-checker.example.com"},
|
|
"test_null_sender": {"on"},
|
|
"test_postmaster": {"true"},
|
|
"test_open_relay": {"yes"},
|
|
"test_probe_address": {"postmaster@example.org"},
|
|
}
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(v.Encode()))
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
opts, err := (&smtpProvider{}).ParseForm(r)
|
|
if err != nil {
|
|
t.Fatalf("ParseForm: %v", err)
|
|
}
|
|
if opts["timeout"].(float64) != 5 {
|
|
t.Errorf("timeout: %v", opts["timeout"])
|
|
}
|
|
if !opts["test_null_sender"].(bool) || !opts["test_postmaster"].(bool) || !opts["test_open_relay"].(bool) {
|
|
t.Errorf("checkboxes: %+v", opts)
|
|
}
|
|
if opts["helo_name"] != "mx-checker.example.com" {
|
|
t.Errorf("helo_name: %v", opts["helo_name"])
|
|
}
|
|
if opts["test_probe_address"] != "postmaster@example.org" {
|
|
t.Errorf("probe addr: %v", opts["test_probe_address"])
|
|
}
|
|
}
|
|
|
|
func TestParseBool_DefaultWhenNotSubmitted(t *testing.T) {
|
|
r := httptest.NewRequest("GET", "/?", nil)
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatalf("ParseForm: %v", err)
|
|
}
|
|
if !parseBool(r, "feature", true) {
|
|
t.Error("missing key on non-submitted form should yield default")
|
|
}
|
|
}
|
|
|
|
func TestParseBool_FalsyValues(t *testing.T) {
|
|
cases := []string{"0", "false", "off", "no", "FALSE"}
|
|
for _, c := range cases {
|
|
r := httptest.NewRequest("GET", "/?domain=x&feature="+c, nil)
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatalf("parse: %v", err)
|
|
}
|
|
if parseBool(r, "feature", true) {
|
|
t.Errorf("value %q should be false", c)
|
|
}
|
|
}
|
|
}
|