@@ -12,6 +12,7 @@ import (
1212 "strconv"
1313 "strings"
1414 "sync"
15+ "sync/atomic"
1516 "time"
1617
1718 "github.com/julienschmidt/httprouter"
@@ -90,6 +91,8 @@ var httpTestcases = []TestCaseInfo{
9091 {Path : "protocols/http/multi-http-var-sharing.yaml" , TestCase : & httpMultiVarSharing {}},
9192 {Path : "protocols/http/raw-path-single-slash.yaml" , TestCase : & httpRawPathSingleSlash {}},
9293 {Path : "protocols/http/raw-unsafe-path-single-slash.yaml" , TestCase : & httpRawUnsafePathSingleSlash {}},
94+ {Path : "protocols/http/disable-http-cache.yaml" , TestCase : & httpDisableCache {}},
95+ {Path : "protocols/http/http-cache.yaml" , TestCase : & httpCache {}},
9396}
9497
9598type httpMultiVarSharing struct {}
@@ -1732,3 +1735,67 @@ func (h *httpRawUnsafePathSingleSlash) Execute(filepath string) error {
17321735 }
17331736 return nil
17341737}
1738+
1739+ type httpCache struct {}
1740+
1741+ func (h * httpCache ) Execute (filePath string ) error {
1742+ router := httprouter .New ()
1743+ var requestCount int32
1744+ router .GET ("/" , func (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
1745+ atomic .AddInt32 (& requestCount , 1 )
1746+ w .Header ().Set ("Cache-Control" , "max-age=2" )
1747+ w .WriteHeader (http .StatusOK )
1748+ _ , _ = fmt .Fprint (w , requestCount )
1749+ })
1750+ ts := httptest .NewServer (router )
1751+ defer ts .Close ()
1752+
1753+ results , err := testutils .RunNucleiTemplateAndGetResults (filePath , ts .URL , debug )
1754+ if err != nil {
1755+ return err
1756+ }
1757+
1758+ // We expect 2 results because we made 2 requests and both should match
1759+ if err := expectResultsCount (results , 2 ); err != nil {
1760+ return err
1761+ }
1762+
1763+ // We expect only 1 actual request to the server because of caching
1764+ if count := atomic .LoadInt32 (& requestCount ); count != 1 {
1765+ return fmt .Errorf ("expected 1 request to server, got %d" , count )
1766+ }
1767+
1768+ return nil
1769+ }
1770+
1771+ type httpDisableCache struct {}
1772+
1773+ func (h * httpDisableCache ) Execute (filePath string ) error {
1774+ var requestCount int32
1775+ router := httprouter .New ()
1776+ router .GET ("/" , func (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
1777+ atomic .AddInt32 (& requestCount , 1 )
1778+ w .Header ().Set ("Cache-Control" , "max-age=2" )
1779+ w .WriteHeader (http .StatusOK )
1780+ _ , _ = fmt .Fprint (w , requestCount )
1781+ })
1782+ ts := httptest .NewServer (router )
1783+ defer ts .Close ()
1784+
1785+ results , err := testutils .RunNucleiTemplateAndGetResults (filePath , ts .URL , debug )
1786+ if err != nil {
1787+ return err
1788+ }
1789+
1790+ // We expect 2 results because we made 2 requests and both should match
1791+ if err := expectResultsCount (results , 2 ); err != nil {
1792+ return err
1793+ }
1794+
1795+ // We expect 2 actual requests to the server because cache is disabled
1796+ if count := atomic .LoadInt32 (& requestCount ); count != 2 {
1797+ return fmt .Errorf ("expected 2 requests to server, got %d" , count )
1798+ }
1799+
1800+ return nil
1801+ }
0 commit comments