2021-10-30 15:04:16 +03:00
package output
import (
"fmt"
"strings"
"testing"
"github.com/pkg/errors"
2023-10-17 17:44:13 +05:30
"github.com/projectdiscovery/nuclei/v3/pkg/types"
2021-10-30 15:04:16 +03:00
"github.com/stretchr/testify/require"
)
func TestStandardWriterRequest ( t * testing . T ) {
t . Run ( "WithoutTraceAndError" , func ( t * testing . T ) {
2022-12-15 16:41:23 +01:00
w , err := NewStandardWriter ( & types . Options { } )
2021-10-30 15:04:16 +03:00
require . NoError ( t , err )
require . NotPanics ( t , func ( ) {
w . Request ( "path" , "input" , "http" , nil )
w . Close ( )
} )
} )
t . Run ( "TraceAndErrorWithoutError" , func ( t * testing . T ) {
traceWriter := & testWriteCloser { }
errorWriter := & testWriteCloser { }
2022-12-15 16:41:23 +01:00
w , err := NewStandardWriter ( & types . Options { } )
2021-10-30 15:04:16 +03:00
w . traceFile = traceWriter
w . errorFile = errorWriter
require . NoError ( t , err )
w . Request ( "path" , "input" , "http" , nil )
2024-05-25 00:29:04 +05:30
require . Equal ( t , ` { "template":"path","type":"http","input":"input","address":"input:","error":"none"} ` , traceWriter . String ( ) )
2021-10-30 15:04:16 +03:00
require . Empty ( t , errorWriter . String ( ) )
} )
t . Run ( "ErrorWithWrappedError" , func ( t * testing . T ) {
errorWriter := & testWriteCloser { }
2022-12-15 16:41:23 +01:00
w , err := NewStandardWriter ( & types . Options { } )
2021-10-30 15:04:16 +03:00
w . errorFile = errorWriter
require . NoError ( t , err )
w . Request (
"misconfiguration/tcpconfig.yaml" ,
"https://example.com/tcpconfig.html" ,
"http" ,
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)" ) ) ,
)
2024-12-17 11:08:42 +01:00
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 ( ) )
2021-10-30 15:04:16 +03:00
} )
}
type testWriteCloser struct {
strings . Builder
}
func ( w testWriteCloser ) Close ( ) error {
return nil
}