gorush/notify/feedback_test.go

76 lines
1.5 KiB
Go
Raw Permalink Normal View History

package notify
import (
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/appleboy/gorush/config"
2021-07-13 08:32:39 +00:00
"github.com/appleboy/gorush/logx"
"github.com/stretchr/testify/assert"
)
func TestEmptyFeedbackURL(t *testing.T) {
cfg, _ := config.LoadConf()
2021-07-13 08:32:39 +00:00
logEntry := logx.LogPushEntry{
ID: "",
Type: "",
Platform: "",
Token: "",
Message: "",
Error: "",
}
err := DispatchFeedback(logEntry, cfg.Core.FeedbackURL, cfg.Core.FeedbackTimeout)
assert.NotNil(t, err)
}
func TestHTTPErrorInFeedbackCall(t *testing.T) {
cfg, _ := config.LoadConf()
cfg.Core.FeedbackURL = "http://test.example.com/api/"
2021-07-13 08:32:39 +00:00
logEntry := logx.LogPushEntry{
ID: "",
Type: "",
Platform: "",
Token: "",
Message: "",
Error: "",
}
err := DispatchFeedback(logEntry, cfg.Core.FeedbackURL, cfg.Core.FeedbackTimeout)
assert.NotNil(t, err)
}
func TestSuccessfulFeedbackCall(t *testing.T) {
// Mock http server
httpMock := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/dispatch" {
w.Header().Add("Content-Type", "application/json")
_, err := w.Write([]byte(`{}`))
if err != nil {
log.Println(err)
panic(err)
}
}
}),
)
defer httpMock.Close()
cfg, _ := config.LoadConf()
cfg.Core.FeedbackURL = httpMock.URL
2021-07-13 08:32:39 +00:00
logEntry := logx.LogPushEntry{
ID: "",
Type: "",
Platform: "",
Token: "",
Message: "",
Error: "",
}
err := DispatchFeedback(logEntry, cfg.Core.FeedbackURL, cfg.Core.FeedbackTimeout)
assert.Nil(t, err)
}