2014-07-04 08:12:28 +00:00
|
|
|
package gin
|
|
|
|
|
2014-07-05 15:04:11 +00:00
|
|
|
import (
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
func PerformRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
|
|
|
|
req, _ := http.NewRequest(method, path, nil)
|
2014-07-04 08:12:28 +00:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r.ServeHTTP(w, req)
|
2014-08-18 17:48:48 +00:00
|
|
|
return w
|
2014-07-04 08:12:28 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// TestSingleRouteOK tests that POST route is correctly invoked.
|
|
|
|
func testRouteOK(method string, t *testing.T) {
|
|
|
|
// SETUP
|
2014-07-04 08:12:28 +00:00
|
|
|
passed := false
|
2014-08-18 17:48:48 +00:00
|
|
|
r := New()
|
|
|
|
r.Handle(method, "/test", []HandlerFunc{func(c *Context) {
|
2014-07-04 08:12:28 +00:00
|
|
|
passed = true
|
2014-08-18 17:48:48 +00:00
|
|
|
}})
|
2014-07-04 08:12:28 +00:00
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// RUN
|
|
|
|
w := PerformRequest(r, method, "/test")
|
2014-07-04 08:12:28 +00:00
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// TEST
|
2014-07-04 08:12:28 +00:00
|
|
|
if passed == false {
|
2014-08-18 17:48:48 +00:00
|
|
|
t.Errorf(method + " route handler was not invoked.")
|
2014-07-04 08:12:28 +00:00
|
|
|
}
|
|
|
|
if w.Code != http.StatusOK {
|
|
|
|
t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 17:48:48 +00:00
|
|
|
func TestRouterGroupRouteOK(t *testing.T) {
|
|
|
|
testRouteOK("POST", t)
|
|
|
|
testRouteOK("DELETE", t)
|
|
|
|
testRouteOK("PATCH", t)
|
|
|
|
testRouteOK("PUT", t)
|
|
|
|
testRouteOK("OPTIONS", t)
|
|
|
|
testRouteOK("HEAD", t)
|
2014-07-04 08:12:28 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// TestSingleRouteOK tests that POST route is correctly invoked.
|
|
|
|
func testRouteNotOK(method string, t *testing.T) {
|
|
|
|
// SETUP
|
2014-07-04 08:12:28 +00:00
|
|
|
passed := false
|
2014-08-18 17:48:48 +00:00
|
|
|
r := New()
|
|
|
|
r.Handle(method, "/test_2", []HandlerFunc{func(c *Context) {
|
2014-07-04 08:12:28 +00:00
|
|
|
passed = true
|
2014-08-18 17:48:48 +00:00
|
|
|
}})
|
2014-07-04 08:12:28 +00:00
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// RUN
|
|
|
|
w := PerformRequest(r, method, "/test")
|
2014-07-04 08:12:28 +00:00
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// TEST
|
|
|
|
if passed == true {
|
|
|
|
t.Errorf(method + " route handler was invoked, when it should not")
|
2014-07-04 08:12:28 +00:00
|
|
|
}
|
2014-08-18 17:48:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// TestSingleRouteOK tests that POST route is correctly invoked.
|
|
|
|
func TestRouteNotOK(t *testing.T) {
|
|
|
|
testRouteNotOK("POST", t)
|
|
|
|
testRouteNotOK("DELETE", t)
|
|
|
|
testRouteNotOK("PATCH", t)
|
|
|
|
testRouteNotOK("PUT", t)
|
|
|
|
testRouteNotOK("OPTIONS", t)
|
|
|
|
testRouteNotOK("HEAD", t)
|
2014-07-04 08:12:28 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// TestSingleRouteOK tests that POST route is correctly invoked.
|
|
|
|
func testRouteNotOK2(method string, t *testing.T) {
|
|
|
|
// SETUP
|
2014-07-04 08:12:28 +00:00
|
|
|
passed := false
|
2014-08-18 17:48:48 +00:00
|
|
|
r := New()
|
|
|
|
var methodRoute string
|
|
|
|
if method == "POST" {
|
|
|
|
methodRoute = "GET"
|
|
|
|
} else {
|
|
|
|
methodRoute = "POST"
|
|
|
|
}
|
|
|
|
r.Handle(methodRoute, "/test", []HandlerFunc{func(c *Context) {
|
2014-07-04 08:12:28 +00:00
|
|
|
passed = true
|
2014-08-18 17:48:48 +00:00
|
|
|
}})
|
2014-07-04 08:12:28 +00:00
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// RUN
|
|
|
|
w := PerformRequest(r, method, "/test")
|
2014-07-04 08:12:28 +00:00
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// TEST
|
|
|
|
if passed == true {
|
|
|
|
t.Errorf(method + " route handler was invoked, when it should not")
|
2014-07-04 08:12:28 +00:00
|
|
|
}
|
2014-08-18 17:48:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// TestSingleRouteOK tests that POST route is correctly invoked.
|
|
|
|
func TestRouteNotOK2(t *testing.T) {
|
|
|
|
testRouteNotOK2("POST", t)
|
|
|
|
testRouteNotOK2("DELETE", t)
|
|
|
|
testRouteNotOK2("PATCH", t)
|
|
|
|
testRouteNotOK2("PUT", t)
|
|
|
|
testRouteNotOK2("OPTIONS", t)
|
|
|
|
testRouteNotOK2("HEAD", t)
|
2014-07-04 08:12:28 +00:00
|
|
|
}
|
|
|
|
|
2014-07-05 15:04:11 +00:00
|
|
|
// TestHandleStaticFile - ensure the static file handles properly
|
|
|
|
func TestHandleStaticFile(t *testing.T) {
|
2014-08-18 17:48:48 +00:00
|
|
|
// SETUP file
|
2014-07-05 15:04:11 +00:00
|
|
|
testRoot, _ := os.Getwd()
|
|
|
|
f, err := ioutil.TempFile(testRoot, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2014-08-18 17:48:48 +00:00
|
|
|
defer os.Remove(f.Name())
|
2014-07-05 15:04:11 +00:00
|
|
|
filePath := path.Join("/", path.Base(f.Name()))
|
|
|
|
f.WriteString("Gin Web Framework")
|
|
|
|
f.Close()
|
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// SETUP gin
|
|
|
|
r := New()
|
2014-07-24 13:51:11 +00:00
|
|
|
r.Static("./", testRoot)
|
2014-07-05 15:04:11 +00:00
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// RUN
|
|
|
|
w := PerformRequest(r, "GET", filePath)
|
2014-07-05 15:04:11 +00:00
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// TEST
|
2014-07-05 15:04:11 +00:00
|
|
|
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) {
|
2014-08-18 17:48:48 +00:00
|
|
|
// SETUP
|
|
|
|
r := New()
|
2014-07-24 13:51:11 +00:00
|
|
|
r.Static("/", "./")
|
2014-07-05 15:04:11 +00:00
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// RUN
|
|
|
|
w := PerformRequest(r, "GET", "/")
|
2014-07-05 15:04:11 +00:00
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// TEST
|
|
|
|
bodyAsString := w.Body.String()
|
2014-07-05 15:04:11 +00:00
|
|
|
if w.Code != 200 {
|
|
|
|
t.Errorf("Response code should be Ok, was: %s", w.Code)
|
|
|
|
}
|
|
|
|
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"))
|
|
|
|
}
|
|
|
|
}
|
2014-07-28 10:05:23 +00:00
|
|
|
|
|
|
|
// TestHandleHeadToDir - ensure the root/sub dir handles properly
|
|
|
|
func TestHandleHeadToDir(t *testing.T) {
|
2014-08-18 17:48:48 +00:00
|
|
|
// SETUP
|
|
|
|
r := New()
|
2014-07-28 10:05:23 +00:00
|
|
|
r.Static("/", "./")
|
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// RUN
|
|
|
|
w := PerformRequest(r, "HEAD", "/")
|
2014-07-28 10:05:23 +00:00
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// TEST
|
|
|
|
bodyAsString := w.Body.String()
|
2014-07-28 10:05:23 +00:00
|
|
|
if w.Code != 200 {
|
|
|
|
t.Errorf("Response code should be Ok, was: %s", w.Code)
|
|
|
|
}
|
|
|
|
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"))
|
|
|
|
}
|
|
|
|
}
|