-
Notifications
You must be signed in to change notification settings - Fork 88
Implement file size limit in the PostObject method of api/controllers/storage.go for cloud storage. #380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Implement file size limit in the PostObject method of api/controllers/storage.go for cloud storage. #380
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6de7a4c
feat: implement maximum upload size and fix storage routes
SiddhaarthB11 ff1fbfd
signed-url-upload-size-changes
SiddhaarthB11 c5680f5
Merge branch 'develop' into feature/upload-limit
SiddhaarthB11 1112161
Merge branch 'develop' into feature/upload-limit
mikehquan19 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "cloud.google.com/go/storage" | ||
| "github.com/UTDNebula/nebula-api/api/controllers" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| func TestMaxUploadSize(t *testing.T) { | ||
| // Set the environment variable for max upload size (e.g., 100 bytes) | ||
| os.Setenv("MAX_UPLOAD_SIZE", "100") | ||
| defer os.Unsetenv("MAX_UPLOAD_SIZE") | ||
|
|
||
| // Setup Gin | ||
| gin.SetMode(gin.TestMode) | ||
| router := gin.New() | ||
|
|
||
| router.POST("/storage/:bucket/:objectID", func(c *gin.Context) { | ||
|
|
||
| c.Set("gcsClient", &storage.Client{}) | ||
| controllers.PostObject(c) | ||
| }) | ||
|
|
||
| t.Run("Upload within limit", func(t *testing.T) { | ||
|
|
||
| defer func() { | ||
| if r := recover(); r != nil { | ||
|
|
||
| } | ||
| }() | ||
|
|
||
| data := make([]byte, 50) | ||
| req, _ := http.NewRequest("POST", "/storage/test-bucket/small-file", bytes.NewBuffer(data)) | ||
| req.Header.Set("Content-Type", "application/octet-stream") | ||
|
|
||
| w := httptest.NewRecorder() | ||
| router.ServeHTTP(w, req) | ||
|
|
||
| if w.Code == http.StatusRequestEntityTooLarge { | ||
| t.Errorf("Expected status NOT 413, got %d", w.Code) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("Upload exceeding limit via Content-Length", func(t *testing.T) { | ||
| data := make([]byte, 150) | ||
| req, _ := http.NewRequest("POST", "/storage/test-bucket/large-file", bytes.NewBuffer(data)) | ||
| req.Header.Set("Content-Type", "application/octet-stream") | ||
|
|
||
| w := httptest.NewRecorder() | ||
| router.ServeHTTP(w, req) | ||
|
|
||
| if w.Code != http.StatusRequestEntityTooLarge { | ||
| t.Errorf("Expected status 413, got %d. Body: %s", w.Code, w.Body.String()) | ||
| } | ||
| if !strings.Contains(w.Body.String(), "File too large") { | ||
| t.Errorf("Expected error message 'File too large', got %s", w.Body.String()) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("Upload exceeding limit via Stream", func(t *testing.T) { | ||
| pr, pw := io.Pipe() | ||
| go func() { | ||
| pw.Write(make([]byte, 150)) | ||
| pw.Close() | ||
| }() | ||
|
|
||
| req, _ := http.NewRequest("POST", "/storage/test-bucket/stream-file", pr) | ||
| req.ContentLength = -1 | ||
| req.Header.Set("Content-Type", "application/octet-stream") | ||
|
|
||
| w := httptest.NewRecorder() | ||
| router.ServeHTTP(w, req) | ||
|
|
||
| if w.Code != http.StatusRequestEntityTooLarge { | ||
| t.Errorf("Expected status 413, got %d. Body: %s", w.Code, w.Body.String()) | ||
| } | ||
| if !strings.Contains(w.Body.String(), "File too large") { | ||
| t.Errorf("Expected error message 'File too large', got %s", w.Body.String()) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.