2021-07-23 17:56:33 +00:00
|
|
|
package notify
|
2019-09-06 07:48:42 +00:00
|
|
|
|
|
|
|
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"
|
2020-02-24 14:18:50 +00:00
|
|
|
|
2019-09-06 07:48:42 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEmptyFeedbackURL(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
2021-07-13 08:32:39 +00:00
|
|
|
logEntry := logx.LogPushEntry{
|
2019-09-06 07:48:42 +00:00
|
|
|
ID: "",
|
|
|
|
Type: "",
|
|
|
|
Platform: "",
|
|
|
|
Token: "",
|
|
|
|
Message: "",
|
|
|
|
Error: "",
|
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
err := DispatchFeedback(logEntry, cfg.Core.FeedbackURL, cfg.Core.FeedbackTimeout)
|
2019-09-06 07:48:42 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPErrorInFeedbackCall(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
|
|
|
cfg.Core.FeedbackURL = "http://test.example.com/api/"
|
2021-07-13 08:32:39 +00:00
|
|
|
logEntry := logx.LogPushEntry{
|
2019-09-06 07:48:42 +00:00
|
|
|
ID: "",
|
|
|
|
Type: "",
|
|
|
|
Platform: "",
|
|
|
|
Token: "",
|
|
|
|
Message: "",
|
|
|
|
Error: "",
|
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
err := DispatchFeedback(logEntry, cfg.Core.FeedbackURL, cfg.Core.FeedbackTimeout)
|
2019-09-06 07:48:42 +00:00
|
|
|
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()
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
|
|
|
cfg.Core.FeedbackURL = httpMock.URL
|
2021-07-13 08:32:39 +00:00
|
|
|
logEntry := logx.LogPushEntry{
|
2019-09-06 07:48:42 +00:00
|
|
|
ID: "",
|
|
|
|
Type: "",
|
|
|
|
Platform: "",
|
|
|
|
Token: "",
|
|
|
|
Message: "",
|
|
|
|
Error: "",
|
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
err := DispatchFeedback(logEntry, cfg.Core.FeedbackURL, cfg.Core.FeedbackTimeout)
|
2019-09-06 07:48:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|