From d229aed598800fac0be5261b1b14e4bf9b4e6333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert-Andr=C3=A9=20Mauchin?= <30413512+eclipseo@users.noreply.github.com> Date: Mon, 17 Aug 2020 15:11:03 +0200 Subject: [PATCH] Convert int to string using fmt.Sprintf (#205) --- static.go | 3 ++- static_test.go | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/static.go b/static.go index f1c0c75..04d91d8 100644 --- a/static.go +++ b/static.go @@ -17,6 +17,7 @@ package macaron import ( "encoding/base64" + "fmt" "log" "net/http" "path" @@ -183,7 +184,7 @@ func staticHandler(ctx *Context, log *log.Logger, opt StaticOptions) bool { } if opt.ETag { - tag := `"` + GenerateETag(string(fi.Size()), fi.Name(), fi.ModTime().UTC().Format(http.TimeFormat)) + `"` + tag := `"` + GenerateETag(fmt.Sprintf("%d", fi.Size()), fi.Name(), fi.ModTime().UTC().Format(http.TimeFormat)) + `"` ctx.Resp.Header().Set("ETag", tag) if ctx.Req.Header.Get("If-None-Match") == tag { ctx.Resp.WriteHeader(http.StatusNotModified) diff --git a/static_test.go b/static_test.go index 8d86638..26de6e3 100644 --- a/static_test.go +++ b/static_test.go @@ -17,6 +17,7 @@ package macaron import ( "bytes" + "fmt" "io/ioutil" "net/http" "net/http/httptest" @@ -197,7 +198,7 @@ func Test_Static_Options(t *testing.T) { req, err := http.NewRequest("GET", "http://localhost:4000/macaron.go", nil) So(err, ShouldBeNil) m.ServeHTTP(resp, req) - tag := GenerateETag(string(resp.Body.Len()), "macaron.go", resp.Header().Get("last-modified")) + tag := GenerateETag(fmt.Sprintf("%d", resp.Body.Len()), "macaron.go", resp.Header().Get("last-modified")) So(resp.Header().Get("ETag"), ShouldEqual, `"`+tag+`"`) }) @@ -211,7 +212,7 @@ func Test_Static_Options(t *testing.T) { req, err := http.NewRequest("GET", "http://localhost:4000/macaron.go", nil) So(err, ShouldBeNil) m.ServeHTTP(resp, req) - tag := GenerateETag(string(resp.Body.Len()), "macaron.go", resp.Header().Get("last-modified")) + tag := GenerateETag(fmt.Sprintf("%d", resp.Body.Len()), "macaron.go", resp.Header().Get("last-modified")) // Second request with ETag in If-None-Match resp = httptest.NewRecorder()