communicate/communicate_test.go

83 lines
1.7 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-30 05:44:35 +00:00
e := events.NewEvent(events.CENTRAL_EVENT, events.CollectorInstallEvent{Version: "Test"})
2016-11-28 05:13:15 +00:00
Send(e)
2016-11-30 05:44:35 +00:00
// t.Log(e.GetResult())
2016-11-28 05:13:15 +00:00
}
}
func TestRealSendByGin(t *testing.T) {
2016-11-28 05:44:24 +00:00
SetRootURL("http://localhost:8080")
2016-11-28 05:13:15 +00:00
e := events.NewEvent(events.CENTRAL_EVENT, events.NewInstallEvent("TestInstallEvent"))
data, _ := json.Marshal(&e)
var u events.URLMaker
u = e.Data.(events.URLMaker)
2016-11-28 06:00:12 +00:00
t.Log(GetRootURL() + u.GetUrl())
2016-11-28 05:13:15 +00:00
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))
}
}