@@ -13,6 +13,7 @@ import (
1313 "net/http/httptest"
1414 "os"
1515 "strconv"
16+ "strings"
1617 "testing"
1718 "time"
1819
@@ -1599,6 +1600,145 @@ func Test_ParseMaxAge(t *testing.T) {
15991600 }
16001601}
16011602
1603+ func Test_AllowsSharedCache (t * testing.T ) {
1604+ t .Parallel ()
1605+
1606+ tests := []struct {
1607+ directives string
1608+ expect bool
1609+ }{
1610+ {"public" , true },
1611+ {"private" , false },
1612+ {"s-maxage=60" , true },
1613+ {"public, max-age=60" , true },
1614+ {"public, must-revalidate" , true },
1615+ {"max-age=60" , false },
1616+ {"no-cache" , false },
1617+ {"no-cache, s-maxage=60" , true },
1618+ {"" , false },
1619+ }
1620+
1621+ for _ , tt := range tests {
1622+ t .Run (tt .directives , func (t * testing.T ) {
1623+ t .Parallel ()
1624+
1625+ got := allowsSharedCache (tt .directives )
1626+ require .Equal (t , tt .expect , got , "directives: %q" , tt .directives )
1627+ })
1628+ }
1629+
1630+ t .Run ("private overrules public" , func (t * testing.T ) {
1631+ t .Parallel ()
1632+
1633+ got := allowsSharedCache (strings .ToUpper ("private, public" ))
1634+ require .False (t , got )
1635+ })
1636+ }
1637+
1638+ func TestCacheSkipsAuthorizationByDefault (t * testing.T ) {
1639+ t .Parallel ()
1640+
1641+ app := fiber .New ()
1642+ app .Use (New ())
1643+
1644+ var count int
1645+ app .Get ("/" , func (c fiber.Ctx ) error {
1646+ count ++
1647+ return c .SendString (strconv .Itoa (count ))
1648+ })
1649+
1650+ req := httptest .NewRequest (fiber .MethodGet , "/" , http .NoBody )
1651+ req .Header .Set (fiber .HeaderAuthorization , "Bearer token" )
1652+
1653+ resp , err := app .Test (req )
1654+ require .NoError (t , err )
1655+ require .Equal (t , cacheUnreachable , resp .Header .Get ("X-Cache" ))
1656+ body , err := io .ReadAll (resp .Body )
1657+ require .NoError (t , err )
1658+ require .Equal (t , "1" , string (body ))
1659+
1660+ req = httptest .NewRequest (fiber .MethodGet , "/" , http .NoBody )
1661+ req .Header .Set (fiber .HeaderAuthorization , "Bearer token" )
1662+
1663+ resp , err = app .Test (req )
1664+ require .NoError (t , err )
1665+ require .Equal (t , cacheUnreachable , resp .Header .Get ("X-Cache" ))
1666+ body , err = io .ReadAll (resp .Body )
1667+ require .NoError (t , err )
1668+ require .Equal (t , "2" , string (body ))
1669+ }
1670+
1671+ func TestCacheBypassesExistingEntryForAuthorization (t * testing.T ) {
1672+ t .Parallel ()
1673+
1674+ app := fiber .New ()
1675+ app .Use (New ())
1676+
1677+ var count int
1678+ app .Get ("/" , func (c fiber.Ctx ) error {
1679+ count ++
1680+ return c .SendString (strconv .Itoa (count ))
1681+ })
1682+
1683+ nonAuthReq := httptest .NewRequest (fiber .MethodGet , "/" , http .NoBody )
1684+
1685+ resp , err := app .Test (nonAuthReq )
1686+ require .NoError (t , err )
1687+ require .Equal (t , cacheMiss , resp .Header .Get ("X-Cache" ))
1688+ body , err := io .ReadAll (resp .Body )
1689+ require .NoError (t , err )
1690+ require .Equal (t , "1" , string (body ))
1691+
1692+ authReq := httptest .NewRequest (fiber .MethodGet , "/" , http .NoBody )
1693+ authReq .Header .Set (fiber .HeaderAuthorization , "Bearer token" )
1694+
1695+ resp , err = app .Test (authReq )
1696+ require .NoError (t , err )
1697+ require .Equal (t , cacheUnreachable , resp .Header .Get ("X-Cache" ))
1698+ body , err = io .ReadAll (resp .Body )
1699+ require .NoError (t , err )
1700+ require .Equal (t , "2" , string (body ))
1701+
1702+ resp , err = app .Test (nonAuthReq )
1703+ require .NoError (t , err )
1704+ require .Equal (t , cacheHit , resp .Header .Get ("X-Cache" ))
1705+ body , err = io .ReadAll (resp .Body )
1706+ require .NoError (t , err )
1707+ require .Equal (t , "1" , string (body ))
1708+ }
1709+
1710+ func TestCacheAllowsSharedCacheWithAuthorization (t * testing.T ) {
1711+ t .Parallel ()
1712+
1713+ app := fiber .New ()
1714+ app .Use (New (Config {Expiration : 10 * time .Second }))
1715+
1716+ var count int
1717+ app .Get ("/" , func (c fiber.Ctx ) error {
1718+ count ++
1719+ c .Set (fiber .HeaderCacheControl , "public, max-age=60" )
1720+ return c .SendString ("ok" )
1721+ })
1722+
1723+ req := httptest .NewRequest (fiber .MethodGet , "/" , http .NoBody )
1724+ req .Header .Set (fiber .HeaderAuthorization , "Bearer token" )
1725+
1726+ resp , err := app .Test (req )
1727+ require .NoError (t , err )
1728+ require .Equal (t , cacheMiss , resp .Header .Get ("X-Cache" ))
1729+
1730+ req = httptest .NewRequest (fiber .MethodGet , "/" , http .NoBody )
1731+ req .Header .Set (fiber .HeaderAuthorization , "Bearer token" )
1732+
1733+ resp , err = app .Test (req )
1734+ require .NoError (t , err )
1735+ require .Equal (t , cacheHit , resp .Header .Get ("X-Cache" ))
1736+ body , err := io .ReadAll (resp .Body )
1737+ require .NoError (t , err )
1738+ require .Equal (t , "ok" , string (body ))
1739+ require .Equal (t , 1 , count )
1740+ }
1741+
16021742// go test -v -run=^$ -bench=Benchmark_Cache -benchmem -count=4
16031743func Benchmark_Cache (b * testing.B ) {
16041744 app := fiber .New ()
0 commit comments