Skip to content

Commit 2ae9ce5

Browse files
committed
test: add HTTP cache integration tests
Signed-off-by: Dwi Siswanto <git@dw1.io>
1 parent 903056f commit 2ae9ce5

3 files changed

Lines changed: 113 additions & 0 deletions

File tree

cmd/integration-test/http.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

9598
type httpMultiVarSharing struct{}
@@ -1756,3 +1759,67 @@ func (h *httpRawUnsafePathSingleSlash) Execute(filepath string) error {
17561759
}
17571760
return nil
17581761
}
1762+
1763+
type httpCache struct{}
1764+
1765+
func (h *httpCache) Execute(filePath string) error {
1766+
router := httprouter.New()
1767+
var requestCount int32
1768+
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
1769+
atomic.AddInt32(&requestCount, 1)
1770+
w.Header().Set("Cache-Control", "max-age=2")
1771+
w.WriteHeader(http.StatusOK)
1772+
_, _ = fmt.Fprint(w, requestCount)
1773+
})
1774+
ts := httptest.NewServer(router)
1775+
defer ts.Close()
1776+
1777+
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, ts.URL, debug)
1778+
if err != nil {
1779+
return err
1780+
}
1781+
1782+
// We expect 2 results because we made 2 requests and both should match
1783+
if err := expectResultsCount(results, 2); err != nil {
1784+
return err
1785+
}
1786+
1787+
// We expect only 1 actual request to the server because of caching
1788+
if count := atomic.LoadInt32(&requestCount); count != 1 {
1789+
return fmt.Errorf("expected 1 request to server, got %d", count)
1790+
}
1791+
1792+
return nil
1793+
}
1794+
1795+
type httpDisableCache struct{}
1796+
1797+
func (h *httpDisableCache) Execute(filePath string) error {
1798+
var requestCount int32
1799+
router := httprouter.New()
1800+
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
1801+
atomic.AddInt32(&requestCount, 1)
1802+
w.Header().Set("Cache-Control", "max-age=2")
1803+
w.WriteHeader(http.StatusOK)
1804+
_, _ = fmt.Fprint(w, requestCount)
1805+
})
1806+
ts := httptest.NewServer(router)
1807+
defer ts.Close()
1808+
1809+
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, ts.URL, debug)
1810+
if err != nil {
1811+
return err
1812+
}
1813+
1814+
// We expect 2 results because we made 2 requests and both should match
1815+
if err := expectResultsCount(results, 2); err != nil {
1816+
return err
1817+
}
1818+
1819+
// We expect 2 actual requests to the server because cache is disabled
1820+
if count := atomic.LoadInt32(&requestCount); count != 2 {
1821+
return fmt.Errorf("expected 2 requests to server, got %d", count)
1822+
}
1823+
1824+
return nil
1825+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
id: disable-http-cache
2+
3+
info:
4+
name: Disable HTTP Cache Test
5+
author: dwisiswant0
6+
severity: info
7+
description: Tests if disable-http-cache works as expected
8+
tags: test
9+
10+
requests:
11+
- raw:
12+
- |
13+
GET / HTTP/1.1
14+
Host: {{Hostname}}
15+
- |
16+
GET / HTTP/1.1
17+
Host: {{Hostname}}
18+
disable-http-cache: true
19+
matchers:
20+
- type: dsl
21+
dsl:
22+
- body_1 == "1"
23+
- body_2 == "2"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
id: http-cache
2+
3+
info:
4+
name: HTTP Cache Test
5+
author: dwisiswant0
6+
severity: info
7+
description: Tests if HTTP cache (RFC 9111) works as expected
8+
tags: test
9+
10+
requests:
11+
- raw:
12+
- |
13+
GET / HTTP/1.1
14+
Host: {{Hostname}}
15+
- |
16+
GET / HTTP/1.1
17+
Host: {{Hostname}}
18+
disable-http-cache: false
19+
matchers:
20+
- type: dsl
21+
dsl:
22+
- body_1 == "1"
23+
- body_2 == "1"

0 commit comments

Comments
 (0)