Пример #1
0
func TestSpiritHandlerGet(t *testing.T) {

	// get mock dao
	daoMock, _ := dao.GetSpiritDAO("", dao.DAOMock)
	handler := NewSpiritHandler(daoMock)

	// build a request
	req, err := http.NewRequest("GET", "localhost/spirits", nil)
	if err != nil {
		t.Fatal(err)
	}

	// build the recorder
	res := httptest.NewRecorder()

	// execute the query
	handler.GetAll(res, req)

	var spiritOut []model.Spirit
	json.NewDecoder(res.Body).Decode(&spiritOut)

	if err != nil {
		t.Errorf("Unable to get JSON content %v", err)
	}

	if res.Code != http.StatusOK {
		t.Error("Wrong response code")
	}

	if dao.MockedSpirit != spiritOut[0] {
		t.Errorf("Expected different from %v output %v", dao.MockedSpirit, spiritOut[0])
	}
}
Пример #2
0
func BenchmarkSpiritHandlerGet(t *testing.B) {

	// tooling purpose
	_ = new([45000]int)

	// get mock dao
	daoMock, _ := dao.GetSpiritDAO("", dao.DAOMock)
	handler := NewSpiritHandler(daoMock)

	// build a request
	req, err := http.NewRequest("GET", "localhost/spirits", nil)
	if err != nil {
		t.Fatal(err)
	}

	// build the recorder
	res := httptest.NewRecorder()

	// execute the query
	handler.GetAll(res, req)

	var spiritOut []model.Spirit
	err = json.NewDecoder(res.Body).Decode(&spiritOut)

	if err != nil {
		t.Errorf("Unable to get JSON content %v", err)
	}

	expected := model.Spirit{
		Name:         "Caroni",
		Distiller:    "Caroni",
		Bottler:      "Velier",
		Country:      "Trinidad",
		Composition:  "Molasse",
		SpiritType:   model.TypeRhum,
		Age:          15,
		BottlingDate: time.Date(2015, 01, 01, 0, 0, 0, 0, time.UTC),
		Score:        8.5,
		Comment:      "heavy tire taste",
	}

	if expected != spiritOut[0] {
		t.Errorf("Expected different from %v output %v", expected, spiritOut)
	}
}
Пример #3
0
// BuildWebServer constructs a new web server with the right DAO and spirits handler
func BuildWebServer(db string, daoType int, statisticsDuration time.Duration) (*negroni.Negroni, error) {

	// spirit dao
	dao, err := dao.GetSpiritDAO(db, daoType)
	if err != nil {
		logger.WithField("error", err).WithField("connection string", db).Fatal("unable to connect to mongo db")
		return nil, err
	}

	// web server
	n := negroni.New()

	// new handler
	handler := NewSpiritHandler(dao)

	// new router
	router := NewRouter(handler)

	// add middleware for logging
	n.Use(negronilogrus.NewMiddlewareFromLogger(logger.StandardLogger(), "spirit"))

	// add recovery middleware in case of panic in handler func
	recovery := negroni.NewRecovery()
	recovery.PrintStack = false
	n.Use(recovery)

	// add statistics middleware
	n.Use(NewStatisticsMiddleware(statisticsDuration))

	// add as many middleware as you like

	// handler goes last
	n.UseHandler(router)

	return n, nil
}