12 "github.com/mjl-/mox/dns"
13 "github.com/mjl-/mox/mox-"
16func TestServeHTTP(t *testing.T) {
17 os.RemoveAll("../testdata/web/data")
18 mox.ConfigStaticPath = filepath.FromSlash("../testdata/web/mox.conf")
19 mox.ConfigDynamicPath = filepath.Join(filepath.Dir(mox.ConfigStaticPath), "domains.conf")
20 mox.MustLoadConfig(true, false)
23 PathHandlers: []pathHandler{
25 HostMatch: func(dom dns.Domain) bool {
26 return strings.HasPrefix(dom.ASCII, "mta-sts.")
28 Path: "/.well-known/mta-sts.txt",
29 Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
30 w.Write([]byte("mta-sts!"))
37 test := func(method, target string, expCode int, expContent string, expHeaders map[string]string) {
40 req := httptest.NewRequest(method, target, nil)
41 rw := httptest.NewRecorder()
42 rw.Body = &bytes.Buffer{}
43 srv.ServeHTTP(rw, req)
45 if resp.StatusCode != expCode {
46 t.Fatalf("got statuscode %d, expected %d", resp.StatusCode, expCode)
51 t.Fatalf("got response data %q, expected %q", s, expContent)
54 for k, v := range expHeaders {
55 if xv := resp.Header.Get(k); xv != v {
56 t.Fatalf("got %q for header %q, expected %q", xv, k, v)
61 test("GET", "http://mta-sts.mox.example/.well-known/mta-sts.txt", http.StatusOK, "mta-sts!", nil)
62 test("GET", "http://mox.example/.well-known/mta-sts.txt", http.StatusNotFound, "", nil) // mta-sts endpoint not in this domain.
63 test("GET", "http://mta-sts.mox.example/static/", http.StatusNotFound, "", nil) // static not served on this domain.
64 test("GET", "http://mta-sts.mox.example/other", http.StatusNotFound, "", nil)
65 test("GET", "http://mox.example/static/", http.StatusOK, "html\n", map[string]string{"X-Test": "mox"}) // index.html is served
66 test("GET", "http://mox.example/static/index.html", http.StatusOK, "html\n", map[string]string{"X-Test": "mox"})
67 test("GET", "http://mox.example/static/dir/", http.StatusOK, "", map[string]string{"X-Test": "mox"}) // Dir listing.
68 test("GET", "http://mox.example/other", http.StatusNotFound, "", nil)