Skip to content

Commit

Permalink
Convert int to string using fmt.Sprintf (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
eclipseo authored Aug 17, 2020
1 parent fa1a373 commit d229aed
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion static.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package macaron

import (
"encoding/base64"
"fmt"
"log"
"net/http"
"path"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package macaron

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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+`"`)
})
Expand All @@ -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()
Expand Down

0 comments on commit d229aed

Please sign in to comment.