Пример #1
0
func TestRouterMatchAny(t *testing.T) {
	e := New()
	r := e.router

	// Routes
	r.Add(GET, "/", func(*Context) error {
		return nil
	}, e)
	r.Add(GET, "/*", func(*Context) error {
		return nil
	}, e)
	r.Add(GET, "/users/*", func(*Context) error {
		return nil
	}, e)
	c := NewContext(nil, nil, e)

	h, _ := r.Find(GET, "/", c)
	if assert.NotNil(t, h) {
		assert.Equal(t, "", c.P(0))
	}

	h, _ = r.Find(GET, "/download", c)
	if assert.NotNil(t, h) {
		assert.Equal(t, "download", c.P(0))
	}

	h, _ = r.Find(GET, "/users/joe", c)
	if assert.NotNil(t, h) {
		assert.Equal(t, "joe", c.P(0))
	}
}
Пример #2
0
func TestRouterAPI(t *testing.T) {
	e := New()
	r := e.router

	for _, route := range api {
		r.Add(route.Method, route.Path, func(c *Context) error {
			return nil
		}, e)
	}

	response := NewResponse(httptest.NewRecorder())

	c := NewContext(nil, response, e)
	for _, route := range api {
		h, _ := r.Find(route.Method, route.Path, c)
		if assert.NotNil(t, h) {
			for i, n := range c.pnames {
				if assert.NotEmpty(t, n) {
					assert.Equal(t, ":"+n, c.P(i))
				}
			}
			h(c)
		}
	}
}
Пример #3
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())
}
Пример #4
0
func TestRouterMultiRoute(t *testing.T) {
	e := New()
	r := e.router

	// Routes
	r.Add(GET, "/users", func(c *Context) error {
		c.Set("path", "/users")
		return nil
	}, e)
	r.Add(GET, "/users/:id", func(c *Context) error {
		return nil
	}, e)
	c := NewContext(nil, new(Response), e)

	// Route > /users
	h, _ := r.Find(GET, "/users", c)
	if assert.NotNil(t, h) {
		h(c)
		assert.Equal(t, "/users", c.Get("path"))
	}

	// Route > /users/:id
	h, _ = r.Find(GET, "/users/1", c)
	if assert.NotNil(t, h) {
		assert.Equal(t, "1", c.P(0))
	}

	// Route > /user
	h, _ = r.Find(GET, "/user", c)
	if assert.IsType(t, new(HTTPError), h(c)) {
		he := h(c).(*HTTPError)
		assert.Equal(t, http.StatusNotFound, he.code)
	}

	// Invalid Method for Resource
	c.response.writer = httptest.NewRecorder()
	h, _ = r.Find("INVALID", "/users", c)
	if assert.IsType(t, new(HTTPError), h(c)) {
		he := h(c).(*HTTPError)
		assert.Equal(t, http.StatusBadRequest, he.code)
	}
}
Пример #5
0
func TestRouterParam(t *testing.T) {
	e := New()
	r := e.router
	r.Add(GET, "/users/:id", func(c *Context) error {
		return nil
	}, e)
	c := NewContext(nil, nil, e)
	h, _ := r.Find(GET, "/users/1", c)
	if assert.NotNil(t, h) {
		assert.Equal(t, "1", c.P(0))
	}
}
Пример #6
0
func TestRouterParamNames(t *testing.T) {
	e := New()
	r := e.router

	// Routes
	r.Add(GET, "/users", func(c *Context) error {
		c.Set("path", "/users")
		return nil
	}, e)
	r.Add(GET, "/users/:id", func(c *Context) error {
		return nil
	}, e)
	r.Add(GET, "/users/:uid/files/:fid", func(c *Context) error {
		return nil
	}, e)
	c := NewContext(nil, nil, e)

	// Route > /users
	h, _ := r.Find(GET, "/users", c)
	if assert.NotNil(t, h) {
		h(c)
		assert.Equal(t, "/users", c.Get("path"))
	}

	// Route > /users/:id
	h, _ = r.Find(GET, "/users/1", c)
	if assert.NotNil(t, h) {
		assert.Equal(t, "id", c.pnames[0])
		assert.Equal(t, "1", c.P(0))
	}

	// Route > /users/:uid/files/:fid
	h, _ = r.Find(GET, "/users/1/files/1", c)
	if assert.NotNil(t, h) {
		assert.Equal(t, "uid", c.pnames[0])
		assert.Equal(t, "1", c.P(0))
		assert.Equal(t, "fid", c.pnames[1])
		assert.Equal(t, "1", c.P(1))
	}
}
Пример #7
0
func TestRouterMicroParam(t *testing.T) {
	e := New()
	r := e.router
	r.Add(GET, "/:a/:b/:c", func(c *Context) error {
		return nil
	}, e)
	c := NewContext(nil, nil, e)
	h, _ := r.Find(GET, "/1/2/3", c)
	if assert.NotNil(t, h) {
		assert.Equal(t, "1", c.P(0))
		assert.Equal(t, "2", c.P(1))
		assert.Equal(t, "3", c.P(2))
	}
}
Пример #8
0
func TestRouterStatic(t *testing.T) {
	e := New()
	r := e.router
	path := "/folders/a/files/vodka.gif"
	r.Add(GET, path, func(c *Context) error {
		c.Set("path", path)
		return nil
	}, e)
	c := NewContext(nil, nil, e)
	h, _ := r.Find(GET, path, c)
	if assert.NotNil(t, h) {
		h(c)
		assert.Equal(t, path, c.Get("path"))
	}
}
Пример #9
0
func TestRouterMixParamMatchAny(t *testing.T) {
	e := New()
	r := e.router

	// Route
	r.Add(GET, "/users/:id/*", func(c *Context) error {
		return nil
	}, e)
	c := NewContext(nil, nil, e)

	h, _ := r.Find(GET, "/users/joe/comments", c)
	if assert.NotNil(t, h) {
		h(c)
		assert.Equal(t, "joe", c.P(0))
	}
}
Пример #10
0
func TestVodka(t *testing.T) {
	e := New()
	req, _ := http.NewRequest(GET, "/", nil)
	rec := httptest.NewRecorder()
	c := NewContext(req, NewResponse(rec), e)

	// Router
	assert.NotNil(t, e.Router())

	// Debug
	e.SetDebug(true)
	assert.True(t, e.debug)

	// DefaultHTTPErrorHandler
	e.DefaultHTTPErrorHandler(errors.New("error"), c)
	assert.Equal(t, http.StatusInternalServerError, rec.Code)
}
Пример #11
0
func TestContext(t *testing.T) {
	userJSON := `{"id":"1","name":"Joe"}`
	userXML := `<user><id>1</id><name>Joe</name></user>`

	req, _ := http.NewRequest(POST, "/", strings.NewReader(userJSON))
	rec := httptest.NewRecorder()
	c := NewContext(req, NewResponse(rec), New())

	// Request
	assert.NotNil(t, c.Request())

	// Response
	assert.NotNil(t, c.Response())

	// Socket
	assert.Nil(t, c.Socket())

	// Param by id
	c.pnames = []string{"id"}
	c.pvalues = []string{"1"}
	assert.Equal(t, "1", c.P(0))

	// Param by name
	assert.Equal(t, "1", c.Param("id"))

	// Store
	c.Set("user", "Joe")
	assert.Equal(t, "Joe", c.Get("user"))

	//------
	// Bind
	//------

	// JSON
	testBind(t, c, "application/json")

	// XML
	c.request, _ = http.NewRequest(POST, "/", strings.NewReader(userXML))
	testBind(t, c, ApplicationXML)

	// Unsupported
	testBind(t, c, "")

	//--------
	// Render
	//--------

	tpl := &Template{
		templates: template.Must(template.New("hello").Parse("Hello, {{.}}!")),
	}
	c.vodka.SetRenderer(tpl)
	err := c.Render(http.StatusOK, "hello", "Joe")
	if assert.NoError(t, err) {
		assert.Equal(t, http.StatusOK, rec.Code)
		assert.Equal(t, "Hello, Joe!", rec.Body.String())
	}

	c.vodka.renderer = nil
	err = c.Render(http.StatusOK, "hello", "Joe")
	assert.Error(t, err)

	// JSON
	rec = httptest.NewRecorder()
	c = NewContext(req, NewResponse(rec), New())
	err = c.JSON(http.StatusOK, user{"1", "Joe"})
	if assert.NoError(t, err) {
		assert.Equal(t, http.StatusOK, rec.Code)
		assert.Equal(t, ApplicationJSONCharsetUTF8, rec.Header().Get(ContentType))
		assert.Equal(t, userJSON+"\n", rec.Body.String())
	}

	// JSONP
	rec = httptest.NewRecorder()
	c = NewContext(req, NewResponse(rec), New())
	callback := "callback"
	err = c.JSONP(http.StatusOK, callback, user{"1", "Joe"})
	if assert.NoError(t, err) {
		assert.Equal(t, http.StatusOK, rec.Code)
		assert.Equal(t, ApplicationJavaScriptCharsetUTF8, rec.Header().Get(ContentType))
		assert.Equal(t, callback+"("+userJSON+"\n);", rec.Body.String())
	}

	// XML
	rec = httptest.NewRecorder()
	c = NewContext(req, NewResponse(rec), New())
	err = c.XML(http.StatusOK, user{"1", "Joe"})
	if assert.NoError(t, err) {
		assert.Equal(t, http.StatusOK, rec.Code)
		assert.Equal(t, ApplicationXMLCharsetUTF8, rec.Header().Get(ContentType))
		assert.Equal(t, xml.Header, xml.Header, rec.Body.String())
	}

	// String
	rec = httptest.NewRecorder()
	c = NewContext(req, NewResponse(rec), New())
	err = c.String(http.StatusOK, "Hello, World!")
	if assert.NoError(t, err) {
		assert.Equal(t, http.StatusOK, rec.Code)
		assert.Equal(t, TextPlain, rec.Header().Get(ContentType))
		assert.Equal(t, "Hello, World!", rec.Body.String())
	}

	// HTML
	rec = httptest.NewRecorder()
	c = NewContext(req, NewResponse(rec), New())
	err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
	if assert.NoError(t, err) {
		assert.Equal(t, http.StatusOK, rec.Code)
		assert.Equal(t, TextHTMLCharsetUTF8, rec.Header().Get(ContentType))
		assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
	}

	// File
	rec = httptest.NewRecorder()
	c = NewContext(req, NewResponse(rec), New())
	err = c.File("test/fixture/walle.png", false)
	if assert.NoError(t, err) {
		assert.Equal(t, http.StatusOK, rec.Code)
		assert.Equal(t, 219885, rec.Body.Len())
	}

	// File as attachment
	rec = httptest.NewRecorder()
	c = NewContext(req, NewResponse(rec), New())
	err = c.File("test/fixture/walle.png", true)
	if assert.NoError(t, err) {
		assert.Equal(t, http.StatusOK, rec.Code)
		assert.Equal(t, rec.Header().Get(ContentDisposition), "attachment; filename=walle.png")
		assert.Equal(t, 219885, rec.Body.Len())
	}

	// Status
	rec = httptest.NewRecorder()
	c = NewContext(req, NewResponse(rec), New())
	c.Status(http.StatusOK)
	assert.Equal(t, http.StatusOK, c.response.status)

	// Redirect
	rec = httptest.NewRecorder()
	c = NewContext(req, NewResponse(rec), New())
	assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://github.com/insionng/vodka"))

	// Error
	rec = httptest.NewRecorder()
	c = NewContext(req, NewResponse(rec), New())
	c.Error(errors.New("error"))
	assert.Equal(t, http.StatusInternalServerError, c.response.status)

	// reset
	c.reset(req, NewResponse(httptest.NewRecorder()), New())
}
Пример #12
0
func TestRouterPriority(t *testing.T) {
	e := New()
	r := e.router

	// Routes
	r.Add(GET, "/users", func(c *Context) error {
		c.Set("a", 1)
		return nil
	}, e)
	r.Add(GET, "/users/new", func(c *Context) error {
		c.Set("b", 2)
		return nil
	}, e)
	r.Add(GET, "/users/:id", func(c *Context) error {
		c.Set("c", 3)
		return nil
	}, e)
	r.Add(GET, "/users/dew", func(c *Context) error {
		c.Set("d", 4)
		return nil
	}, e)
	r.Add(GET, "/users/:id/files", func(c *Context) error {
		c.Set("e", 5)
		return nil
	}, e)
	r.Add(GET, "/users/newsee", func(c *Context) error {
		c.Set("f", 6)
		return nil
	}, e)
	r.Add(GET, "/users/*", func(c *Context) error {
		c.Set("g", 7)
		return nil
	}, e)
	c := NewContext(nil, nil, e)

	// Route > /users
	h, _ := r.Find(GET, "/users", c)
	if assert.NotNil(t, h) {
		h(c)
		assert.Equal(t, 1, c.Get("a"))
	}

	// Route > /users/new
	h, _ = r.Find(GET, "/users/new", c)
	if assert.NotNil(t, h) {
		h(c)
		assert.Equal(t, 2, c.Get("b"))
	}

	// Route > /users/:id
	h, _ = r.Find(GET, "/users/1", c)
	if assert.NotNil(t, h) {
		h(c)
		assert.Equal(t, 3, c.Get("c"))
	}

	// Route > /users/dew
	h, _ = r.Find(GET, "/users/dew", c)
	if assert.NotNil(t, h) {
		h(c)
		assert.Equal(t, 4, c.Get("d"))
	}

	// Route > /users/:id/files
	h, _ = r.Find(GET, "/users/1/files", c)
	if assert.NotNil(t, h) {
		h(c)
		assert.Equal(t, 5, c.Get("e"))
	}

	// Route > /users/:id
	h, _ = r.Find(GET, "/users/news", c)
	if assert.NotNil(t, h) {
		h(c)
		assert.Equal(t, 3, c.Get("c"))
	}

	// Route > /users/*
	h, _ = r.Find(GET, "/users/joe/books", c)
	if assert.NotNil(t, h) {
		h(c)
		assert.Equal(t, 7, c.Get("g"))
		assert.Equal(t, "joe/books", c.Param("_*"))
	}
}