Skip to content

Commit

Permalink
Enhance server and tests error handling
Browse files Browse the repository at this point in the history
- Add regex validation and better error handling in EditThread
- Modify NewTsNetServer to accept hostname parameter
- Implement new test cases for EditThread and EditThreadPost
- Add TestNewDiscussService and improve existing tests
- Refactor form data handling and add mock implementations

Change-Id: I4a971faa0882ce5d525c94b5a3e3676b55a9a2f3
Signed-off-by: Ian Meyer <[email protected]>
  • Loading branch information
imeyer committed Oct 11, 2024
1 parent 55a13cc commit 7e7172d
Show file tree
Hide file tree
Showing 2 changed files with 267 additions and 33 deletions.
33 changes: 22 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"syscall"
"time"
Expand Down Expand Up @@ -142,10 +143,10 @@ func NewDiscussService(tailClient TailscaleClient,
}
}

func NewTsNetServer(dataDir *string) *tsnet.Server {
func NewTsNetServer(dataDir *string, hostname string) *tsnet.Server {
return &tsnet.Server{
Dir: filepath.Join(*dataDir, "tsnet"),
Hostname: *hostname,
Hostname: hostname,
UserLogf: tsnetlog.Discard,
Logf: tsnetlog.Discard,
}
Expand Down Expand Up @@ -186,7 +187,7 @@ func setupTsNetServer(logger *slog.Logger) *tsnet.Server {
logger.Info(fmt.Sprintf("creating configuration directory (%s) failed: %v", *dataDir, err), "data-dir", *dataDir)
}

s := NewTsNetServer(dataDir)
s := NewTsNetServer(dataDir, *hostname)

if *tsnetLog {
s.UserLogf = log.Printf
Expand Down Expand Up @@ -519,13 +520,10 @@ func (s *DiscussService) EditThread(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
s.editThreadGET(w, r)
return
case http.MethodPost:
s.editThreadPOST(w, r)
return
default:
s.renderError(w, http.StatusMethodNotAllowed)
return
}
}

Expand All @@ -536,6 +534,21 @@ func (s *DiscussService) editThreadPOST(w http.ResponseWriter, r *http.Request)
return
}

if err := r.ParseForm(); err != nil {
s.logger.ErrorContext(r.Context(), "ParseForm", slog.String("error", err.Error()))
s.renderError(w, http.StatusBadRequest)
return
}

re := regexp.MustCompile(`^/thread/\d+/edit$`)
matches := re.FindStringSubmatch(r.URL.Path)
s.logger.DebugContext(r.Context(), "editThreadPOST", slog.String("path", r.URL.Path), slog.Any("matches", matches))
if len(matches) < 1 {
s.logger.ErrorContext(r.Context(), "Invalid path")
s.renderError(w, http.StatusBadRequest)
return
}

threadIDStr := r.PathValue("tid")
threadID, err := strconv.ParseInt(threadIDStr, 10, 64)
if err != nil {
Expand Down Expand Up @@ -571,6 +584,7 @@ func (s *DiscussService) editThreadPOST(w http.ResponseWriter, r *http.Request)
MemberID: user.ID,
})
if err != nil {
s.logger.ErrorContext(r.Context(), err.Error())
s.renderError(w, http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -665,13 +679,10 @@ func (s *DiscussService) EditThreadPost(w http.ResponseWriter, r *http.Request)
switch r.Method {
case http.MethodGet:
s.editThreadPostGET(w, r, threadID, postID)
return
case http.MethodPost:
s.editThreadPostPOST(w, r, threadID, postID)
return
default:
s.renderError(w, http.StatusMethodNotAllowed)
return
}
}

Expand All @@ -687,9 +698,9 @@ func (s *DiscussService) editThreadPostPOST(w http.ResponseWriter, r *http.Reque
return
}

// Parse the form data
if err := r.ParseForm(); err != nil {
s.renderError(w, http.StatusInternalServerError)
s.logger.ErrorContext(r.Context(), "ParseForm", slog.String("error", err.Error()))
s.renderError(w, http.StatusBadRequest)
return
}

Expand Down
Loading

0 comments on commit 7e7172d

Please sign in to comment.