83 lines
1.6 KiB
Go
83 lines
1.6 KiB
Go
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++ {
|
|
e := events.NewEvent(events.CENTRAL_EVENT, events.CollectorInstallEvent{Message: "Test"})
|
|
Send(e)
|
|
t.Log(e.GetResult())
|
|
}
|
|
}
|
|
|
|
func TestRealSendByGin(t *testing.T) {
|
|
|
|
SetRootURL("http://localhost:8080")
|
|
|
|
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))
|
|
}
|
|
}
|