diff --git a/hash.go b/hash.go index 951aa20..a80e9c1 100644 --- a/hash.go +++ b/hash.go @@ -12,7 +12,12 @@ import ( type RequestHashFn func(req *http.Request) string func simpleRequestHash(req *http.Request) string { - return fmt.Sprintf("%s:%s:%s", req.Method, req.URL.String(), hash(req.Header)) + return fmt.Sprintf("%s:%s:%s", req.Method, sha256str([]byte(req.URL.String())), hash(req.Header)) +} + +func sha256str(key []byte) string { + hash := sha256.Sum256(key) + return hex.EncodeToString(hash[:]) } const delimiter = "|" @@ -31,6 +36,5 @@ func hash(headers http.Header) string { sb.WriteString(fmt.Sprintf("%s:%s%s", key, headers.Get(key), delimiter)) } - hash := sha256.Sum256([]byte(sb.String())) - return hex.EncodeToString(hash[:]) + return sha256str([]byte(sb.String())) } diff --git a/hash_test.go b/hash_test.go new file mode 100644 index 0000000..c11386d --- /dev/null +++ b/hash_test.go @@ -0,0 +1,29 @@ +package httpcache + +import ( + "net/http" + "net/url" + "testing" +) + +func mustParse(t *testing.T, urlstr string) *url.URL { + t.Helper() + parsed, err := url.ParseRequestURI(urlstr) + if err != nil { + t.Fatalf("unable to parse url string: %v", err) + } + + return parsed +} + +func TestSimpleRequestHash(t *testing.T) { + req := &http.Request{ + Method: http.MethodGet, + URL: mustParse(t, "http://www.google.com"), + Header: http.Header{"key": {"value"}}, + } + + if got, want := simpleRequestHash(req), "GET:253d142703041dd25197550a0fc11d6ac03befc1e64a1320009f1edf400c39ad:7cb0ba540850f2f8b7f62da704748662704cfa62c97c36bf251f26d656610656"; got != want { + t.Fatalf("expected %s; got %s", want, got) + } +} diff --git a/postgres/store.go b/postgres/cache.go similarity index 68% rename from postgres/store.go rename to postgres/cache.go index f351200..a5f6ac6 100644 --- a/postgres/store.go +++ b/postgres/cache.go @@ -9,10 +9,10 @@ import ( "github.com/jackc/pgx/v5/pgtype" ) -type store struct{ queries *postgres.Queries } +type cache struct{ queries *postgres.Queries } -func (s *store) CacheResponse(ctx context.Context, arg httpcache.Params) (httpcache.Response, error) { - return wrapPostgresResponse(s.queries.CacheResponse(ctx, postgres.CacheResponseParams{ +func (c *cache) CacheResponse(ctx context.Context, arg httpcache.Params) (httpcache.Response, error) { + return wrapPostgresResponse(c.queries.CacheResponse(ctx, postgres.CacheResponseParams{ ReqHash: arg.ReqHash, Body: pgtype.Text{String: arg.Body, Valid: true}, Headers: pgtype.Text{String: arg.Headers, Valid: true}, @@ -20,12 +20,12 @@ func (s *store) CacheResponse(ctx context.Context, arg httpcache.Params) (httpca })) } -func (s *store) DeleteAllResponses(ctx context.Context) error { - return s.queries.DeleteAllResponses(ctx) +func (c *cache) DeleteAllResponses(ctx context.Context) error { + return c.queries.DeleteAllResponses(ctx) } -func (s *store) GetResponse(ctx context.Context, reqHash string) (httpcache.Response, error) { - return wrapPostgresResponse(s.queries.GetResponse(ctx, reqHash)) +func (c *cache) GetResponse(ctx context.Context, reqHash string) (httpcache.Response, error) { + return wrapPostgresResponse(c.queries.GetResponse(ctx, reqHash)) } func wrapPostgresResponse(res postgres.Response, err error) (httpcache.Response, error) { @@ -45,5 +45,5 @@ func (c Connection) Init(ctx context.Context) (httpcache.ResponseCacher, error) return nil, err } - return &store{queries: postgres.New(c.Conn)}, nil + return &cache{queries: postgres.New(c.Conn)}, nil } diff --git a/sqlite/store.go b/sqlite/cache.go similarity index 100% rename from sqlite/store.go rename to sqlite/cache.go