Skip to content

Commit

Permalink
feat: add unit tests to handlers
Browse files Browse the repository at this point in the history
- adds unit tests to handlers
- update to respond 404 on file not found
- update method of vuejs handler response
  • Loading branch information
BobyMCbobs committed Dec 19, 2023
1 parent bc9f70f commit 50f91a4
Show file tree
Hide file tree
Showing 3 changed files with 823 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ lint_backend:
script:
- golint -set_exit_status ./...

gotest:
stage: lint
image: $IMAGE_GOLANG_ALPINE
script:
- go test -cover -v ./...

govet:
stage: lint
image: $IMAGE_GOLANG_ALPINE
Expand Down
19 changes: 14 additions & 5 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package handlers

import (
"bytes"
"fmt"
"html/template"
"log"
"net/http"
Expand Down Expand Up @@ -60,12 +62,17 @@ func (h *Handler) serveHandlerVuejsHistoryMode() http.Handler {
indexPath := path.Join(h.ServeFolder, "/index.html")
tmpl, err := template.ParseFiles(indexPath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Println("warning: unable to parse template html:", err)
http.Error(w, "500 internal error", http.StatusInternalServerError)
return
}
if err := tmpl.Execute(w, h.TemplateMap); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
var buf bytes.Buffer
defer buf.Reset()
if err := tmpl.ExecuteTemplate(&buf, tmpl.Name(), h.TemplateMap); err != nil {
log.Println("warning: unable to execute template html:", err)
http.Error(w, "500 internal error", http.StatusInternalServerError)
}
fmt.Fprint(w, &buf)
})
}

Expand All @@ -86,8 +93,9 @@ func (h *Handler) serveHandlerStandard() http.Handler {
}
}
if _, err := os.Stat(path.Join(h.ServeFolder, req.URL.Path)); err != nil || isDisallowed {
req.URL.Path = h.Error404FilePath
req.RequestURI = req.URL.Path
w.WriteHeader(http.StatusNotFound)
http.ServeFile(w, req, path.Join(h.ServeFolder, h.Error404FilePath))
return
}
handler.ServeHTTP(w, req)
})
Expand Down Expand Up @@ -116,6 +124,7 @@ func (h *Handler) ServeStandardRedirect(from string, to string) http.HandlerFunc
toURL, err := url.Parse(to)
if err != nil {
log.Printf("Unable to parse redirection destination URL '%v' for route '%v'\n", to, from)
http.Error(w, "fatal: unable to redirect to destination URL", http.StatusInternalServerError)
return
}
toURL.RawQuery = req.URL.Query().Encode()
Expand Down
Loading

0 comments on commit 50f91a4

Please sign in to comment.