Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/output/output_test.go
2070 views
1
package output
2
3
import (
4
"fmt"
5
"strings"
6
"testing"
7
8
"github.com/pkg/errors"
9
"github.com/projectdiscovery/nuclei/v3/pkg/types"
10
"github.com/stretchr/testify/require"
11
)
12
13
func TestStandardWriterRequest(t *testing.T) {
14
t.Run("WithoutTraceAndError", func(t *testing.T) {
15
w, err := NewStandardWriter(&types.Options{})
16
require.NoError(t, err)
17
require.NotPanics(t, func() {
18
w.Request("path", "input", "http", nil)
19
w.Close()
20
})
21
})
22
23
t.Run("TraceAndErrorWithoutError", func(t *testing.T) {
24
traceWriter := &testWriteCloser{}
25
errorWriter := &testWriteCloser{}
26
27
w, err := NewStandardWriter(&types.Options{})
28
w.traceFile = traceWriter
29
w.errorFile = errorWriter
30
require.NoError(t, err)
31
w.Request("path", "input", "http", nil)
32
33
require.Equal(t, `{"template":"path","type":"http","input":"input","address":"input:","error":"none"}`, traceWriter.String())
34
require.Empty(t, errorWriter.String())
35
})
36
37
t.Run("ErrorWithWrappedError", func(t *testing.T) {
38
errorWriter := &testWriteCloser{}
39
40
w, err := NewStandardWriter(&types.Options{})
41
w.errorFile = errorWriter
42
require.NoError(t, err)
43
w.Request(
44
"misconfiguration/tcpconfig.yaml",
45
"https://example.com/tcpconfig.html",
46
"http",
47
fmt.Errorf("GET https://example.com/tcpconfig.html/tcpconfig.html giving up after 2 attempts: %w", errors.New("context deadline exceeded (Client.Timeout exceeded while awaiting headers)")),
48
)
49
50
require.Equal(t, `{"template":"misconfiguration/tcpconfig.yaml","type":"http","input":"https://example.com/tcpconfig.html","address":"example.com:443","error":"cause=\"context deadline exceeded (Client.Timeout exceeded while awaiting headers)\"","kind":"unknown-error"}`, errorWriter.String())
51
})
52
}
53
54
type testWriteCloser struct {
55
strings.Builder
56
}
57
58
func (w testWriteCloser) Close() error {
59
return nil
60
}
61
62