Merge pull request #1 from msoedov/tests

Added tests for ServeFiles #37
This commit is contained in:
Alexander Nyquist 2014-07-08 13:17:45 +02:00
commit 78ccff217c

View File

@ -1,10 +1,14 @@
package gin package gin
import ( import (
"testing"
"html/template" "html/template"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"path"
"strings"
"testing"
) )
// TestRouterGroupGETRouteOK tests that GET route is correctly invoked. // TestRouterGroupGETRouteOK tests that GET route is correctly invoked.
@ -135,7 +139,6 @@ func TestRouterGroupPUTRouteOK(t *testing.T) {
} }
} }
// TestRouterGroupOPTIONSRouteOK tests that OPTIONS route is correctly invoked. // TestRouterGroupOPTIONSRouteOK tests that OPTIONS route is correctly invoked.
func TestRouterGroupOPTIONSRouteOK(t *testing.T) { func TestRouterGroupOPTIONSRouteOK(t *testing.T) {
req, _ := http.NewRequest("OPTIONS", "/test", nil) req, _ := http.NewRequest("OPTIONS", "/test", nil)
@ -158,7 +161,6 @@ func TestRouterGroupOPTIONSRouteOK(t *testing.T) {
} }
} }
// TestRouterGroupHEADRouteOK tests that HEAD route is correctly invoked. // TestRouterGroupHEADRouteOK tests that HEAD route is correctly invoked.
func TestRouterGroupHEADRouteOK(t *testing.T) { func TestRouterGroupHEADRouteOK(t *testing.T) {
req, _ := http.NewRequest("HEAD", "/test", nil) req, _ := http.NewRequest("HEAD", "/test", nil)
@ -181,7 +183,6 @@ func TestRouterGroupHEADRouteOK(t *testing.T) {
} }
} }
// TestRouterGroup404 tests that 404 is returned for a route that does not exist. // TestRouterGroup404 tests that 404 is returned for a route that does not exist.
func TestEngine404(t *testing.T) { func TestEngine404(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil) req, _ := http.NewRequest("GET", "/", nil)
@ -306,3 +307,72 @@ func TestContextString(t *testing.T) {
t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type")) t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
} }
} }
// 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"))
}
}