From f944cff1a8787874e6165d9ec9cfe119d9db7441 Mon Sep 17 00:00:00 2001 From: msoedov Date: Sat, 5 Jul 2014 18:04:11 +0300 Subject: [PATCH] Added tests for ServeFiles #37 --- gin_test.go | 116 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 93 insertions(+), 23 deletions(-) diff --git a/gin_test.go b/gin_test.go index e66c5e3d..c853b0f4 100644 --- a/gin_test.go +++ b/gin_test.go @@ -1,10 +1,14 @@ package gin -import( - "testing" +import ( "html/template" + "io/ioutil" "net/http" "net/http/httptest" + "os" + "path" + "strings" + "testing" ) // TestRouterGroupGETRouteOK tests that GET route is correctly invoked. @@ -14,7 +18,7 @@ func TestRouterGroupGETRouteOK(t *testing.T) { passed := false r := Default() - r.GET("/test", func (c *Context) { + r.GET("/test", func(c *Context) { passed = true }) @@ -34,9 +38,9 @@ func TestRouterGroupGETRouteOK(t *testing.T) { func TestRouterGroupGETNoRootExistsRouteOK(t *testing.T) { req, _ := http.NewRequest("GET", "/", nil) w := httptest.NewRecorder() - + r := Default() - r.GET("/test", func (c *Context) { + r.GET("/test", func(c *Context) { }) r.ServeHTTP(w, req) @@ -54,7 +58,7 @@ func TestRouterGroupPOSTRouteOK(t *testing.T) { passed := false r := Default() - r.POST("/test", func (c *Context) { + r.POST("/test", func(c *Context) { passed = true }) @@ -76,7 +80,7 @@ func TestRouterGroupDELETERouteOK(t *testing.T) { passed := false r := Default() - r.DELETE("/test", func (c *Context) { + r.DELETE("/test", func(c *Context) { passed = true }) @@ -98,7 +102,7 @@ func TestRouterGroupPATCHRouteOK(t *testing.T) { passed := false r := Default() - r.PATCH("/test", func (c *Context) { + r.PATCH("/test", func(c *Context) { passed = true }) @@ -120,7 +124,7 @@ func TestRouterGroupPUTRouteOK(t *testing.T) { passed := false r := Default() - r.PUT("/test", func (c *Context) { + r.PUT("/test", func(c *Context) { passed = true }) @@ -135,7 +139,6 @@ func TestRouterGroupPUTRouteOK(t *testing.T) { } } - // TestRouterGroupOPTIONSRouteOK tests that OPTIONS route is correctly invoked. func TestRouterGroupOPTIONSRouteOK(t *testing.T) { req, _ := http.NewRequest("OPTIONS", "/test", nil) @@ -143,7 +146,7 @@ func TestRouterGroupOPTIONSRouteOK(t *testing.T) { passed := false r := Default() - r.OPTIONS("/test", func (c *Context) { + r.OPTIONS("/test", func(c *Context) { passed = true }) @@ -158,7 +161,6 @@ func TestRouterGroupOPTIONSRouteOK(t *testing.T) { } } - // TestRouterGroupHEADRouteOK tests that HEAD route is correctly invoked. func TestRouterGroupHEADRouteOK(t *testing.T) { req, _ := http.NewRequest("HEAD", "/test", nil) @@ -166,7 +168,7 @@ func TestRouterGroupHEADRouteOK(t *testing.T) { passed := false r := Default() - r.HEAD("/test", func (c *Context) { + r.HEAD("/test", func(c *Context) { passed = true }) @@ -181,15 +183,14 @@ func TestRouterGroupHEADRouteOK(t *testing.T) { } } - // 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() - + r := Default() r.ServeHTTP(w, req) - + if w.Code != http.StatusNotFound { t.Errorf("Response code should be %v, was %d", http.StatusNotFound, w.Code) } @@ -202,7 +203,7 @@ func TestContextParamsByName(t *testing.T) { name := "" r := Default() - r.GET("/test/:name", func (c *Context) { + r.GET("/test/:name", func(c *Context) { name = c.Params.ByName("name") }) @@ -220,7 +221,7 @@ func TestContextSetGet(t *testing.T) { w := httptest.NewRecorder() r := Default() - r.GET("/test", func (c *Context) { + r.GET("/test", func(c *Context) { // Key should be lazily created if c.Keys != nil { t.Error("Keys should be nil") @@ -244,7 +245,7 @@ func TestContextJSON(t *testing.T) { w := httptest.NewRecorder() r := Default() - r.GET("/test", func (c *Context) { + r.GET("/test", func(c *Context) { c.JSON(200, H{"foo": "bar"}) }) @@ -268,9 +269,9 @@ func TestContextHTML(t *testing.T) { r := Default() r.HTMLTemplates = template.Must(template.New("t").Parse(`Hello {{.Name}}`)) - type TestData struct { Name string } + type TestData struct{ Name string } - r.GET("/test", func (c *Context) { + r.GET("/test", func(c *Context) { c.HTML(200, "t", TestData{"alexandernyquist"}) }) @@ -292,7 +293,7 @@ func TestContextString(t *testing.T) { w := httptest.NewRecorder() r := Default() - r.GET("/test", func (c *Context) { + r.GET("/test", func(c *Context) { c.String(200, "test") }) @@ -305,4 +306,73 @@ func TestContextString(t *testing.T) { if w.HeaderMap.Get("Content-Type") != "text/plain" { t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type")) } -} \ No newline at end of file +} + +// 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() + r.ServeFiles("/*filepath", http.Dir("./")) + + 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() + r.ServeFiles("/*filepath", http.Dir("./")) + + 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")) + } +}