mirror of https://github.com/gin-gonic/gin.git
Improves unit test coverage try my best
This commit is contained in:
parent
cc367f9125
commit
ac0eaab99c
|
@ -33,9 +33,6 @@ import (
|
||||||
var _ context.Context = (*Context)(nil)
|
var _ context.Context = (*Context)(nil)
|
||||||
|
|
||||||
// Unit tests TODO
|
// Unit tests TODO
|
||||||
// func (c *Context) File(filepath string) {
|
|
||||||
// func (c *Context) Negotiate(code int, config Negotiate) {
|
|
||||||
// BAD case: func (c *Context) Render(code int, render render.Render, obj ...interface{}) {
|
|
||||||
// test that information is not leaked when reusing Contexts (using the Pool)
|
// test that information is not leaked when reusing Contexts (using the Pool)
|
||||||
|
|
||||||
func createMultipartRequest() *http.Request {
|
func createMultipartRequest() *http.Request {
|
||||||
|
@ -88,6 +85,31 @@ func TestContextFormFile(t *testing.T) {
|
||||||
assert.NoError(t, c.SaveUploadedFile(f, "test"))
|
assert.NoError(t, c.SaveUploadedFile(f, "test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextFormFileFailed(t *testing.T) {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
|
c.Request, _ = http.NewRequest("POST", "/", buf)
|
||||||
|
_, err := c.FormFile("file")
|
||||||
|
if assert.Error(t, err) {
|
||||||
|
assert.Equal(t, http.ErrNotMultipart, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mw := multipart.NewWriter(buf)
|
||||||
|
w, err := mw.CreateFormFile("file", "test")
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
_, err = w.Write([]byte("test"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
mw.Close()
|
||||||
|
|
||||||
|
c.Request, _ = http.NewRequest("POST", "/", buf)
|
||||||
|
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
|
||||||
|
_, err = c.FormFile("file2")
|
||||||
|
if assert.Error(t, err) {
|
||||||
|
assert.Equal(t, http.ErrMissingFile, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextMultipartForm(t *testing.T) {
|
func TestContextMultipartForm(t *testing.T) {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
mw := multipart.NewWriter(buf)
|
mw := multipart.NewWriter(buf)
|
||||||
|
@ -142,6 +164,7 @@ func TestSaveUploadedCreateFailed(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Error(t, c.SaveUploadedFile(f, "/"))
|
assert.Error(t, c.SaveUploadedFile(f, "/"))
|
||||||
|
assert.Error(t, c.SaveUploadedFile(f, "/file/"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextReset(t *testing.T) {
|
func TestContextReset(t *testing.T) {
|
||||||
|
@ -609,6 +632,13 @@ func TestContextPostFormMultipart(t *testing.T) {
|
||||||
|
|
||||||
dicts = c.PostFormMap("nokey")
|
dicts = c.PostFormMap("nokey")
|
||||||
assert.Equal(t, 0, len(dicts))
|
assert.Equal(t, 0, len(dicts))
|
||||||
|
|
||||||
|
c.Request = createMultipartRequest()
|
||||||
|
c.formCache = nil
|
||||||
|
c.Request.Header = http.Header{"Content-Type": {`multipart/form-data; boundary="foo123"`}}
|
||||||
|
values, ok = c.GetPostFormArray("array")
|
||||||
|
assert.Nil(t, values)
|
||||||
|
assert.False(t, ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextSetCookie(t *testing.T) {
|
func TestContextSetCookie(t *testing.T) {
|
||||||
|
@ -1113,7 +1143,6 @@ func TestContextHeaders(t *testing.T) {
|
||||||
assert.False(t, exist)
|
assert.False(t, exist)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
|
||||||
func TestContextRenderRedirectWithRelativePath(t *testing.T) {
|
func TestContextRenderRedirectWithRelativePath(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
|
|
|
@ -19,10 +19,6 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO
|
|
||||||
// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
|
|
||||||
// func debugPrint(format string, values ...interface{}) {
|
|
||||||
|
|
||||||
func TestIsDebugging(t *testing.T) {
|
func TestIsDebugging(t *testing.T) {
|
||||||
SetMode(DebugMode)
|
SetMode(DebugMode)
|
||||||
assert.True(t, IsDebugging())
|
assert.True(t, IsDebugging())
|
||||||
|
|
|
@ -19,9 +19,6 @@ import (
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO unit tests
|
|
||||||
// test errors
|
|
||||||
|
|
||||||
func TestRenderJSON(t *testing.T) {
|
func TestRenderJSON(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
data := map[string]any{
|
data := map[string]any{
|
||||||
|
@ -105,6 +102,18 @@ func TestRenderSecureJSONFail(t *testing.T) {
|
||||||
// json: unsupported type: chan int
|
// json: unsupported type: chan int
|
||||||
err := (SecureJSON{"while(1);", data}).Render(w)
|
err := (SecureJSON{"while(1);", data}).Render(w)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
// test ResponseWriter.Write failed
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(
|
||||||
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Restrict the length of returned packages
|
||||||
|
w.Header().Set("Content-Length", "0")
|
||||||
|
datas := []map[string]any{{"foo": "bar"}, {"bar": "foo"}}
|
||||||
|
err2 := (SecureJSON{"while(1);", datas}).Render(w)
|
||||||
|
assert.Error(t, err2)
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
_, _ = http.Get(ts.URL)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRenderJsonpJSON(t *testing.T) {
|
func TestRenderJsonpJSON(t *testing.T) {
|
||||||
|
@ -157,6 +166,19 @@ func TestRenderJsonpJSONFail(t *testing.T) {
|
||||||
// json: unsupported type: chan int
|
// json: unsupported type: chan int
|
||||||
err := (JsonpJSON{"x", data}).Render(w)
|
err := (JsonpJSON{"x", data}).Render(w)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
// test ResponseWriter.Write failed
|
||||||
|
for _, l := range []string{"0", "1", "2", "4"} {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(
|
||||||
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Restrict the length of returned packages
|
||||||
|
w.Header().Set("Content-Length", l)
|
||||||
|
err2 := (JsonpJSON{"x", ""}).Render(w)
|
||||||
|
assert.Error(t, err2)
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
_, _ = http.Get(ts.URL)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRenderAsciiJSON(t *testing.T) {
|
func TestRenderAsciiJSON(t *testing.T) {
|
||||||
|
@ -510,3 +532,27 @@ func TestRenderReaderNoContentLength(t *testing.T) {
|
||||||
assert.Equal(t, headers["Content-Disposition"], w.Header().Get("Content-Disposition"))
|
assert.Equal(t, headers["Content-Disposition"], w.Header().Get("Content-Disposition"))
|
||||||
assert.Equal(t, headers["x-request-id"], w.Header().Get("x-request-id"))
|
assert.Equal(t, headers["x-request-id"], w.Header().Get("x-request-id"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReaderTOML(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
data := `
|
||||||
|
title = "TOML Example"
|
||||||
|
[server]
|
||||||
|
IP = "127.0.0.1"
|
||||||
|
Port = ":8080"
|
||||||
|
`
|
||||||
|
(TOML{data}).WriteContentType(w)
|
||||||
|
err := (TOML{data}).Render(w)
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, "application/toml; charset=utf-8", w.Header().Get("Content-Type"))
|
||||||
|
assert.Equal(t, "\"\\n\\ttitle = \\\"TOML Example\\\"\\n\\t[server]\\n\\tIP = \\\"127.0.0.1\\\"\\n\\tPort = \\\":8080\\\"\\n\\t\"", w.Body.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRenderTOMLFail(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
data := make(chan int)
|
||||||
|
|
||||||
|
// TOML: unsupported type: chan int
|
||||||
|
assert.Error(t, (TOML{data}).Render(w))
|
||||||
|
}
|
||||||
|
|
|
@ -12,11 +12,6 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO
|
|
||||||
// func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
||||||
// func (w *responseWriter) CloseNotify() <-chan bool {
|
|
||||||
// func (w *responseWriter) Flush() {
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ ResponseWriter = &responseWriter{}
|
_ ResponseWriter = &responseWriter{}
|
||||||
_ http.ResponseWriter = &responseWriter{}
|
_ http.ResponseWriter = &responseWriter{}
|
||||||
|
|
Loading…
Reference in New Issue