mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
chore: more small lint/perf fixes (#493)
Signed-off-by: James Hillyerd <james@hillyerd.com>
This commit is contained in:
@@ -79,9 +79,9 @@ func (s *StoreManager) Deliver(
|
||||
tstamp := now.UTC().Format(recvdTimeFmt)
|
||||
|
||||
// Process inbound message through extensions.
|
||||
mailboxes := make([]string, len(recipients))
|
||||
for i, recip := range recipients {
|
||||
mailboxes[i] = recip.Mailbox
|
||||
mailboxes := make([]string, 0, len(recipients))
|
||||
for _, recip := range recipients {
|
||||
mailboxes = append(mailboxes, recip.Mailbox)
|
||||
}
|
||||
|
||||
// Construct InboundMessage event and process through extensions.
|
||||
|
||||
@@ -368,10 +368,10 @@ func TestGetMessage(t *testing.T) {
|
||||
|
||||
// Add a test message.
|
||||
subject := "getMessage1"
|
||||
id := addTestMessage(sm, "box1", subject)
|
||||
id := addTestMessage(sm, "get-box", subject)
|
||||
|
||||
// Verify retrieval of the test message.
|
||||
msg, err := sm.GetMessage("box1", id)
|
||||
msg, err := sm.GetMessage("get-box", id)
|
||||
require.NoError(t, err, "GetMessage must succeed")
|
||||
require.NotNil(t, msg, "GetMessage must return a result")
|
||||
assert.Equal(t, subject, msg.Subject)
|
||||
@@ -383,19 +383,19 @@ func TestMarkSeen(t *testing.T) {
|
||||
|
||||
// Add a test message.
|
||||
subject := "getMessage1"
|
||||
id := addTestMessage(sm, "box1", subject)
|
||||
id := addTestMessage(sm, "seen-box", subject)
|
||||
|
||||
// Verify test message unseen.
|
||||
msg, err := sm.GetMessage("box1", id)
|
||||
msg, err := sm.GetMessage("seen-box", id)
|
||||
require.NoError(t, err, "GetMessage must succeed")
|
||||
require.NotNil(t, msg, "GetMessage must return a result")
|
||||
assert.False(t, msg.Seen, "msg should be unseen")
|
||||
|
||||
err = sm.MarkSeen("box1", id)
|
||||
err = sm.MarkSeen("seen-box", id)
|
||||
require.NoError(t, err, "MarkSeen should succeed")
|
||||
|
||||
// Verify test message seen.
|
||||
msg, err = sm.GetMessage("box1", id)
|
||||
msg, err = sm.GetMessage("seen-box", id)
|
||||
require.NoError(t, err, "GetMessage must succeed")
|
||||
require.NotNil(t, msg, "GetMessage must return a result")
|
||||
assert.True(t, msg.Seen, "msg should have been seen")
|
||||
@@ -405,17 +405,17 @@ func TestRemoveMessage(t *testing.T) {
|
||||
sm, _ := testStoreManager()
|
||||
|
||||
// Add test messages.
|
||||
id1 := addTestMessage(sm, "box1", "subject 1")
|
||||
id2 := addTestMessage(sm, "box1", "subject 2")
|
||||
id3 := addTestMessage(sm, "box1", "subject 3")
|
||||
got, err := sm.GetMetadata("box1")
|
||||
id1 := addTestMessage(sm, "rm-box", "subject 1")
|
||||
id2 := addTestMessage(sm, "rm-box", "subject 2")
|
||||
id3 := addTestMessage(sm, "rm-box", "subject 3")
|
||||
got, err := sm.GetMetadata("rm-box")
|
||||
require.NoError(t, err)
|
||||
require.Len(t, got, 3)
|
||||
|
||||
// Delete message 2 and verify.
|
||||
err = sm.RemoveMessage("box1", id2)
|
||||
err = sm.RemoveMessage("rm-box", id2)
|
||||
require.NoError(t, err)
|
||||
got, err = sm.GetMetadata("box1")
|
||||
got, err = sm.GetMetadata("rm-box")
|
||||
require.NoError(t, err)
|
||||
require.Len(t, got, 2, "Should be 2 messages remaining")
|
||||
|
||||
@@ -431,17 +431,17 @@ func TestPurgeMessages(t *testing.T) {
|
||||
sm, _ := testStoreManager()
|
||||
|
||||
// Add test messages.
|
||||
_ = addTestMessage(sm, "box1", "subject 1")
|
||||
_ = addTestMessage(sm, "box1", "subject 2")
|
||||
_ = addTestMessage(sm, "box1", "subject 3")
|
||||
got, err := sm.GetMetadata("box1")
|
||||
_ = addTestMessage(sm, "purge-box", "subject 1")
|
||||
_ = addTestMessage(sm, "purge-box", "subject 2")
|
||||
_ = addTestMessage(sm, "purge-box", "subject 3")
|
||||
got, err := sm.GetMetadata("purge-box")
|
||||
require.NoError(t, err)
|
||||
require.Len(t, got, 3)
|
||||
|
||||
// Purge and verify.
|
||||
err = sm.PurgeMessages("box1")
|
||||
err = sm.PurgeMessages("purge-box")
|
||||
require.NoError(t, err)
|
||||
got, err = sm.GetMetadata("box1")
|
||||
got, err = sm.GetMetadata("purge-box")
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, got, "Purge should remove all mailbox messages")
|
||||
}
|
||||
|
||||
@@ -140,7 +140,7 @@ func (hub *Hub) RemoveListener(l Listener) {
|
||||
// for unit tests.
|
||||
func (hub *Hub) Sync() {
|
||||
done := make(chan struct{})
|
||||
hub.opChan <- func(h *Hub) {
|
||||
hub.opChan <- func(_ *Hub) {
|
||||
close(done)
|
||||
}
|
||||
<-done
|
||||
|
||||
@@ -306,15 +306,16 @@ LOOP:
|
||||
case c == '\\':
|
||||
inCharQuote = true
|
||||
case c == '"':
|
||||
if inCharQuote {
|
||||
switch {
|
||||
case inCharQuote:
|
||||
err = buf.WriteByte(c)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
inCharQuote = false
|
||||
} else if inStringQuote {
|
||||
case inStringQuote:
|
||||
inStringQuote = false
|
||||
} else {
|
||||
default:
|
||||
if i == 0 {
|
||||
inStringQuote = true
|
||||
} else {
|
||||
|
||||
@@ -13,7 +13,7 @@ type options struct {
|
||||
|
||||
// Option can apply itself to the private options type.
|
||||
type Option interface {
|
||||
apply(*options)
|
||||
apply(opts *options)
|
||||
}
|
||||
|
||||
func getDefaultOptions() *options {
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
// httpClient allows http.Client to be mocked for tests
|
||||
type httpClient interface {
|
||||
Do(*http.Request) (*http.Response, error)
|
||||
Do(req *http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
// Generic REST restClient
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const baseURLStr = "http://test.local:8080"
|
||||
@@ -78,10 +80,11 @@ func TestDoTable(t *testing.T) {
|
||||
t.Run(testname, func(t *testing.T) {
|
||||
mth := &mockHTTPClient{}
|
||||
c := &restClient{mth, test.base}
|
||||
_, err := c.do(test.method, test.uri, test.wantBody)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := c.do(test.method, test.uri, test.wantBody)
|
||||
require.NoError(t, err)
|
||||
err = resp.Body.Close()
|
||||
require.NoError(t, err)
|
||||
|
||||
if mth.req.Method != test.wantMethod {
|
||||
t.Errorf("req.Method == %q, want %q", mth.req.Method, test.wantMethod)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/inbucket/inbucket/v3/pkg/config"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
@@ -86,13 +85,13 @@ func requestLoggingWrapper(next http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
// spaTemplateHandler creates a handler to serve the index.html template for our SPA.
|
||||
func spaTemplateHandler(tmpl *template.Template, basePath string,
|
||||
webConfig config.Web) http.Handler {
|
||||
func spaTemplateHandler(tmpl *template.Template, basePath string) http.Handler {
|
||||
tmplData := struct {
|
||||
BasePath string
|
||||
}{
|
||||
BasePath: basePath,
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
// ensure we do now allow click jacking
|
||||
w.Header().Set("X-Frame-Options", "SameOrigin")
|
||||
|
||||
@@ -21,6 +21,6 @@ func TextToHTML(text string) string {
|
||||
|
||||
// WrapURL wraps a <a href> tag around the provided URL
|
||||
func WrapURL(url string) string {
|
||||
unescaped := strings.Replace(url, "&", "&", -1)
|
||||
unescaped := strings.ReplaceAll(url, "&", "&")
|
||||
return fmt.Sprintf("<a href=\"%s\" target=\"_blank\">%s</a>", unescaped, url)
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ func NewServer(conf *config.Root, mm message.Manager, mh *msghub.Hub) *Server {
|
||||
|
||||
// SPA managed paths.
|
||||
spaHandler := cookieHandler(appConfigCookie(conf.Web),
|
||||
spaTemplateHandler(indexTmpl, prefix("/"), conf.Web))
|
||||
spaTemplateHandler(indexTmpl, prefix("/")))
|
||||
Router.Path(prefix("/")).Handler(spaHandler)
|
||||
Router.Path(prefix("/monitor")).Handler(spaHandler)
|
||||
Router.Path(prefix("/status")).Handler(spaHandler)
|
||||
|
||||
@@ -282,8 +282,7 @@ func clearEnv() {
|
||||
}
|
||||
|
||||
// Backup ciritcal env variables.
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
if runtime.GOOS == "windows" {
|
||||
backup("SYSTEMROOT")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user