Skip to content

Commit 1f2ed4e

Browse files
committed
Fix go1.17 test error (#2856)
1 parent d420ebc commit 1f2ed4e

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

context_1.16_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2021 Gin Core Team. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build !go1.17
6+
// +build !go1.17
7+
8+
package gin
9+
10+
import (
11+
"bytes"
12+
"mime/multipart"
13+
"net/http"
14+
"net/http/httptest"
15+
"testing"
16+
17+
"github.com/stretchr/testify/assert"
18+
)
19+
20+
func TestContextFormFileFailed16(t *testing.T) {
21+
buf := new(bytes.Buffer)
22+
mw := multipart.NewWriter(buf)
23+
mw.Close()
24+
c, _ := CreateTestContext(httptest.NewRecorder())
25+
c.Request, _ = http.NewRequest("POST", "/", nil)
26+
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
27+
c.engine.MaxMultipartMemory = 8 << 20
28+
f, err := c.FormFile("file")
29+
assert.Error(t, err)
30+
assert.Nil(t, f)
31+
}

context_1.17_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2021 Gin Core Team. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build go1.17
6+
// +build go1.17
7+
8+
package gin
9+
10+
import (
11+
"bytes"
12+
"mime/multipart"
13+
"net/http"
14+
"net/http/httptest"
15+
"testing"
16+
17+
"github.com/stretchr/testify/assert"
18+
)
19+
20+
func TestContextFormFileFailed17(t *testing.T) {
21+
buf := new(bytes.Buffer)
22+
mw := multipart.NewWriter(buf)
23+
mw.Close()
24+
c, _ := CreateTestContext(httptest.NewRecorder())
25+
c.Request, _ = http.NewRequest("POST", "/", nil)
26+
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
27+
c.engine.MaxMultipartMemory = 8 << 20
28+
assert.Panics(t, func() {
29+
f, err := c.FormFile("file")
30+
assert.Error(t, err)
31+
assert.Nil(t, f)
32+
})
33+
}

context_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,6 @@ func TestContextFormFile(t *testing.T) {
8787
assert.NoError(t, c.SaveUploadedFile(f, "test"))
8888
}
8989

90-
func TestContextFormFileFailed(t *testing.T) {
91-
buf := new(bytes.Buffer)
92-
mw := multipart.NewWriter(buf)
93-
mw.Close()
94-
c, _ := CreateTestContext(httptest.NewRecorder())
95-
c.Request, _ = http.NewRequest("POST", "/", nil)
96-
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
97-
c.engine.MaxMultipartMemory = 8 << 20
98-
f, err := c.FormFile("file")
99-
assert.Error(t, err)
100-
assert.Nil(t, f)
101-
}
102-
10390
func TestContextMultipartForm(t *testing.T) {
10491
buf := new(bytes.Buffer)
10592
mw := multipart.NewWriter(buf)

0 commit comments

Comments
 (0)