func NewConfig(c *engine.Config) (s *Server) { s = &Server{ Server: new(http.Server), config: c, pool: &Pool{ request: sync.Pool{ New: func() interface{} { return &Request{} }, }, response: sync.Pool{ New: func() interface{} { return &Response{logger: s.logger} }, }, header: sync.Pool{ New: func() interface{} { return &Header{} }, }, url: sync.Pool{ New: func() interface{} { return &URL{} }, }, }, handler: engine.HandlerFunc(func(req engine.Request, res engine.Response) { panic("vodka: handler not set, use `Server#SetHandler()` to set it.") }), logger: log.New("vodka"), } return }
func TestResponseWriteHeader(t *testing.T) { rec := httptest.NewRecorder() res := NewResponse(rec, log.New("test")) res.WriteHeader(http.StatusOK) assert.True(t, res.Committed()) assert.Equal(t, http.StatusOK, res.Status()) }
func TestResponseWrite(t *testing.T) { c := new(fasthttp.RequestCtx) res := NewResponse(c, log.New("test")) res.Write([]byte("test")) assert.Equal(t, int64(4), res.Size()) assert.Equal(t, "test", string(c.Response.Body())) }
func TestResponseWriteHeader(t *testing.T) { c := new(fasthttp.RequestCtx) res := NewResponse(c, log.New("test")) res.WriteHeader(http.StatusOK) assert.True(t, res.Committed()) assert.Equal(t, http.StatusOK, res.Status()) }
func TestRequest(t *testing.T) { ctx := new(fast.RequestCtx) url, _ := url.Parse("http://github.com/insionng/vodka") ctx.Init(&fast.Request{}, fakeAddr{addr: "127.0.0.1"}, nil) ctx.Request.Read(bufio.NewReader(bytes.NewBufferString(test.MultipartRequest))) ctx.Request.SetRequestURI(url.String()) test.RequestTest(t, NewRequest(ctx, log.New("vodka"))) }
func TestResponseSetCookie(t *testing.T) { rec := httptest.NewRecorder() res := NewResponse(rec, log.New("test")) res.SetCookie(&Cookie{&http.Cookie{ Name: "name", Value: "Jon Snow", }}) assert.Equal(t, "name=Jon Snow", rec.Header().Get("Set-Cookie")) }
func TestResponseWrite(t *testing.T) { rec := httptest.NewRecorder() res := NewResponse(rec, log.New("test")) res.Write([]byte("test")) assert.Equal(t, int64(4), res.Size()) assert.Equal(t, "test", rec.Body.String()) res.Flush() assert.True(t, rec.Flushed) }
func TestVodkaLogger(t *testing.T) { e := New() l := log.New("test") e.SetLogger(l) assert.Equal(t, l, e.Logger()) e.SetLogOutput(ioutil.Discard) assert.Equal(t, l.Output(), ioutil.Discard) e.SetLogLevel(log.OFF) assert.Equal(t, l.Level(), log.OFF) }
func TestRequest(t *testing.T) { httpReq, _ := http.ReadRequest(bufio.NewReader(strings.NewReader(test.MultipartRequest))) url, _ := url.Parse("http://github.com/insionng/vodka") httpReq.URL = url httpReq.RemoteAddr = "127.0.0.1" req := NewRequest(httpReq, log.New("vodka")) test.RequestTest(t, req) nr, _ := http.NewRequest("GET", "/", nil) req.reset(nr, nil, nil) assert.Equal(t, "", req.Host()) }
func NewResponseRecorder() *ResponseRecorder { rec := httptest.NewRecorder() return &ResponseRecorder{ Response: &Response{ response: rec, header: &Header{rec.Header()}, writer: rec, logger: log.New("test"), }, Body: rec.Body, } }
func TestResponseSetCookie(t *testing.T) { c := new(fasthttp.RequestCtx) res := NewResponse(c, log.New("test")) cookie := new(fasthttp.Cookie) cookie.SetKey("name") cookie.SetValue("Jon Snow") res.SetCookie(&Cookie{cookie}) c.Response.Header.SetCookie(cookie) ck := new(fasthttp.Cookie) ck.SetKey("name") assert.True(t, c.Response.Header.Cookie(ck)) assert.Equal(t, "Jon Snow", string(ck.Value())) }
// New creates an instance of Vodka. func New() (e *Vodka) { e = &Vodka{maxParam: new(int)} e.pool.New = func() interface{} { return e.NewContext(nil, nil) } e.router = NewRouter(e) // Defaults e.SetHTTPErrorHandler(e.DefaultHTTPErrorHandler) e.SetBinder(&binder{}) l := glog.New("vodka") l.SetLevel(glog.OFF) e.SetLogger(l) return }
// WithConfig returns `Server` instance with provided config. func WithConfig(c engine.Config) (s *Server) { s = &Server{ Server: new(http.Server), config: c, pool: &pool{ request: sync.Pool{ New: func() interface{} { return &Request{logger: s.logger} }, }, response: sync.Pool{ New: func() interface{} { return &Response{logger: s.logger} }, }, responseAdapter: sync.Pool{ New: func() interface{} { return &responseAdapter{} }, }, header: sync.Pool{ New: func() interface{} { return &Header{} }, }, url: sync.Pool{ New: func() interface{} { return &URL{} }, }, }, handler: engine.HandlerFunc(func(req engine.Request, res engine.Response) { panic("vodka: handler not set, use `Server#SetHandler()` to set it.") }), logger: glog.New("vodka"), } s.ReadTimeout = c.ReadTimeout s.WriteTimeout = c.WriteTimeout s.Addr = c.Address s.Handler = s return }