-
Notifications
You must be signed in to change notification settings - Fork 119
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
Add Config.FileContent #1069
Closed
Closed
Add Config.FileContent #1069
Changes from all commits
Commits
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
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package swagger | ||
|
||
import ( | ||
_ "embed" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
|
@@ -10,6 +11,12 @@ import ( | |
"github.com/stretchr/testify/require" | ||
) | ||
|
||
//go:embed swagger.json | ||
var swaggerJSON []byte | ||
|
||
//go:embed swagger.yaml | ||
var swaggerYAML []byte | ||
|
||
func performRequest(method, target string, app *fiber.App) *http.Response { | ||
r := httptest.NewRequest(method, target, nil) | ||
resp, _ := app.Test(r, -1) | ||
|
@@ -259,3 +266,229 @@ func TestNew(t *testing.T) { | |
require.Equal(t, "success", string(bodyBytes)) | ||
}) | ||
} | ||
|
||
func TestNewWithFileContent(t *testing.T) { | ||
t.Run("Endpoint check with only custom path", func(t *testing.T) { | ||
app := fiber.New() | ||
|
||
cfg := Config{ | ||
Path: "custompath", | ||
FileContent: swaggerJSON, | ||
} | ||
app.Use(New(cfg)) | ||
|
||
w1 := performRequest("GET", "/custompath", app) | ||
require.Equal(t, 200, w1.StatusCode) | ||
|
||
w2 := performRequest("GET", "/swagger.json", app) | ||
require.Equal(t, 200, w2.StatusCode) | ||
|
||
w3 := performRequest("GET", "/notfound", app) | ||
require.Equal(t, 404, w3.StatusCode) | ||
}) | ||
|
||
t.Run("Endpoint check with only custom basepath", func(t *testing.T) { | ||
app := fiber.New() | ||
|
||
cfg := Config{ | ||
BasePath: "/api/v1", | ||
FileContent: swaggerJSON, | ||
} | ||
app.Use(New(cfg)) | ||
|
||
w1 := performRequest("GET", "/api/v1/docs", app) | ||
require.Equal(t, 200, w1.StatusCode) | ||
|
||
w2 := performRequest("GET", "/api/v1/swagger.json", app) | ||
require.Equal(t, 200, w2.StatusCode) | ||
|
||
w3 := performRequest("GET", "/notfound", app) | ||
require.Equal(t, 404, w3.StatusCode) | ||
}) | ||
|
||
t.Run("Endpoint check with custom config", func(t *testing.T) { | ||
app := fiber.New() | ||
|
||
cfg := Config{ | ||
BasePath: "/", | ||
FilePath: "swagger.json", | ||
FileContent: swaggerJSON, | ||
} | ||
app.Use(New(cfg)) | ||
|
||
w1 := performRequest("GET", "/docs", app) | ||
require.Equal(t, 200, w1.StatusCode) | ||
|
||
w2 := performRequest("GET", "/swagger.json", app) | ||
require.Equal(t, 200, w2.StatusCode) | ||
|
||
w3 := performRequest("GET", "/notfound", app) | ||
require.Equal(t, 404, w3.StatusCode) | ||
}) | ||
|
||
t.Run("Endpoint check with custom path", func(t *testing.T) { | ||
app := fiber.New() | ||
|
||
cfg := Config{ | ||
BasePath: "/", | ||
FilePath: "swagger.json", | ||
Path: "swagger", | ||
FileContent: swaggerJSON, | ||
} | ||
app.Use(New(cfg)) | ||
|
||
w1 := performRequest("GET", "/swagger", app) | ||
require.Equal(t, 200, w1.StatusCode) | ||
|
||
w2 := performRequest("GET", "/swagger.json", app) | ||
require.Equal(t, 200, w2.StatusCode) | ||
|
||
w3 := performRequest("GET", "/notfound", app) | ||
require.Equal(t, 404, w3.StatusCode) | ||
}) | ||
|
||
t.Run("Endpoint check with custom config and yaml spec", func(t *testing.T) { | ||
app := fiber.New() | ||
|
||
cfg := Config{ | ||
BasePath: "/", | ||
FilePath: "./swagger.yaml", | ||
FileContent: swaggerYAML, | ||
} | ||
app.Use(New(cfg)) | ||
|
||
w1 := performRequest("GET", "/docs", app) | ||
require.Equal(t, 200, w1.StatusCode) | ||
|
||
w2 := performRequest("GET", "/swagger.yaml", app) | ||
require.Equal(t, 200, w2.StatusCode) | ||
|
||
w3 := performRequest("GET", "/notfound", app) | ||
require.Equal(t, 404, w3.StatusCode) | ||
}) | ||
|
||
t.Run("Endpoint check with custom path and yaml spec", func(t *testing.T) { | ||
app := fiber.New() | ||
|
||
cfg := Config{ | ||
BasePath: "/", | ||
FilePath: "swagger.yaml", | ||
Path: "swagger", | ||
FileContent: swaggerYAML, | ||
} | ||
app.Use(New(cfg)) | ||
|
||
w1 := performRequest("GET", "/swagger", app) | ||
require.Equal(t, 200, w1.StatusCode) | ||
|
||
w2 := performRequest("GET", "/swagger.yaml", app) | ||
require.Equal(t, 200, w2.StatusCode) | ||
|
||
w3 := performRequest("GET", "/notfound", app) | ||
require.Equal(t, 404, w3.StatusCode) | ||
}) | ||
|
||
t.Run("Endpoint check with empty custom config", func(t *testing.T) { | ||
app := fiber.New() | ||
|
||
cfg := Config{FileContent: swaggerJSON} | ||
|
||
app.Use(New(cfg)) | ||
|
||
w1 := performRequest("GET", "/docs", app) | ||
require.Equal(t, 200, w1.StatusCode) | ||
|
||
w2 := performRequest("GET", "/swagger.json", app) | ||
require.Equal(t, 200, w2.StatusCode) | ||
|
||
w3 := performRequest("GET", "/notfound", app) | ||
require.Equal(t, 404, w3.StatusCode) | ||
}) | ||
|
||
t.Run("Swagger file content not specified", func(t *testing.T) { | ||
app := fiber.New() | ||
|
||
cfg := Config{ | ||
FilePath: "./docs/swagger.json", | ||
} | ||
|
||
require.Panics(t, func() { | ||
app.Use(New(cfg)) | ||
}, "content not specified") | ||
}) | ||
|
||
t.Run("Endpoint check with multiple Swagger instances", func(t *testing.T) { | ||
app := fiber.New() | ||
|
||
app.Use(New(Config{ | ||
BasePath: "/api/v1", | ||
FileContent: swaggerJSON, | ||
})) | ||
|
||
app.Use(New(Config{ | ||
BasePath: "/api/v2", | ||
FileContent: swaggerJSON, | ||
})) | ||
|
||
w1 := performRequest("GET", "/api/v1/docs", app) | ||
require.Equal(t, 200, w1.StatusCode) | ||
|
||
w2 := performRequest("GET", "/api/v1/swagger.json", app) | ||
require.Equal(t, 200, w2.StatusCode) | ||
|
||
w3 := performRequest("GET", "/api/v2/docs", app) | ||
require.Equal(t, 200, w3.StatusCode) | ||
|
||
w4 := performRequest("GET", "/api/v2/swagger.json", app) | ||
require.Equal(t, 200, w4.StatusCode) | ||
|
||
w5 := performRequest("GET", "/notfound", app) | ||
require.Equal(t, 404, w5.StatusCode) | ||
}) | ||
|
||
t.Run("Endpoint check with custom routes", func(t *testing.T) { | ||
app := fiber.New() | ||
|
||
app.Use(New(Config{ | ||
BasePath: "/api/v1", | ||
FileContent: swaggerJSON, | ||
})) | ||
|
||
app.Get("/api/v1/tasks", func(c *fiber.Ctx) error { | ||
return c.SendString("success") | ||
}) | ||
|
||
app.Get("/api/v1", func(c *fiber.Ctx) error { | ||
return c.SendString("success") | ||
}) | ||
|
||
w1 := performRequest("GET", "/api/v1/docs", app) | ||
require.Equal(t, 200, w1.StatusCode) | ||
|
||
w2 := performRequest("GET", "/api/v1/swagger.json", app) | ||
require.Equal(t, 200, w2.StatusCode) | ||
|
||
w3 := performRequest("GET", "/notfound", app) | ||
require.Equal(t, 404, w3.StatusCode) | ||
|
||
// Verify we can send request to handler with the same BasePath as the middleware | ||
w4 := performRequest("GET", "/api/v1/tasks", app) | ||
bodyBytes, err := io.ReadAll(w4.Body) | ||
require.NoError(t, err) | ||
require.Equal(t, 200, w4.StatusCode) | ||
require.Equal(t, "success", string(bodyBytes)) | ||
|
||
// Verify handler in BasePath still works | ||
w5 := performRequest("GET", "/api/v1", app) | ||
bodyBytes, err = io.ReadAll(w5.Body) | ||
require.NoError(t, err) | ||
require.Equal(t, 200, w5.StatusCode) | ||
require.Equal(t, "success", string(bodyBytes)) | ||
|
||
w6 := performRequest("GET", "/api/v1/", app) | ||
bodyBytes, err = io.ReadAll(w6.Body) | ||
require.NoError(t, err) | ||
require.Equal(t, 200, w6.StatusCode) | ||
require.Equal(t, "success", string(bodyBytes)) | ||
}) | ||
Comment on lines
+270
to
+493
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. Add negative test cases to ensure robust error handling, such as providing invalid JSON/YAML in Would you like me to help by adding these test cases? |
||
} |
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.
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.
Use
errors.Is(err, fs.ErrNotExist)
insteadThere 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.
That is existing code. I tried to minimize the changes. Do you still want me to change it?