-
-
Notifications
You must be signed in to change notification settings - Fork 463
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
404-test-OK #2725
base: master
Are you sure you want to change the base?
404-test-OK #2725
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
module main | ||
module 404 | ||
|
||
go 1.18 | ||
go 1.23.3 | ||
|
||
require github.com/gofiber/fiber/v2 v2.52.5 | ||
|
||
require ( | ||
github.com/andybalholm/brotli v1.0.5 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/google/uuid v1.5.0 // indirect | ||
github.com/klauspost/compress v1.17.0 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/mattn/go-runewidth v0.0.15 // indirect | ||
github.com/rivo/uniseg v0.4.4 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/rivo/uniseg v0.2.0 // indirect | ||
github.com/stretchr/testify v1.10.0 // indirect | ||
github.com/valyala/bytebufferpool v1.0.0 // indirect | ||
github.com/valyala/fasthttp v1.51.0 // indirect | ||
github.com/valyala/tcplisten v1.0.0 // indirect | ||
golang.org/x/sys v0.15.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,4 @@ func main() { | |
// Handler | ||
func hello(c *fiber.Ctx) error { | ||
return c.SendString("I made a ☕ for you!") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,57 @@ | ||||||||||||||||||||
package main | ||||||||||||||||||||
|
||||||||||||||||||||
import ( | ||||||||||||||||||||
"net/http" | ||||||||||||||||||||
"net/http/httptest" | ||||||||||||||||||||
"testing" | ||||||||||||||||||||
|
||||||||||||||||||||
"github.com/gofiber/fiber/v2" | ||||||||||||||||||||
"github.com/stretchr/testify/assert" | ||||||||||||||||||||
) | ||||||||||||||||||||
|
||||||||||||||||||||
// Setup function to initialize the Fiber app | ||||||||||||||||||||
func setupApp() *fiber.App { | ||||||||||||||||||||
app := fiber.New() | ||||||||||||||||||||
|
||||||||||||||||||||
// Routes | ||||||||||||||||||||
app.Get("/hello", hello) | ||||||||||||||||||||
|
||||||||||||||||||||
// 404 Handler | ||||||||||||||||||||
app.Use(func(c *fiber.Ctx) error { | ||||||||||||||||||||
return c.SendStatus(404) | ||||||||||||||||||||
}) | ||||||||||||||||||||
|
||||||||||||||||||||
return app | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func TestHelloRoute(t *testing.T) { | ||||||||||||||||||||
// Initialize the app | ||||||||||||||||||||
app := setupApp() | ||||||||||||||||||||
|
||||||||||||||||||||
// Create a test request | ||||||||||||||||||||
req := httptest.NewRequest(http.MethodGet, "/hello", nil) | ||||||||||||||||||||
resp, _ := app.Test(req, -1) // -1 disables timeout | ||||||||||||||||||||
|
||||||||||||||||||||
// Check the response | ||||||||||||||||||||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||||||||||||||||||||
|
||||||||||||||||||||
// Read the response body | ||||||||||||||||||||
body := make([]byte, resp.ContentLength) | ||||||||||||||||||||
resp.Body.Read(body) | ||||||||||||||||||||
defer resp.Body.Close() | ||||||||||||||||||||
Comment on lines
+39
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential read errors. The response body read operation should handle potential errors. - body := make([]byte, resp.ContentLength)
- resp.Body.Read(body)
+ body := make([]byte, resp.ContentLength)
+ _, err := resp.Body.Read(body)
+ if err != nil {
+ t.Fatalf("Failed to read response body: %v", err)
+ } 📝 Committable suggestion
Suggested change
🧰 Tools🪛 golangci-lint (1.62.2)40-40: Error return value of (errcheck) |
||||||||||||||||||||
|
||||||||||||||||||||
// Assert the response body | ||||||||||||||||||||
assert.Equal(t, "I made a ☕ for you!", string(body)) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func TestNotFoundRoute(t *testing.T) { | ||||||||||||||||||||
// Initialize the app | ||||||||||||||||||||
app := setupApp() | ||||||||||||||||||||
|
||||||||||||||||||||
// Create a test request for an unknown route | ||||||||||||||||||||
req := httptest.NewRequest(http.MethodGet, "/unknown", nil) | ||||||||||||||||||||
resp, _ := app.Test(req, -1) // -1 disables timeout | ||||||||||||||||||||
|
||||||||||||||||||||
// Check the response | ||||||||||||||||||||
assert.Equal(t, http.StatusNotFound, resp.StatusCode) | ||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Remove redundant code example.
The code example at the end of the README is redundant as it's already shown in the "Code Overview" section above.
Consider removing lines 192-232 to avoid duplication and potential maintenance issues when the code changes.
🧰 Tools
🪛 LanguageTool
[style] ~230-~230: If you’re making a request, consider inserting ‘please’ to sound more courteous and respectful.
Context: ...ndString("I made a ☕ for you!") } ``` Can you show me how to write a unit test file f...
(CAN_YOU)
🪛 Markdownlint (0.35.0)
203-203: Column: 1
Hard tabs
(MD010, no-hard-tabs)
205-205: Column: 1
Hard tabs
(MD010, no-hard-tabs)
209-209: Column: 1
Hard tabs
(MD010, no-hard-tabs)
210-210: Column: 1
Hard tabs
(MD010, no-hard-tabs)
212-212: Column: 1
Hard tabs
(MD010, no-hard-tabs)
213-213: Column: 1
Hard tabs
(MD010, no-hard-tabs)
215-215: Column: 1
Hard tabs
(MD010, no-hard-tabs)
216-216: Column: 1
Hard tabs
(MD010, no-hard-tabs)
217-217: Column: 1
Hard tabs
(MD010, no-hard-tabs)
218-218: Column: 1
Hard tabs
(MD010, no-hard-tabs)
220-220: Column: 1
Hard tabs
(MD010, no-hard-tabs)
221-221: Column: 1
Hard tabs
(MD010, no-hard-tabs)
226-226: Column: 1
Hard tabs
(MD010, no-hard-tabs)
194-194: Expected: 1; Actual: 2
Multiple consecutive blank lines
(MD012, no-multiple-blanks)
195-195: null
Fenced code blocks should have a language specified
(MD040, fenced-code-language)