1package http
2
3import (
4 "bytes"
5 "net/http"
6 "net/http/httptest"
7 "os"
8 "path/filepath"
9 "testing"
10
11 "github.com/mjl-/mox/mox-"
12)
13
14func TestServeHTTP(t *testing.T) {
15 os.RemoveAll("../testdata/web/data")
16 mox.ConfigStaticPath = filepath.FromSlash("../testdata/web/mox.conf")
17 mox.ConfigDynamicPath = filepath.Join(filepath.Dir(mox.ConfigStaticPath), "domains.conf")
18 mox.MustLoadConfig(true, false)
19
20 portSrvs := portServes(mox.Conf.Static.Listeners["local"])
21 srv := portSrvs[80]
22
23 test := func(method, target string, expCode int, expContent string, expHeaders map[string]string) {
24 t.Helper()
25
26 req := httptest.NewRequest(method, target, nil)
27 rw := httptest.NewRecorder()
28 rw.Body = &bytes.Buffer{}
29 srv.ServeHTTP(rw, req)
30 resp := rw.Result()
31 if resp.StatusCode != expCode {
32 t.Errorf("got statuscode %d, expected %d", resp.StatusCode, expCode)
33 }
34 if expContent != "" {
35 s := rw.Body.String()
36 if s != expContent {
37 t.Errorf("got response data %q, expected %q", s, expContent)
38 }
39 }
40 for k, v := range expHeaders {
41 if xv := resp.Header.Get(k); xv != v {
42 t.Errorf("got %q for header %q, expected %q", xv, k, v)
43 }
44 }
45 }
46
47 test("GET", "http://mta-sts.mox.example/.well-known/mta-sts.txt", http.StatusOK, "version: STSv1\nmode: enforce\nmax_age: 86400\nmx: mox.example\n", nil)
48 test("GET", "http://mox.example/.well-known/mta-sts.txt", http.StatusNotFound, "", nil) // mta-sts endpoint not in this domain.
49 test("GET", "http://mta-sts.mox.example/static/", http.StatusNotFound, "", nil) // static not served on this domain.
50 test("GET", "http://mta-sts.mox.example/other", http.StatusNotFound, "", nil)
51 test("GET", "http://mox.example/static/", http.StatusOK, "html\n", map[string]string{"X-Test": "mox"}) // index.html is served
52 test("GET", "http://mox.example/static/index.html", http.StatusOK, "html\n", map[string]string{"X-Test": "mox"})
53 test("GET", "http://mox.example/static/dir/", http.StatusOK, "", map[string]string{"X-Test": "mox"}) // Dir listing.
54 test("GET", "http://mox.example/other", http.StatusNotFound, "", nil)
55
56 // Webmail on IP, localhost, mail host, clientsettingsdomain, not others.
57 test("GET", "http://127.0.0.1/webmail/", http.StatusOK, "", nil)
58 test("GET", "http://localhost/webmail/", http.StatusOK, "", nil)
59 test("GET", "http://mox.example/webmail/", http.StatusOK, "", nil)
60 test("GET", "http://mail.mox.example/webmail/", http.StatusOK, "", nil)
61 test("GET", "http://mail.other.example/webmail/", http.StatusNotFound, "", nil)
62 test("GET", "http://remotehost/webmail/", http.StatusNotFound, "", nil)
63
64 // admin on IP, localhost, mail host, not clientsettingsdomain.
65 test("GET", "http://127.0.0.1/admin/", http.StatusOK, "", nil)
66 test("GET", "http://localhost/admin/", http.StatusOK, "", nil)
67 test("GET", "http://mox.example/admin/", http.StatusPermanentRedirect, "", nil) // Override by WebHandler.
68 test("GET", "http://mail.mox.example/admin/", http.StatusNotFound, "", nil)
69
70 // account is off.
71 test("GET", "http://127.0.0.1/", http.StatusNotFound, "", nil)
72 test("GET", "http://localhost/", http.StatusNotFound, "", nil)
73 test("GET", "http://mox.example/", http.StatusNotFound, "", nil)
74 test("GET", "http://mail.mox.example/", http.StatusNotFound, "", nil)
75}
76