Exemple #1
0
func TestResponse(t *testing.T) {
	w := httptest.NewRecorder()
	r := NewResponse(w)

	// SetWriter
	r.SetWriter(w)

	// Writer
	assert.Equal(t, w, r.Writer())

	// Header
	assert.NotNil(t, r.Header())

	// WriteHeader
	r.WriteHeader(http.StatusOK)
	assert.Equal(t, http.StatusOK, r.status)

	// Committed
	assert.True(t, r.committed)

	// Already committed
	r.WriteHeader(http.StatusTeapot)
	assert.NotEqual(t, http.StatusTeapot, r.Status())

	// Status
	r.status = http.StatusOK
	assert.Equal(t, http.StatusOK, r.Status())

	// Write
	s := "vodka"
	_, err := r.Write([]byte(s))
	assert.NoError(t, err)

	// Flush
	r.Flush()

	// Size
	assert.EqualValues(t, len(s), r.Size())

	// Committed
	assert.Equal(t, true, r.Committed())

	// Hijack
	assert.Panics(t, func() {
		r.Hijack()
	})

	// CloseNotify
	assert.Panics(t, func() {
		r.CloseNotify()
	})

	// reset
	r.reset(httptest.NewRecorder())
}
Exemple #2
0
func TestVodkaHandler(t *testing.T) {
	e := New()

	// HandlerFunc
	e.Get("/1", HandlerFunc(func(c *Context) error {
		return c.String(http.StatusOK, "1")
	}))

	// func(*vodka.Context) error
	e.Get("/2", func(c *Context) error {
		return c.String(http.StatusOK, "2")
	})

	// http.Handler/http.HandlerFunc
	e.Get("/3", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("3"))
	}))

	// func(http.ResponseWriter, *http.Request)
	e.Get("/4", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("4"))
	})

	for _, p := range []string{"1", "2", "3", "4"} {
		c, b := request(GET, "/"+p, e)
		assert.Equal(t, http.StatusOK, c)
		assert.Equal(t, p, b)
	}

	// Unknown
	assert.Panics(t, func() {
		e.Get("/5", nil)
	})
}
Exemple #3
0
func TestRouterAddInvalidMethod(t *testing.T) {
	e := New()
	r := e.router
	assert.Panics(t, func() {
		r.Add("INVALID", "/", func(*Context) error {
			return nil
		}, e)
	})
}
Exemple #4
0
func TestVodkaMiddleware(t *testing.T) {
	e := New()
	buf := new(bytes.Buffer)

	// vodka.MiddlewareFunc
	e.Use(MiddlewareFunc(func(h HandlerFunc) HandlerFunc {
		return func(c *Context) error {
			buf.WriteString("a")
			return h(c)
		}
	}))

	// func(vodka.HandlerFunc) vodka.HandlerFunc
	e.Use(func(h HandlerFunc) HandlerFunc {
		return func(c *Context) error {
			buf.WriteString("b")
			return h(c)
		}
	})

	// vodka.HandlerFunc
	e.Use(HandlerFunc(func(c *Context) error {
		buf.WriteString("c")
		return nil
	}))

	// func(*vodka.Context) error
	e.Use(func(c *Context) error {
		buf.WriteString("d")
		return nil
	})

	// func(http.Handler) http.Handler
	e.Use(func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			buf.WriteString("e")
			h.ServeHTTP(w, r)
		})
	})

	// http.Handler
	e.Use(http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		buf.WriteString("f")
	})))

	// http.HandlerFunc
	e.Use(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		buf.WriteString("g")
	}))

	// func(http.ResponseWriter, *http.Request)
	e.Use(func(w http.ResponseWriter, r *http.Request) {
		buf.WriteString("h")
	})

	// Unknown
	assert.Panics(t, func() {
		e.Use(nil)
	})

	// Route
	e.Get("/", func(c *Context) error {
		return c.String(http.StatusOK, "Hello!")
	})

	c, b := request(GET, "/", e)
	assert.Equal(t, "abcdefgh", buf.String())
	assert.Equal(t, http.StatusOK, c)
	assert.Equal(t, "Hello!", b)

	// Error
	e.Use(func(*Context) error {
		return errors.New("error")
	})
	c, b = request(GET, "/", e)
	assert.Equal(t, http.StatusInternalServerError, c)
}