예제 #1
0
파일: server.go 프로젝트: insionng/vodka
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
}
예제 #2
0
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())
}
예제 #3
0
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()))
}
예제 #4
0
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())
}
예제 #5
0
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")))
}
예제 #6
0
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"))
}
예제 #7
0
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)
}
예제 #8
0
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)
}
예제 #9
0
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())
}
예제 #10
0
파일: response.go 프로젝트: insionng/vodka
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,
	}
}
예제 #11
0
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()))
}
예제 #12
0
파일: vodka.go 프로젝트: insionng/vodka
// 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
}
예제 #13
0
파일: server.go 프로젝트: insionng/vodka
// 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
}