Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/lib/sdk_test.go
2061 views
1
package nuclei_test
2
3
import (
4
"context"
5
"log"
6
"testing"
7
"time"
8
9
nuclei "github.com/projectdiscovery/nuclei/v3/lib"
10
"github.com/stretchr/testify/require"
11
)
12
13
func TestContextCancelNucleiEngine(t *testing.T) {
14
// create nuclei engine with options
15
ctx, cancel := context.WithCancel(context.Background())
16
ne, err := nuclei.NewNucleiEngineCtx(ctx,
17
nuclei.WithTemplateFilters(nuclei.TemplateFilters{Tags: []string{"oast"}}),
18
nuclei.EnableStatsWithOpts(nuclei.StatsOptions{MetricServerPort: 0}),
19
)
20
require.NoError(t, err, "could not create nuclei engine")
21
22
go func() {
23
time.Sleep(time.Second * 2)
24
cancel()
25
log.Println("Test: context cancelled")
26
}()
27
28
// load targets and optionally probe non http/https targets
29
ne.LoadTargets([]string{"http://honey.scanme.sh"}, false)
30
// when callback is nil it nuclei will print JSON output to stdout
31
err = ne.ExecuteWithCallback(nil)
32
if err != nil {
33
// we expect a context cancellation error
34
require.ErrorIs(t, err, context.Canceled, "was expecting context cancellation error")
35
}
36
defer ne.Close()
37
}
38
39