Skip to content

Commit ea668ec

Browse files
committed
test: add HTTP cache integration tests
Signed-off-by: Dwi Siswanto <git@dw1.io>
1 parent 800a4f5 commit ea668ec

3 files changed

Lines changed: 112 additions & 0 deletions

File tree

cmd/integration-test/http.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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,7 @@ 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{}},
9193
}
9294

9395
type httpMultiVarSharing struct{}
@@ -1685,3 +1687,67 @@ func (h *httpRawUnsafePathSingleSlash) Execute(filepath string) error {
16851687
}
16861688
return nil
16871689
}
1690+
1691+
type httpCache struct{}
1692+
1693+
func (h *httpCache) Execute(filePath string) error {
1694+
router := httprouter.New()
1695+
var requestCount int32
1696+
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
1697+
atomic.AddInt32(&requestCount, 1)
1698+
w.Header().Set("Cache-Control", "max-age=2")
1699+
w.WriteHeader(http.StatusOK)
1700+
fmt.Fprint(w, requestCount)
1701+
})
1702+
ts := httptest.NewServer(router)
1703+
defer ts.Close()
1704+
1705+
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, ts.URL, debug)
1706+
if err != nil {
1707+
return err
1708+
}
1709+
1710+
// We expect 2 results because we made 2 requests and both should match
1711+
if err := expectResultsCount(results, 2); err != nil {
1712+
return err
1713+
}
1714+
1715+
// We expect only 1 actual request to the server because of caching
1716+
if count := atomic.LoadInt32(&requestCount); count != 1 {
1717+
return fmt.Errorf("expected 1 request to server, got %d", count)
1718+
}
1719+
1720+
return nil
1721+
}
1722+
1723+
type httpDisableCache struct{}
1724+
1725+
func (h *httpDisableCache) Execute(filePath string) error {
1726+
var requestCount int32
1727+
router := httprouter.New()
1728+
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
1729+
atomic.AddInt32(&requestCount, 1)
1730+
w.Header().Set("Cache-Control", "max-age=2")
1731+
w.WriteHeader(http.StatusOK)
1732+
fmt.Fprint(w, requestCount)
1733+
})
1734+
ts := httptest.NewServer(router)
1735+
defer ts.Close()
1736+
1737+
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, ts.URL, debug)
1738+
if err != nil {
1739+
return err
1740+
}
1741+
1742+
// We expect 2 results because we made 2 requests and both should match
1743+
if err := expectResultsCount(results, 2); err != nil {
1744+
return err
1745+
}
1746+
1747+
// We expect 2 actual requests to the server because cache is disabled
1748+
if count := atomic.LoadInt32(&requestCount); count != 2 {
1749+
return fmt.Errorf("expected 2 requests to server, got %d", count)
1750+
}
1751+
1752+
return nil
1753+
}
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)