forked from mirror/gin
Removes unit test in performance branch temporarily.
This commit is contained in:
parent
2915fa0ffe
commit
3faa81a464
519
context_test.go
519
context_test.go
|
@ -1,519 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 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 := New()
|
||||
r.GET("/test/:name", func(c *Context) {
|
||||
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 := New()
|
||||
r.GET("/test", func(c *Context) {
|
||||
// Key should be lazily created
|
||||
if c.Keys != nil {
|
||||
t.Error("Keys should be nil")
|
||||
}
|
||||
|
||||
// Set
|
||||
c.Set("foo", "bar")
|
||||
|
||||
v, ok := c.Get("foo")
|
||||
if !ok {
|
||||
t.Errorf("Error on exist key")
|
||||
}
|
||||
if v != "bar" {
|
||||
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 := New()
|
||||
r.GET("/test", func(c *Context) {
|
||||
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; charset=utf-8" {
|
||||
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 := New()
|
||||
templ, _ := template.New("t").Parse(`Hello {{.Name}}`)
|
||||
r.SetHTMLTemplate(templ)
|
||||
|
||||
type TestData struct{ Name string }
|
||||
|
||||
r.GET("/test", func(c *Context) {
|
||||
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; charset=utf-8" {
|
||||
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 := New()
|
||||
r.GET("/test", func(c *Context) {
|
||||
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; charset=utf-8" {
|
||||
t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
|
||||
}
|
||||
}
|
||||
|
||||
// TestContextXML tests that the response is serialized as XML
|
||||
// and Content-Type is set to application/xml
|
||||
func TestContextXML(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r := New()
|
||||
r.GET("/test", func(c *Context) {
|
||||
c.XML(200, H{"foo": "bar"})
|
||||
})
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Body.String() != "<map><foo>bar</foo></map>" {
|
||||
t.Errorf("Response should be <map><foo>bar</foo></map>, was: %s", w.Body.String())
|
||||
}
|
||||
|
||||
if w.HeaderMap.Get("Content-Type") != "application/xml; charset=utf-8" {
|
||||
t.Errorf("Content-Type should be application/xml, was %s", w.HeaderMap.Get("Content-Type"))
|
||||
}
|
||||
}
|
||||
|
||||
// TestContextData tests that the response can be written from `bytesting`
|
||||
// with specified MIME type
|
||||
func TestContextData(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "/test/csv", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r := New()
|
||||
r.GET("/test/csv", func(c *Context) {
|
||||
c.Data(200, "text/csv", []byte(`foo,bar`))
|
||||
})
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Body.String() != "foo,bar" {
|
||||
t.Errorf("Response should be foo&bar, was: %s", w.Body.String())
|
||||
}
|
||||
|
||||
if w.HeaderMap.Get("Content-Type") != "text/csv" {
|
||||
t.Errorf("Content-Type should be text/csv, was %s", w.HeaderMap.Get("Content-Type"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextFile(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "/test/file", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r := New()
|
||||
r.GET("/test/file", func(c *Context) {
|
||||
c.File("./gin.go")
|
||||
})
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
bodyAsString := w.Body.String()
|
||||
|
||||
if len(bodyAsString) == 0 {
|
||||
t.Errorf("Got empty body instead of file data")
|
||||
}
|
||||
|
||||
if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" {
|
||||
t.Errorf("Content-Type should be text/plain; charset=utf-8, was %s", w.HeaderMap.Get("Content-Type"))
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandlerFunc - ensure that custom middleware works properly
|
||||
func TestHandlerFunc(t *testing.T) {
|
||||
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r := New()
|
||||
var stepsPassed int = 0
|
||||
|
||||
r.Use(func(context *Context) {
|
||||
stepsPassed += 1
|
||||
context.Next()
|
||||
stepsPassed += 1
|
||||
})
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != 404 {
|
||||
t.Errorf("Response code should be Not found, was: %d", w.Code)
|
||||
}
|
||||
|
||||
if stepsPassed != 2 {
|
||||
t.Errorf("Falied to switch context in handler function: %d", stepsPassed)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBadAbortHandlersChain - ensure that Abort after switch context will not interrupt pending handlers
|
||||
func TestBadAbortHandlersChain(t *testing.T) {
|
||||
// SETUP
|
||||
var stepsPassed int = 0
|
||||
r := New()
|
||||
r.Use(func(c *Context) {
|
||||
stepsPassed += 1
|
||||
c.Next()
|
||||
stepsPassed += 1
|
||||
// after check and abort
|
||||
c.AbortWithStatus(409)
|
||||
})
|
||||
r.Use(func(c *Context) {
|
||||
stepsPassed += 1
|
||||
c.Next()
|
||||
stepsPassed += 1
|
||||
c.AbortWithStatus(403)
|
||||
})
|
||||
|
||||
// RUN
|
||||
w := PerformRequest(r, "GET", "/")
|
||||
|
||||
// TEST
|
||||
if w.Code != 409 {
|
||||
t.Errorf("Response code should be Forbiden, was: %d", w.Code)
|
||||
}
|
||||
if stepsPassed != 4 {
|
||||
t.Errorf("Falied to switch context in handler function: %d", stepsPassed)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAbortHandlersChain - ensure that Abort interrupt used middlewares in fifo order
|
||||
func TestAbortHandlersChain(t *testing.T) {
|
||||
// SETUP
|
||||
var stepsPassed int = 0
|
||||
r := New()
|
||||
r.Use(func(context *Context) {
|
||||
stepsPassed += 1
|
||||
context.AbortWithStatus(409)
|
||||
})
|
||||
r.Use(func(context *Context) {
|
||||
stepsPassed += 1
|
||||
context.Next()
|
||||
stepsPassed += 1
|
||||
})
|
||||
|
||||
// RUN
|
||||
w := PerformRequest(r, "GET", "/")
|
||||
|
||||
// TEST
|
||||
if w.Code != 409 {
|
||||
t.Errorf("Response code should be Conflict, was: %d", w.Code)
|
||||
}
|
||||
if stepsPassed != 1 {
|
||||
t.Errorf("Falied to switch context in handler function: %d", stepsPassed)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFailHandlersChain - ensure that Fail interrupt used middlewares in fifo order as
|
||||
// as well as Abort
|
||||
func TestFailHandlersChain(t *testing.T) {
|
||||
// SETUP
|
||||
var stepsPassed int = 0
|
||||
r := New()
|
||||
r.Use(func(context *Context) {
|
||||
stepsPassed += 1
|
||||
context.Fail(500, errors.New("foo"))
|
||||
})
|
||||
r.Use(func(context *Context) {
|
||||
stepsPassed += 1
|
||||
context.Next()
|
||||
stepsPassed += 1
|
||||
})
|
||||
|
||||
// RUN
|
||||
w := PerformRequest(r, "GET", "/")
|
||||
|
||||
// TEST
|
||||
if w.Code != 500 {
|
||||
t.Errorf("Response code should be Server error, was: %d", w.Code)
|
||||
}
|
||||
if stepsPassed != 1 {
|
||||
t.Errorf("Falied to switch context in handler function: %d", stepsPassed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindingJSON(t *testing.T) {
|
||||
|
||||
body := bytes.NewBuffer([]byte("{\"foo\":\"bar\"}"))
|
||||
|
||||
r := New()
|
||||
r.POST("/binding/json", func(c *Context) {
|
||||
var body struct {
|
||||
Foo string `json:"foo"`
|
||||
}
|
||||
if c.Bind(&body) {
|
||||
c.JSON(200, H{"parsed": body.Foo})
|
||||
}
|
||||
})
|
||||
|
||||
req, _ := http.NewRequest("POST", "/binding/json", body)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != 200 {
|
||||
t.Errorf("Response code should be Ok, was: %d", w.Code)
|
||||
}
|
||||
|
||||
if w.Body.String() != "{\"parsed\":\"bar\"}\n" {
|
||||
t.Errorf("Response should be {\"parsed\":\"bar\"}, was: %s", w.Body.String())
|
||||
}
|
||||
|
||||
if w.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" {
|
||||
t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindingJSONEncoding(t *testing.T) {
|
||||
|
||||
body := bytes.NewBuffer([]byte("{\"foo\":\"嘉\"}"))
|
||||
|
||||
r := New()
|
||||
r.POST("/binding/json", func(c *Context) {
|
||||
var body struct {
|
||||
Foo string `json:"foo"`
|
||||
}
|
||||
if c.Bind(&body) {
|
||||
c.JSON(200, H{"parsed": body.Foo})
|
||||
}
|
||||
})
|
||||
|
||||
req, _ := http.NewRequest("POST", "/binding/json", body)
|
||||
req.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != 200 {
|
||||
t.Errorf("Response code should be Ok, was: %d", w.Code)
|
||||
}
|
||||
|
||||
if w.Body.String() != "{\"parsed\":\"嘉\"}\n" {
|
||||
t.Errorf("Response should be {\"parsed\":\"嘉\"}, was: %s", w.Body.String())
|
||||
}
|
||||
|
||||
if w.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" {
|
||||
t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindingJSONNoContentType(t *testing.T) {
|
||||
|
||||
body := bytes.NewBuffer([]byte("{\"foo\":\"bar\"}"))
|
||||
|
||||
r := New()
|
||||
r.POST("/binding/json", func(c *Context) {
|
||||
var body struct {
|
||||
Foo string `json:"foo"`
|
||||
}
|
||||
if c.Bind(&body) {
|
||||
c.JSON(200, H{"parsed": body.Foo})
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
req, _ := http.NewRequest("POST", "/binding/json", body)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != 400 {
|
||||
t.Errorf("Response code should be Bad request, was: %d", w.Code)
|
||||
}
|
||||
|
||||
if w.Body.String() == "{\"parsed\":\"bar\"}\n" {
|
||||
t.Errorf("Response should not be {\"parsed\":\"bar\"}, was: %s", w.Body.String())
|
||||
}
|
||||
|
||||
if w.HeaderMap.Get("Content-Type") == "application/json" {
|
||||
t.Errorf("Content-Type should not be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindingJSONMalformed(t *testing.T) {
|
||||
|
||||
body := bytes.NewBuffer([]byte("\"foo\":\"bar\"\n"))
|
||||
|
||||
r := New()
|
||||
r.POST("/binding/json", func(c *Context) {
|
||||
var body struct {
|
||||
Foo string `json:"foo"`
|
||||
}
|
||||
if c.Bind(&body) {
|
||||
c.JSON(200, H{"parsed": body.Foo})
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
req, _ := http.NewRequest("POST", "/binding/json", body)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != 400 {
|
||||
t.Errorf("Response code should be Bad request, was: %d", w.Code)
|
||||
}
|
||||
if w.Body.String() == "{\"parsed\":\"bar\"}\n" {
|
||||
t.Errorf("Response should not be {\"parsed\":\"bar\"}, was: %s", w.Body.String())
|
||||
}
|
||||
|
||||
if w.HeaderMap.Get("Content-Type") == "application/json" {
|
||||
t.Errorf("Content-Type should not be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindingForm(t *testing.T) {
|
||||
|
||||
body := bytes.NewBuffer([]byte("foo=bar&num=123&unum=1234567890"))
|
||||
|
||||
r := New()
|
||||
r.POST("/binding/form", func(c *Context) {
|
||||
var body struct {
|
||||
Foo string `form:"foo"`
|
||||
Num int `form:"num"`
|
||||
Unum uint `form:"unum"`
|
||||
}
|
||||
if c.Bind(&body) {
|
||||
c.JSON(200, H{"foo": body.Foo, "num": body.Num, "unum": body.Unum})
|
||||
}
|
||||
})
|
||||
|
||||
req, _ := http.NewRequest("POST", "/binding/form", body)
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != 200 {
|
||||
t.Errorf("Response code should be Ok, was: %d", w.Code)
|
||||
}
|
||||
|
||||
expected := "{\"foo\":\"bar\",\"num\":123,\"unum\":1234567890}\n"
|
||||
if w.Body.String() != expected {
|
||||
t.Errorf("Response should be %s, was %s", expected, w.Body.String())
|
||||
}
|
||||
|
||||
if w.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" {
|
||||
t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientIP(t *testing.T) {
|
||||
r := New()
|
||||
|
||||
var clientIP string = ""
|
||||
r.GET("/", func(c *Context) {
|
||||
clientIP = c.ClientIP()
|
||||
})
|
||||
|
||||
body := bytes.NewBuffer([]byte(""))
|
||||
req, _ := http.NewRequest("GET", "/", body)
|
||||
req.RemoteAddr = "clientip:1234"
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if clientIP != "clientip:1234" {
|
||||
t.Errorf("ClientIP should not be %s, but clientip:1234", clientIP)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientIPWithXForwardedForWithProxy(t *testing.T) {
|
||||
r := New()
|
||||
r.Use(ForwardedFor())
|
||||
|
||||
var clientIP string = ""
|
||||
r.GET("/", func(c *Context) {
|
||||
clientIP = c.ClientIP()
|
||||
})
|
||||
|
||||
body := bytes.NewBuffer([]byte(""))
|
||||
req, _ := http.NewRequest("GET", "/", body)
|
||||
req.RemoteAddr = "172.16.8.3:1234"
|
||||
req.Header.Set("X-Real-Ip", "realip")
|
||||
req.Header.Set("X-Forwarded-For", "1.2.3.4, 10.10.0.4, 192.168.0.43, 172.16.8.4")
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if clientIP != "1.2.3.4:0" {
|
||||
t.Errorf("ClientIP should not be %s, but 1.2.3.4:0", clientIP)
|
||||
}
|
||||
}
|
1
gin.go
1
gin.go
|
@ -229,6 +229,7 @@ func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||
c.handlers = handlers
|
||||
c.Params = params
|
||||
c.Next()
|
||||
c.Writer.WriteHeaderNow()
|
||||
engine.reuseContext(c)
|
||||
return
|
||||
}
|
||||
|
|
206
gin_test.go
206
gin_test.go
|
@ -1,206 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func init() {
|
||||
SetMode(TestMode)
|
||||
}
|
||||
|
||||
func PerformRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
|
||||
req, _ := http.NewRequest(method, path, nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
return w
|
||||
}
|
||||
|
||||
// TestSingleRouteOK tests that POST route is correctly invoked.
|
||||
func testRouteOK(method string, t *testing.T) {
|
||||
// SETUP
|
||||
passed := false
|
||||
r := New()
|
||||
r.Handle(method, "/test", []HandlerFunc{func(c *Context) {
|
||||
passed = true
|
||||
}})
|
||||
|
||||
// RUN
|
||||
w := PerformRequest(r, method, "/test")
|
||||
|
||||
// TEST
|
||||
if passed == false {
|
||||
t.Errorf(method + " route handler was not invoked.")
|
||||
}
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
|
||||
}
|
||||
}
|
||||
func TestRouterGroupRouteOK(t *testing.T) {
|
||||
testRouteOK("POST", t)
|
||||
testRouteOK("DELETE", t)
|
||||
testRouteOK("PATCH", t)
|
||||
testRouteOK("PUT", t)
|
||||
testRouteOK("OPTIONS", t)
|
||||
testRouteOK("HEAD", t)
|
||||
}
|
||||
|
||||
// TestSingleRouteOK tests that POST route is correctly invoked.
|
||||
func testRouteNotOK(method string, t *testing.T) {
|
||||
// SETUP
|
||||
passed := false
|
||||
r := New()
|
||||
r.Handle(method, "/test_2", []HandlerFunc{func(c *Context) {
|
||||
passed = true
|
||||
}})
|
||||
|
||||
// RUN
|
||||
w := PerformRequest(r, method, "/test")
|
||||
|
||||
// TEST
|
||||
if passed == true {
|
||||
t.Errorf(method + " route handler was invoked, when it should not")
|
||||
}
|
||||
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"))
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// TestSingleRouteOK tests that POST route is correctly invoked.
|
||||
func testRouteNotOK2(method string, t *testing.T) {
|
||||
// SETUP
|
||||
passed := false
|
||||
r := New()
|
||||
var methodRoute string
|
||||
if method == "POST" {
|
||||
methodRoute = "GET"
|
||||
} else {
|
||||
methodRoute = "POST"
|
||||
}
|
||||
r.Handle(methodRoute, "/test", []HandlerFunc{func(c *Context) {
|
||||
passed = true
|
||||
}})
|
||||
|
||||
// RUN
|
||||
w := PerformRequest(r, method, "/test")
|
||||
|
||||
// TEST
|
||||
if passed == true {
|
||||
t.Errorf(method + " route handler was invoked, when it should not")
|
||||
}
|
||||
if w.Code != http.StatusMethodNotAllowed {
|
||||
t.Errorf("Status code should be %v, was %d. Location: %s", http.StatusMethodNotAllowed, w.Code, w.HeaderMap.Get("Location"))
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// TestHandleStaticFile - ensure the static file handles properly
|
||||
func TestHandleStaticFile(t *testing.T) {
|
||||
// SETUP file
|
||||
testRoot, _ := os.Getwd()
|
||||
f, err := ioutil.TempFile(testRoot, "")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
defer os.Remove(f.Name())
|
||||
filePath := path.Join("/", path.Base(f.Name()))
|
||||
f.WriteString("Gin Web Framework")
|
||||
f.Close()
|
||||
|
||||
// SETUP gin
|
||||
r := New()
|
||||
r.Static("./", testRoot)
|
||||
|
||||
// RUN
|
||||
w := PerformRequest(r, "GET", filePath)
|
||||
|
||||
// TEST
|
||||
if w.Code != 200 {
|
||||
t.Errorf("Response code should be 200, was: %d", 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) {
|
||||
// SETUP
|
||||
r := New()
|
||||
r.Static("/", "./")
|
||||
|
||||
// RUN
|
||||
w := PerformRequest(r, "GET", "/")
|
||||
|
||||
// TEST
|
||||
bodyAsString := w.Body.String()
|
||||
if w.Code != 200 {
|
||||
t.Errorf("Response code should be 200, was: %d", 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"))
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleHeadToDir - ensure the root/sub dir handles properly
|
||||
func TestHandleHeadToDir(t *testing.T) {
|
||||
// SETUP
|
||||
r := New()
|
||||
r.Static("/", "./")
|
||||
|
||||
// RUN
|
||||
w := PerformRequest(r, "HEAD", "/")
|
||||
|
||||
// TEST
|
||||
bodyAsString := w.Body.String()
|
||||
if w.Code != 200 {
|
||||
t.Errorf("Response code should be Ok, was: %d", 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"))
|
||||
}
|
||||
}
|
92
path_test.go
92
path_test.go
|
@ -1,92 +0,0 @@
|
|||
// Copyright 2013 Julien Schmidt. All rights reserved.
|
||||
// Based on the path package, Copyright 2009 The Go Authors.
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var cleanTests = []struct {
|
||||
path, result string
|
||||
}{
|
||||
// Already clean
|
||||
{"/", "/"},
|
||||
{"/abc", "/abc"},
|
||||
{"/a/b/c", "/a/b/c"},
|
||||
{"/abc/", "/abc/"},
|
||||
{"/a/b/c/", "/a/b/c/"},
|
||||
|
||||
// missing root
|
||||
{"", "/"},
|
||||
{"abc", "/abc"},
|
||||
{"abc/def", "/abc/def"},
|
||||
{"a/b/c", "/a/b/c"},
|
||||
|
||||
// Remove doubled slash
|
||||
{"//", "/"},
|
||||
{"/abc//", "/abc/"},
|
||||
{"/abc/def//", "/abc/def/"},
|
||||
{"/a/b/c//", "/a/b/c/"},
|
||||
{"/abc//def//ghi", "/abc/def/ghi"},
|
||||
{"//abc", "/abc"},
|
||||
{"///abc", "/abc"},
|
||||
{"//abc//", "/abc/"},
|
||||
|
||||
// Remove . elements
|
||||
{".", "/"},
|
||||
{"./", "/"},
|
||||
{"/abc/./def", "/abc/def"},
|
||||
{"/./abc/def", "/abc/def"},
|
||||
{"/abc/.", "/abc/"},
|
||||
|
||||
// Remove .. elements
|
||||
{"..", "/"},
|
||||
{"../", "/"},
|
||||
{"../../", "/"},
|
||||
{"../..", "/"},
|
||||
{"../../abc", "/abc"},
|
||||
{"/abc/def/ghi/../jkl", "/abc/def/jkl"},
|
||||
{"/abc/def/../ghi/../jkl", "/abc/jkl"},
|
||||
{"/abc/def/..", "/abc"},
|
||||
{"/abc/def/../..", "/"},
|
||||
{"/abc/def/../../..", "/"},
|
||||
{"/abc/def/../../..", "/"},
|
||||
{"/abc/def/../../../ghi/jkl/../../../mno", "/mno"},
|
||||
|
||||
// Combinations
|
||||
{"abc/./../def", "/def"},
|
||||
{"abc//./../def", "/def"},
|
||||
{"abc/../../././../def", "/def"},
|
||||
}
|
||||
|
||||
func TestPathClean(t *testing.T) {
|
||||
for _, test := range cleanTests {
|
||||
if s := CleanPath(test.path); s != test.result {
|
||||
t.Errorf("CleanPath(%q) = %q, want %q", test.path, s, test.result)
|
||||
}
|
||||
if s := CleanPath(test.result); s != test.result {
|
||||
t.Errorf("CleanPath(%q) = %q, want %q", test.result, s, test.result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathCleanMallocs(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping malloc count in short mode")
|
||||
}
|
||||
if runtime.GOMAXPROCS(0) > 1 {
|
||||
t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
|
||||
return
|
||||
}
|
||||
|
||||
for _, test := range cleanTests {
|
||||
allocs := testing.AllocsPerRun(100, func() { CleanPath(test.result) })
|
||||
if allocs > 0 {
|
||||
t.Errorf("CleanPath(%q): %v allocs, want zero", test.result, allocs)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestPanicInHandler assert that panic has been recovered.
|
||||
func TestPanicInHandler(t *testing.T) {
|
||||
// SETUP
|
||||
log.SetOutput(bytes.NewBuffer(nil)) // Disable panic logs for testing
|
||||
r := New()
|
||||
r.Use(Recovery())
|
||||
r.GET("/recovery", func(_ *Context) {
|
||||
log.Panic("Oupps, Houston, we have a problem")
|
||||
})
|
||||
|
||||
// RUN
|
||||
w := PerformRequest(r, "GET", "/recovery")
|
||||
|
||||
// restore logging
|
||||
log.SetOutput(os.Stderr)
|
||||
|
||||
if w.Code != 500 {
|
||||
t.Errorf("Response code should be Internal Server Error, was: %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used.
|
||||
func TestPanicWithAbort(t *testing.T) {
|
||||
// SETUP
|
||||
log.SetOutput(bytes.NewBuffer(nil))
|
||||
r := New()
|
||||
r.Use(Recovery())
|
||||
r.GET("/recovery", func(c *Context) {
|
||||
c.AbortWithStatus(400)
|
||||
log.Panic("Oupps, Houston, we have a problem")
|
||||
})
|
||||
|
||||
// RUN
|
||||
w := PerformRequest(r, "GET", "/recovery")
|
||||
|
||||
// restore logging
|
||||
log.SetOutput(os.Stderr)
|
||||
|
||||
// TEST
|
||||
if w.Code != 500 {
|
||||
t.Errorf("Response code should be Bad request, was: %d", w.Code)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue