Esempio n. 1
0
func TestGinLogger(t *testing.T) {
	Convey("Given a buffered logrus", t, func() {
		log := logrus.New()
		buf := &bytes.Buffer{}
		log.Out = buf

		Convey("And a configured HTTP server", func() {
			gin.SetMode(gin.ReleaseMode)
			router := gin.New()

			router.Use(utils.GinLogger("API", log))

			router.GET("/", func(c *gin.Context) {
				c.JSON(200, "hello")
			})

			ts := httptest.NewServer(router)

			Convey("A message should be logged to the buffer upon a request", func() {
				resp, err := http.Get(ts.URL + "/")
				So(err, ShouldBeNil)

				body, err := ioutil.ReadAll(resp.Body)
				So(err, ShouldBeNil)
				So(string(body), ShouldEqual, "\"hello\"\n")

				So(buf.String(), ShouldContainSubstring, "API request finished")
				So(buf.String(), ShouldContainSubstring, "method=GET path=\"/\" status=200")
			})
		})
	})
}
Esempio n. 2
0
func TestGinRecovery(t *testing.T) {
	Convey("Given a configured HTTP server", t, func() {
		gin.SetMode(gin.ReleaseMode)
		router := gin.New()

		var rc *raven.Client
		router.Use(utils.GinRecovery(rc))

		router.GET("/test1", func(c *gin.Context) {
			panic("hello1")
		})
		router.GET("/test2", func(c *gin.Context) {
			panic(errors.New("hello2"))
		})
		router.GET("/test3", func(c *gin.Context) {
			c.JSON(200, "hello3")
		})

		ts := httptest.NewServer(router)

		Convey("String-valued panic should report to Sentry", func() {
			resp, err := http.Get(ts.URL + "/test1")
			So(err, ShouldBeNil)

			So(resp.StatusCode, ShouldEqual, 500)

			body, err := ioutil.ReadAll(resp.Body)
			So(err, ShouldBeNil)
			So(string(body), ShouldEqual, "{\"code\":500,\"error\":\"Internal server error\"}\n")
		})

		Convey("Error-valued panic should report to Sentry", func() {
			resp, err := http.Get(ts.URL + "/test2")
			So(err, ShouldBeNil)

			So(resp.StatusCode, ShouldEqual, 500)

			body, err := ioutil.ReadAll(resp.Body)
			So(err, ShouldBeNil)
			So(string(body), ShouldEqual, "{\"code\":500,\"error\":\"Internal server error\"}\n")
		})

		Convey("No panic should not report anything", func() {
			resp, err := http.Get(ts.URL + "/test3")
			So(err, ShouldBeNil)

			So(resp.StatusCode, ShouldEqual, 200)

			body, err := ioutil.ReadAll(resp.Body)
			So(err, ShouldBeNil)
			So(string(body), ShouldEqual, "\"hello3\"\n")

		})
	})
}
Esempio n. 3
0
File: api.go Progetto: pgpst/pgpst
func init() {
	gin.SetMode(gin.ReleaseMode)
}