From ac0eaab99cafa174223a8fbf97cdeb3eb52846ec Mon Sep 17 00:00:00 2001 From: Cookiery <2061803022@qq.com> Date: Sun, 4 Dec 2022 20:12:18 +0800 Subject: [PATCH] Improves unit test coverage try my best --- context_test.go | 37 +++++++++++++++++++++++++---- debug_test.go | 4 ---- render/render_test.go | 52 ++++++++++++++++++++++++++++++++++++++--- response_writer_test.go | 5 ---- 4 files changed, 82 insertions(+), 16 deletions(-) diff --git a/context_test.go b/context_test.go index 85e0a616..842a150b 100644 --- a/context_test.go +++ b/context_test.go @@ -33,9 +33,6 @@ import ( var _ context.Context = (*Context)(nil) // 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) func createMultipartRequest() *http.Request { @@ -88,6 +85,31 @@ func TestContextFormFile(t *testing.T) { 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) { buf := new(bytes.Buffer) 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, "/file/")) } func TestContextReset(t *testing.T) { @@ -609,6 +632,13 @@ func TestContextPostFormMultipart(t *testing.T) { dicts = c.PostFormMap("nokey") 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) { @@ -1113,7 +1143,6 @@ func TestContextHeaders(t *testing.T) { assert.False(t, exist) } -// TODO func TestContextRenderRedirectWithRelativePath(t *testing.T) { w := httptest.NewRecorder() c, _ := CreateTestContext(w) diff --git a/debug_test.go b/debug_test.go index abe8b41c..9ab2ed3c 100644 --- a/debug_test.go +++ b/debug_test.go @@ -19,10 +19,6 @@ import ( "github.com/stretchr/testify/assert" ) -// TODO -// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) { -// func debugPrint(format string, values ...interface{}) { - func TestIsDebugging(t *testing.T) { SetMode(DebugMode) assert.True(t, IsDebugging()) diff --git a/render/render_test.go b/render/render_test.go index 3509db3c..edb2466b 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -19,9 +19,6 @@ import ( "google.golang.org/protobuf/proto" ) -// TODO unit tests -// test errors - func TestRenderJSON(t *testing.T) { w := httptest.NewRecorder() data := map[string]any{ @@ -105,6 +102,18 @@ func TestRenderSecureJSONFail(t *testing.T) { // json: unsupported type: chan int err := (SecureJSON{"while(1);", data}).Render(w) 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) { @@ -157,6 +166,19 @@ func TestRenderJsonpJSONFail(t *testing.T) { // json: unsupported type: chan int err := (JsonpJSON{"x", data}).Render(w) 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) { @@ -510,3 +532,27 @@ func TestRenderReaderNoContentLength(t *testing.T) { assert.Equal(t, headers["Content-Disposition"], w.Header().Get("Content-Disposition")) 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)) +} diff --git a/response_writer_test.go b/response_writer_test.go index 57d163c9..209f5500 100644 --- a/response_writer_test.go +++ b/response_writer_test.go @@ -12,11 +12,6 @@ import ( "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 ( _ ResponseWriter = &responseWriter{} _ http.ResponseWriter = &responseWriter{}