Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b044393

Browse filesBrowse files
author
anonx
committed
Added some tests to upload sample
1 parent 799867c commit b044393
Copy full SHA for b044393

File tree

Expand file treeCollapse file tree

5 files changed

+85
-99
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+85
-99
lines changed

‎samples/upload/README.md

Copy file name to clipboardExpand all lines: samples/upload/README.md
-75Lines changed: 0 additions & 75 deletions
This file was deleted.

‎samples/upload/app/controllers/single.go

Copy file name to clipboardExpand all lines: samples/upload/app/controllers/single.go
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ func (c *Single) HandleUpload(avatar []byte) revel.Result {
3737

3838
// Check format of the file.
3939
conf, format, err := image.DecodeConfig(bytes.NewReader(avatar))
40-
c.Validation.Required(err == nil).
40+
c.Validation.Required(err == nil).Key("avatar").
4141
Message("Incorrect file format")
42-
c.Validation.Required(format == "jpeg" || format == "png").
42+
c.Validation.Required(format == "jpeg" || format == "png").Key("avatar").
4343
Message("JPEG or PNG file format is expected")
4444

4545
// Check resolution.
46-
c.Validation.Required(conf.Height >= 150 && conf.Width >= 150).
46+
c.Validation.Required(conf.Height >= 150 && conf.Width >= 150).Key("avatar").
4747
Message("Minimum allowed resolution is 150x150px")
4848

4949
// Handle errors.

‎samples/upload/tests/apptest.go

Copy file name to clipboardExpand all lines: samples/upload/tests/apptest.go
-21Lines changed: 0 additions & 21 deletions
This file was deleted.

‎samples/upload/tests/multipletest.go

Copy file name to clipboard
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package tests
2+
3+
import (
4+
"net/url"
5+
6+
"github.com/revel/revel/samples/upload/app/routes"
7+
8+
"github.com/revel/revel"
9+
)
10+
11+
type MultipleTest struct {
12+
revel.TestSuite
13+
}
14+
15+
func (t *MultipleTest) TestThatMultipleFilesUploadWorks() {
16+
// Make sure it is not allowed to submit less than 2 files.
17+
t.PostFile(routes.Multiple.HandleUpload(), url.Values{}, url.Values{
18+
"file": {
19+
"github.com/revel/revel/samples/upload/public/img/favicon.png",
20+
},
21+
})
22+
t.AssertOk()
23+
t.AssertContains("You cannot submit less than 2 files")
24+
25+
// Make sure upload of 2 files works.
26+
t.PostFile(routes.Multiple.HandleUpload(), url.Values{}, url.Values{
27+
"file[]": {
28+
"github.com/revel/revel/samples/upload/public/img/favicon.png",
29+
"github.com/revel/revel/samples/upload/public/img/glyphicons-halflings.png",
30+
},
31+
})
32+
revel.WARN.Println(string(t.ResponseBody))
33+
t.AssertOk()
34+
t.AssertContains("Successfully uploaded")
35+
t.AssertContains("favicon.png")
36+
t.AssertContains("glyphicons-halflings.png")
37+
t.AssertContains("image/png")
38+
}

‎samples/upload/tests/singletest.go

Copy file name to clipboard
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package tests
2+
3+
import (
4+
"net/url"
5+
6+
"github.com/revel/revel"
7+
)
8+
9+
type SingleTest struct {
10+
revel.TestSuite
11+
}
12+
13+
func (t *SingleTest) TestThatSingleAvatarUploadWorks() {
14+
// Make sure file is required.
15+
t.PostFile("/single/HandleUpload", url.Values{}, url.Values{
16+
"avatar": {},
17+
})
18+
t.AssertOk()
19+
t.AssertContains("Upload demo")
20+
t.AssertContains("Required")
21+
22+
// Make sure incorrect format is not being accepted.
23+
t.PostFile("/single/HandleUpload", url.Values{}, url.Values{
24+
"avatar": {"github.com/revel/revel/samples/upload/public/css/bootstrap.css"},
25+
})
26+
t.AssertOk()
27+
t.AssertContains("Incorrect file format")
28+
29+
// Ensure low resolution avatar cannot be uploaded.
30+
t.PostFile("/single/HandleUpload", url.Values{}, url.Values{
31+
"avatar": {"github.com/revel/revel/samples/upload/public/img/favicon.png"},
32+
})
33+
t.AssertOk()
34+
t.AssertContains("Minimum allowed resolution is 150x150px")
35+
36+
// Check whether correct avatar is uploaded.
37+
t.PostFile("/single/HandleUpload", url.Values{}, url.Values{
38+
"avatar": {"github.com/revel/revel/samples/upload/public/img/glyphicons-halflings.png"},
39+
})
40+
t.AssertOk()
41+
t.AssertContains("image/png")
42+
t.AssertContains("glyphicons-halflings.png")
43+
t.AssertContains("Successfully uploaded")
44+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.