update static file handling and tests
This commit is contained in:
parent
ddd6e5696f
commit
64918503a0
9
ron.go
9
ron.go
@ -43,11 +43,10 @@ type (
|
||||
)
|
||||
|
||||
const (
|
||||
ContentType string = "Content-Type"
|
||||
HeaderJSON string = "application/json"
|
||||
HeaderHTML_UTF8 string = "text/html; charset=utf-8"
|
||||
HeaderCSS_UTF8 string = "text/css; charset=utf-8"
|
||||
HeaderJS_UTF8 string = "text/javascript; charset=utf-8"
|
||||
HeaderAppJS string = "application/javascript"
|
||||
HeaderPlain_UTF8 string = "text/plain; charset=utf-8"
|
||||
)
|
||||
|
||||
@ -157,7 +156,7 @@ func (g *groupMux) POST(path string, handler func(*Context)) {
|
||||
// Calling r.Static("assets", "./folder") will make the contents of the "./folder" directory
|
||||
// accessible in the browser at "/assets". For instance, a file located at "./folder/image.png"
|
||||
// would be available at "/assets/image.png" in HTML templates.
|
||||
func (e *Engine) Static(path, dir string) {
|
||||
func (e *Engine) Static(path, dir string) error {
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
@ -170,12 +169,14 @@ func (e *Engine) Static(path, dir string) {
|
||||
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
slog.Error("static directory does not exist", "path", path, "dir", dir)
|
||||
return
|
||||
e.mux.Handle(path, http.NotFoundHandler())
|
||||
return err
|
||||
}
|
||||
|
||||
fs := http.FileServer(http.Dir(dir))
|
||||
e.mux.Handle(path, http.StripPrefix(path, fs))
|
||||
slog.Info("static files served", "path", path, "dir", dir)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Context) JSON(code int, data any) {
|
||||
|
||||
18
ron_test.go
18
ron_test.go
@ -41,7 +41,6 @@ func TestMain(m *testing.M) {
|
||||
f.WriteString("console.log('Hello, World!');")
|
||||
f.Close()
|
||||
|
||||
|
||||
code := m.Run()
|
||||
|
||||
os.RemoveAll("templates")
|
||||
@ -328,7 +327,7 @@ func Test_Static(t *testing.T) {
|
||||
givenDirectory: "assets",
|
||||
expectedResponse: testhelpers.ExpectedResponse{
|
||||
Code: http.StatusOK,
|
||||
Header: HeaderJS_UTF8,
|
||||
Header: HeaderAppJS,
|
||||
Body: "console.log('Hello, World!');",
|
||||
},
|
||||
},
|
||||
@ -342,16 +341,25 @@ func Test_Static(t *testing.T) {
|
||||
Body: "404 page not found\n",
|
||||
},
|
||||
},
|
||||
"invalid directory": {
|
||||
givenPath: "assets",
|
||||
givenFile: "style.css",
|
||||
givenDirectory: "nonexistent",
|
||||
expectedResponse: testhelpers.ExpectedResponse{
|
||||
Code: http.StatusNotFound,
|
||||
Header: HeaderPlain_UTF8,
|
||||
Body: "404 page not found\n",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tt := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
e := New()
|
||||
e.Static(tt.givenPath, tt.givenDirectory)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf("/%s/%s", tt.givenPath, tt.givenFile), nil)
|
||||
e := New()
|
||||
e.Static(tt.givenPath, tt.givenDirectory)
|
||||
e.ServeHTTP(rr, req)
|
||||
|
||||
testhelpers.VerifyResponse(t, rr, tt.expectedResponse)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user