Skip to content

Commit

Permalink
smtp: Implemented HTTP router
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Hinderberger committed Jun 24, 2024
1 parent 98acb5b commit 71e410e
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 5 deletions.
2 changes: 1 addition & 1 deletion http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (ats *Suite) StartHttpServer() {

// Register SMTP server query routes
if ats.smtpServer != nil {
ats.smtpServer.RegisterRoutes(mux, "/")
ats.smtpServer.RegisterRoutes(mux, "/", ats.Config.LogShort)
}

ats.httpServer = http.Server{
Expand Down
122 changes: 118 additions & 4 deletions internal/smtp/http.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,124 @@
package smtp

import "net/http"
import (
"fmt"
"net/http"
"path"
"strconv"
"strings"

"github.com/programmfabrik/apitest/internal/httpproxy"
)

type smtpHTTPHandler struct {
server *Server
prefix string
}

// RegisterRoutes sets up HTTP routes for inspecting the SMTP Server's
// received messages.
func (s *Server) RegisterRoutes(mux *http.ServeMux, prefix string) {
// TODO
panic("not implemented")
func (s *Server) RegisterRoutes(mux *http.ServeMux, prefix string, skipLogs bool) {
handler := &smtpHTTPHandler{
server: s,
prefix: path.Join(prefix, "smtp"),
}

mux.Handle(handler.prefix, httpproxy.LogH(skipLogs, handler))
mux.Handle(handler.prefix+"/", httpproxy.LogH(skipLogs, handler))
}

func (h *smtpHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := path.Clean(r.URL.Path)
path = strings.TrimPrefix(path, h.prefix)
path = strings.TrimPrefix(path, "/")

if path == "" {
h.handleMessageIndex(w, r)
return
}

pathParts := strings.Split(path, "/")
fmt.Println(pathParts)

// We know that pathParts must have at least length 1, since empty path
// was already handled above.
idx, err := strconv.Atoi(pathParts[0])
if err != nil {
httpproxy.RespondWithErr(
w, http.StatusBadRequest,
fmt.Errorf("could not parse message index: %w", err),
)
return
}

switch len(pathParts) {
case 1:
h.handleMessageMeta(w, r, idx)
return
case 2:
switch pathParts[1] {
case "body":
h.handleMessageBody(w, r, idx)
return
case "multipart":
h.handleMultipartIndex(w, r, idx)
return
}
case 3, 4:
if pathParts[1] == "multipart" {
partIdx, err := strconv.Atoi(pathParts[2])
if err != nil {
httpproxy.RespondWithErr(
w, http.StatusBadRequest,
fmt.Errorf("could not parse multipart index: %w", err),
)
return
}

if len(pathParts) == 3 {
h.handleMultipartMeta(w, r, idx, partIdx)
return
} else if pathParts[3] == "body" {
h.handleMultipartBody(w, r, idx, partIdx)
return
}
}
}

// If routing failed, return status 404.
w.WriteHeader(http.StatusNotFound)
}

func (h *smtpHTTPHandler) handleMessageIndex(w http.ResponseWriter, r *http.Request) {
// TODO: Implement
fmt.Println("=== MESSAGE INDEX ===")
}

func (h *smtpHTTPHandler) handleMessageMeta(w http.ResponseWriter, r *http.Request, idx int) {
// TODO: Implement
fmt.Println("=== MESSAGE META ===", idx)
}

func (h *smtpHTTPHandler) handleMessageBody(w http.ResponseWriter, r *http.Request, idx int) {
// TODO: Implement
fmt.Println("=== MESSAGE BODY ===", idx)
}

func (h *smtpHTTPHandler) handleMultipartIndex(w http.ResponseWriter, r *http.Request, idx int) {
// TODO: Implement
fmt.Println("=== MULTIPART INDEX ===", idx)
}

func (h *smtpHTTPHandler) handleMultipartMeta(
w http.ResponseWriter, r *http.Request, idx, partIdx int,
) {
// TODO: Implement
fmt.Println("=== MULTIPART META ===", idx, partIdx)
}

func (h *smtpHTTPHandler) handleMultipartBody(
w http.ResponseWriter, r *http.Request, idx, partIdx int,
) {
// TODO: Implement
fmt.Println("=== MULTIPART BODY ===", idx, partIdx)
}

0 comments on commit 71e410e

Please sign in to comment.