Esempio n. 1
0
func TestEchoWithoutTestHelpers(t *testing.T) {
	s := neptulon.NewServer("127.0.0.1:3002")
	go s.ListenAndServe()
	time.Sleep(time.Millisecond * 30)
	defer s.Close()

	s.MiddlewareFunc(func(ctx *neptulon.ReqCtx) error {
		t.Log("Request received:", ctx.Method)
		ctx.Res = "response-wow!"
		return ctx.Next()
	})

	origin := "http://127.0.0.1"
	url := "ws://127.0.0.1:3002"
	ws, err := websocket.Dial(url, "", origin)
	if err != nil {
		t.Fatal(err)
	}
	if err := websocket.JSON.Send(ws, map[string]string{"id": "123", "method": "test"}); err != nil {
		t.Fatal(err)
	}
	var res interface{}
	if err := websocket.JSON.Receive(ws, &res); err != nil {
		t.Fatal(err)
	}
	t.Log("Got response:", res)

	if err := ws.Close(); err != nil {
		t.Fatal(err)
	}

	time.Sleep(time.Millisecond * 30)
}
Esempio n. 2
0
// NewServerHelper creates a new server helper object.
func NewServerHelper(t *testing.T) *ServerHelper {
	if testing.Short() {
		t.Skip("Skipping integration test in short testing mode.")
	}

	return &ServerHelper{
		Server:  neptulon.NewServer(laddr),
		Address: laddr,
		testing: t,
	}
}
Esempio n. 3
0
// NewServer creates a new server.
func NewServer(addr string) (*Server, error) {
	if (Conf == Config{}) {
		InitConf("")
	}

	s := Server{
		server: neptulon.NewServer(addr),
		db:     NewInMemDB(),
		queue:  NewQueue(),
	}

	s.server.MiddlewareFunc(middleware.Logger)
	s.pubRoutes = middleware.NewRouter()
	s.server.Middleware(s.pubRoutes)
	initPubRoutes(s.pubRoutes, s.db, Conf.App.JWTPass())

	//all communication below this point is authenticated
	s.server.MiddlewareFunc(jwt.HMAC(Conf.App.JWTPass()))
	s.server.Middleware(&s.queue)
	s.privRoutes = middleware.NewRouter()
	s.server.Middleware(s.privRoutes)
	initPrivRoutes(s.privRoutes, &s.queue)
	// r.Middleware(NotFoundHandler()) - 404-like handler

	// todo: research a better way to handle inner-circular dependencies so remove these lines back into Server contructor
	// (maybe via dereferencing: http://openmymind.net/Things-I-Wish-Someone-Had-Told-Me-About-Go/, but then initializers
	// actually using the pointer values would have to be lazy!)
	s.queue.SetServer(s.server)

	s.server.DisconnHandler(func(c *neptulon.Conn) {
		// only handle this event for previously authenticated
		if id, ok := c.Session.GetOk("userid"); ok {
			s.queue.RemoveConn(id.(string))
		}
	})

	return &s, nil
}