@@ -11,6 +11,7 @@ import (
1111 "regexp"
1212 "strconv"
1313 "strings"
14+ "sync/atomic"
1415 "time"
1516
1617 "github.com/julienschmidt/httprouter"
@@ -88,6 +89,8 @@ var httpTestcases = []TestCaseInfo{
8889 {Path : "protocols/http/multi-http-var-sharing.yaml" , TestCase : & httpMultiVarSharing {}},
8990 {Path : "protocols/http/raw-path-single-slash.yaml" , TestCase : & httpRawPathSingleSlash {}},
9091 {Path : "protocols/http/raw-unsafe-path-single-slash.yaml" , TestCase : & httpRawUnsafePathSingleSlash {}},
92+ {Path : "protocols/http/disable-http-cache.yaml" , TestCase : & httpDisableCache {}},
93+ {Path : "protocols/http/http-cache.yaml" , TestCase : & httpCache {}},
9194}
9295
9396type httpMultiVarSharing struct {}
@@ -1685,3 +1688,67 @@ func (h *httpRawUnsafePathSingleSlash) Execute(filepath string) error {
16851688 }
16861689 return nil
16871690}
1691+
1692+ type httpCache struct {}
1693+
1694+ func (h * httpCache ) Execute (filePath string ) error {
1695+ router := httprouter .New ()
1696+ var requestCount int32
1697+ router .GET ("/" , func (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
1698+ atomic .AddInt32 (& requestCount , 1 )
1699+ w .Header ().Set ("Cache-Control" , "max-age=2" )
1700+ w .WriteHeader (http .StatusOK )
1701+ _ , _ = fmt .Fprint (w , requestCount )
1702+ })
1703+ ts := httptest .NewServer (router )
1704+ defer ts .Close ()
1705+
1706+ results , err := testutils .RunNucleiTemplateAndGetResults (filePath , ts .URL , debug )
1707+ if err != nil {
1708+ return err
1709+ }
1710+
1711+ // We expect 2 results because we made 2 requests and both should match
1712+ if err := expectResultsCount (results , 2 ); err != nil {
1713+ return err
1714+ }
1715+
1716+ // We expect only 1 actual request to the server because of caching
1717+ if count := atomic .LoadInt32 (& requestCount ); count != 1 {
1718+ return fmt .Errorf ("expected 1 request to server, got %d" , count )
1719+ }
1720+
1721+ return nil
1722+ }
1723+
1724+ type httpDisableCache struct {}
1725+
1726+ func (h * httpDisableCache ) Execute (filePath string ) error {
1727+ var requestCount int32
1728+ router := httprouter .New ()
1729+ router .GET ("/" , func (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
1730+ atomic .AddInt32 (& requestCount , 1 )
1731+ w .Header ().Set ("Cache-Control" , "max-age=2" )
1732+ w .WriteHeader (http .StatusOK )
1733+ _ , _ = fmt .Fprint (w , requestCount )
1734+ })
1735+ ts := httptest .NewServer (router )
1736+ defer ts .Close ()
1737+
1738+ results , err := testutils .RunNucleiTemplateAndGetResults (filePath , ts .URL , debug )
1739+ if err != nil {
1740+ return err
1741+ }
1742+
1743+ // We expect 2 results because we made 2 requests and both should match
1744+ if err := expectResultsCount (results , 2 ); err != nil {
1745+ return err
1746+ }
1747+
1748+ // We expect 2 actual requests to the server because cache is disabled
1749+ if count := atomic .LoadInt32 (& requestCount ); count != 2 {
1750+ return fmt .Errorf ("expected 2 requests to server, got %d" , count )
1751+ }
1752+
1753+ return nil
1754+ }
0 commit comments