gin/gin_test.go

384 lines
8.9 KiB
Go
Raw Normal View History

2014-07-04 08:12:28 +00:00
package gin
2014-07-05 15:04:11 +00:00
import (
2014-07-04 08:12:28 +00:00
"html/template"
2014-07-05 15:04:11 +00:00
"io/ioutil"
2014-07-04 08:12:28 +00:00
"net/http"
"net/http/httptest"
2014-07-05 15:04:11 +00:00
"os"
"path"
"strings"
"testing"
2014-07-04 08:12:28 +00:00
)
// TestRouterGroupGETRouteOK tests that GET route is correctly invoked.
func TestRouterGroupGETRouteOK(t *testing.T) {
req, _ := http.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
passed := false
r := Default()
2014-07-05 15:04:11 +00:00
r.GET("/test", func(c *Context) {
2014-07-04 08:12:28 +00:00
passed = true
})
r.ServeHTTP(w, req)
if passed == false {
t.Errorf("GET route handler was not invoked.")
}
if w.Code != http.StatusOK {
t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
}
}
// TestRouterGroupGETNoRootExistsRouteOK tests that a GET requse to root is correctly
// handled (404) when no root route exists.
func TestRouterGroupGETNoRootExistsRouteOK(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
2014-07-05 15:04:11 +00:00
r := Default()
2014-07-05 15:04:11 +00:00
r.GET("/test", func(c *Context) {
})
r.ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
// If this fails, it's because httprouter needs to be updated to at least f78f58a0db
t.Errorf("Status code should be %v, was %d. Location: %s", http.StatusNotFound, w.Code, w.HeaderMap.Get("Location"))
}
}
2014-07-04 08:12:28 +00:00
// TestRouterGroupPOSTRouteOK tests that POST route is correctly invoked.
func TestRouterGroupPOSTRouteOK(t *testing.T) {
req, _ := http.NewRequest("POST", "/test", nil)
w := httptest.NewRecorder()
passed := false
r := Default()
2014-07-05 15:04:11 +00:00
r.POST("/test", func(c *Context) {
2014-07-04 08:12:28 +00:00
passed = true
})
r.ServeHTTP(w, req)
if passed == false {
t.Errorf("POST route handler was not invoked.")
}
if w.Code != http.StatusOK {
t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
}
}
// TestRouterGroupDELETERouteOK tests that DELETE route is correctly invoked.
func TestRouterGroupDELETERouteOK(t *testing.T) {
req, _ := http.NewRequest("DELETE", "/test", nil)
w := httptest.NewRecorder()
passed := false
r := Default()
2014-07-05 15:04:11 +00:00
r.DELETE("/test", func(c *Context) {
2014-07-04 08:12:28 +00:00
passed = true
})
r.ServeHTTP(w, req)
if passed == false {
t.Errorf("DELETE route handler was not invoked.")
}
if w.Code != http.StatusOK {
t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
}
}
// TestRouterGroupPATCHRouteOK tests that PATCH route is correctly invoked.
func TestRouterGroupPATCHRouteOK(t *testing.T) {
req, _ := http.NewRequest("PATCH", "/test", nil)
w := httptest.NewRecorder()
passed := false
r := Default()
2014-07-05 15:04:11 +00:00
r.PATCH("/test", func(c *Context) {
2014-07-04 08:12:28 +00:00
passed = true
})
r.ServeHTTP(w, req)
if passed == false {
t.Errorf("PATCH route handler was not invoked.")
}
if w.Code != http.StatusOK {
t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
}
}
// TestRouterGroupPUTRouteOK tests that PUT route is correctly invoked.
func TestRouterGroupPUTRouteOK(t *testing.T) {
req, _ := http.NewRequest("PUT", "/test", nil)
w := httptest.NewRecorder()
passed := false
r := Default()
2014-07-05 15:04:11 +00:00
r.PUT("/test", func(c *Context) {
2014-07-04 08:12:28 +00:00
passed = true
})
r.ServeHTTP(w, req)
if passed == false {
t.Errorf("PUT route handler was not invoked.")
}
if w.Code != http.StatusOK {
t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
}
}
// TestRouterGroupOPTIONSRouteOK tests that OPTIONS route is correctly invoked.
func TestRouterGroupOPTIONSRouteOK(t *testing.T) {
req, _ := http.NewRequest("OPTIONS", "/test", nil)
w := httptest.NewRecorder()
passed := false
r := Default()
2014-07-05 15:04:11 +00:00
r.OPTIONS("/test", func(c *Context) {
2014-07-04 08:12:28 +00:00
passed = true
})
r.ServeHTTP(w, req)
if passed == false {
t.Errorf("OPTIONS route handler was not invoked.")
}
if w.Code != http.StatusOK {
t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
}
}
// TestRouterGroupHEADRouteOK tests that HEAD route is correctly invoked.
func TestRouterGroupHEADRouteOK(t *testing.T) {
req, _ := http.NewRequest("HEAD", "/test", nil)
w := httptest.NewRecorder()
passed := false
r := Default()
2014-07-05 15:04:11 +00:00
r.HEAD("/test", func(c *Context) {
2014-07-04 08:12:28 +00:00
passed = true
})
r.ServeHTTP(w, req)
if passed == false {
t.Errorf("HEAD route handler was not invoked.")
}
if w.Code != http.StatusOK {
t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
}
}
// TestRouterGroup404 tests that 404 is returned for a route that does not exist.
func TestEngine404(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
2014-07-05 15:04:11 +00:00
2014-07-04 08:12:28 +00:00
r := Default()
r.ServeHTTP(w, req)
2014-07-05 15:04:11 +00:00
2014-07-04 08:12:28 +00:00
if w.Code != http.StatusNotFound {
t.Errorf("Response code should be %v, was %d", http.StatusNotFound, w.Code)
}
}
// TestContextParamsGet tests that a parameter can be parsed from the URL.
func TestContextParamsByName(t *testing.T) {
req, _ := http.NewRequest("GET", "/test/alexandernyquist", nil)
w := httptest.NewRecorder()
name := ""
r := Default()
2014-07-05 15:04:11 +00:00
r.GET("/test/:name", func(c *Context) {
2014-07-04 08:12:28 +00:00
name = c.Params.ByName("name")
})
r.ServeHTTP(w, req)
if name != "alexandernyquist" {
t.Errorf("Url parameter was not correctly parsed. Should be alexandernyquist, was %s.", name)
}
}
// TestContextSetGet tests that a parameter is set correctly on the
// current context and can be retrieved using Get.
func TestContextSetGet(t *testing.T) {
req, _ := http.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
r := Default()
2014-07-05 15:04:11 +00:00
r.GET("/test", func(c *Context) {
2014-07-04 08:12:28 +00:00
// Key should be lazily created
if c.Keys != nil {
t.Error("Keys should be nil")
}
// Set
c.Set("foo", "bar")
2014-07-18 14:42:38 +00:00
v, err := c.Get("foo")
if err != nil {
t.Errorf("Error on exist key")
}
if v != "bar" {
2014-07-04 08:12:28 +00:00
t.Errorf("Value should be bar, was %s", v)
}
})
r.ServeHTTP(w, req)
}
// TestContextJSON tests that the response is serialized as JSON
// and Content-Type is set to application/json
func TestContextJSON(t *testing.T) {
req, _ := http.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
r := Default()
2014-07-05 15:04:11 +00:00
r.GET("/test", func(c *Context) {
2014-07-04 08:12:28 +00:00
c.JSON(200, H{"foo": "bar"})
})
r.ServeHTTP(w, req)
if w.Body.String() != "{\"foo\":\"bar\"}\n" {
t.Errorf("Response should be {\"foo\":\"bar\"}, was: %s", w.Body.String())
}
if w.HeaderMap.Get("Content-Type") != "application/json" {
t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type"))
}
}
// TestContextHTML tests that the response executes the templates
// and responds with Content-Type set to text/html
func TestContextHTML(t *testing.T) {
req, _ := http.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
r := Default()
2014-07-18 14:42:38 +00:00
templ, _ := template.New("t").Parse(`Hello {{.Name}}`)
r.SetHTMLTemplate(templ)
2014-07-04 08:12:28 +00:00
2014-07-05 15:04:11 +00:00
type TestData struct{ Name string }
2014-07-04 08:12:28 +00:00
2014-07-05 15:04:11 +00:00
r.GET("/test", func(c *Context) {
2014-07-04 08:12:28 +00:00
c.HTML(200, "t", TestData{"alexandernyquist"})
})
r.ServeHTTP(w, req)
if w.Body.String() != "Hello alexandernyquist" {
t.Errorf("Response should be Hello alexandernyquist, was: %s", w.Body.String())
}
if w.HeaderMap.Get("Content-Type") != "text/html" {
t.Errorf("Content-Type should be text/html, was %s", w.HeaderMap.Get("Content-Type"))
}
}
// TestContextString tests that the response is returned
// with Content-Type set to text/plain
func TestContextString(t *testing.T) {
req, _ := http.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
r := Default()
2014-07-05 15:04:11 +00:00
r.GET("/test", func(c *Context) {
2014-07-04 08:12:28 +00:00
c.String(200, "test")
})
r.ServeHTTP(w, req)
if w.Body.String() != "test" {
t.Errorf("Response should be test, was: %s", w.Body.String())
}
if w.HeaderMap.Get("Content-Type") != "text/plain" {
t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
}
2014-07-05 15:04:11 +00:00
}
// TestHandleStaticFile - ensure the static file handles properly
func TestHandleStaticFile(t *testing.T) {
testRoot, _ := os.Getwd()
f, err := ioutil.TempFile(testRoot, "")
defer os.Remove(f.Name())
if err != nil {
t.Error(err)
}
filePath := path.Join("/", path.Base(f.Name()))
req, _ := http.NewRequest("GET", filePath, nil)
f.WriteString("Gin Web Framework")
f.Close()
w := httptest.NewRecorder()
r := Default()
2014-07-24 13:51:11 +00:00
r.Static("./", testRoot)
2014-07-05 15:04:11 +00:00
r.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("Response code should be Ok, was: %s", w.Code)
}
if w.Body.String() != "Gin Web Framework" {
t.Errorf("Response should be test, was: %s", w.Body.String())
}
if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" {
t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
}
}
// TestHandleStaticDir - ensure the root/sub dir handles properly
func TestHandleStaticDir(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
r := Default()
2014-07-24 13:51:11 +00:00
r.Static("/", "./")
2014-07-05 15:04:11 +00:00
r.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("Response code should be Ok, was: %s", w.Code)
}
bodyAsString := w.Body.String()
if len(bodyAsString) == 0 {
t.Errorf("Got empty body instead of file tree")
}
if !strings.Contains(bodyAsString, "gin.go") {
t.Errorf("Can't find:`gin.go` in file tree: %s", bodyAsString)
}
if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" {
t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
}
}