communicate/communicate_test.go

81 lines
1.6 KiB
Go
Raw Normal View History

2016-11-28 05:13:15 +00:00
package communicate
import (
"bytes"
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"gopkg.in/gin-gonic/gin.v1"
"io/ioutil"
"loafle.com/commons/communicate/events"
"net/http"
"net/http/httptest"
"testing"
)
func TestCommunicatorInit(t *testing.T) {
assert.NotNil(t, _c)
}
func makeGin() *gin.Engine {
r := gin.New()
api := r.Group("/_api")
{
collector := api.Group("/collector")
{
event := collector.Group("/event")
{
{
types := event.Group("/status")
{
types.POST("/:type", func(c *gin.Context) {
fmt.Println("called /_api/collector/event/status/:type")
var j events.Event
c.BindJSON(&j)
fmt.Println(j)
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
}
}
}
}
}
return r
}
func TestSend(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
defer ts.Close()
for i := 0; i < 10; i++ {
2016-11-28 05:20:54 +00:00
e := events.NewEvent(events.CENTRAL_EVENT, events.CollectorInstallEvent{Message: "Test"})
2016-11-28 05:13:15 +00:00
Send(e)
t.Log(e.GetResult())
}
}
func TestRealSendByGin(t *testing.T) {
e := events.NewEvent(events.CENTRAL_EVENT, events.NewInstallEvent("TestInstallEvent"))
data, _ := json.Marshal(&e)
var u events.URLMaker
u = e.Data.(events.URLMaker)
t.Log(u.GetUrl())
req := httptest.NewRequest("POST", u.GetUrl(), bytes.NewReader(data))
w := httptest.NewRecorder()
g := makeGin()
g.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Home page didn't return %v", http.StatusOK)
} else {
t.Log("OKOKOK")
data, _ := ioutil.ReadAll(w.Body)
t.Log(string(data))
}
}