예제 #1
0
//Handlers  Returns a mux router containing all handlers for all routes
func Handlers(c *ctx.Context) *mux.Router {
	r := mux.NewRouter()
	//Each supported route is being added to the router
	trackHandler := ctx.NewFinalHandler(c, []byte(""), handleTrack)
	r.Handle("/track", middlewares.EnforceJSONHandler(middlewares.AuthHandler(middlewares.ValidateEventTrackingPayloadHandler(trackHandler)))).Methods("POST")
	connectedHandler := &ctx.Handler{c, handleConnected}
	r.Handle("/connected", middlewares.AuthHandler(connectedHandler)).Methods("GET")

	return r
}
func init() {
	mockedDb, err := sqlmock.New()
	if err != nil {
		log.Fatalf("An error: %s, was not expected when opening a stub database connection", err.Error())
	}

	columns := []string{"exists"}
	sqlmock.ExpectQuery("SELECT EXISTS\\(SELECT id FROM api_tokens WHERE token = (.+)\\)").
		WithArgs("123").
		WillReturnRows(sqlmock.NewRows(columns).AddRow(false))

	sqlmock.ExpectQuery("SELECT EXISTS\\(SELECT id FROM api_tokens WHERE token = (.+)\\)").
		WithArgs("456").
		WillReturnRows(sqlmock.NewRows(columns).AddRow(true))

	trackingValidator, err := validators.NewJSONEventTrackingValidator()
	if err != nil {
		log.Fatalf("An error: %s, was not expected when initializing tracking validator", err.Error())
	}

	router = mux.NewRouter()
	context := ctx.NewContext(datastores.NewAuthInstance(mockedDb), nil, nil, trackingValidator, "DEV")
	testHandler := ctx.NewHandler(context, testHandle)
	testFinalHander := ctx.NewFinalHandler(context, []byte(""), testFinalHandle)

	router.Handle("/testEnforceJSON", middlewares.EnforceJSONHandler(testHandler))
	router.Handle("/testAuth", middlewares.AuthHandler(testHandler))
	router.Handle("/testValidateEventTrackingPayloadHandler", middlewares.ValidateEventTrackingPayloadHandler(testFinalHander))

	server = httptest.NewServer(router)

	testEnforceJSONUrl = fmt.Sprintf("%s/testEnforceJSON", server.URL)
	testAuthUrl = fmt.Sprintf("%s/testAuth", server.URL)
	testValidateEventTrackingPayloadHandlerUrl = fmt.Sprintf("%s/testValidateEventTrackingPayloadHandler", server.URL)
}