2021-10-30 15:04:16 +03:00
package output
import (
"fmt"
"strings"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
func TestStandardWriterRequest ( t * testing . T ) {
t . Run ( "WithoutTraceAndError" , func ( t * testing . T ) {
2021-11-22 17:53:25 +05:30
w , err := NewStandardWriter ( false , false , false , false , false , false , "" , "" , "" )
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 { }
2021-11-22 17:53:25 +05:30
w , err := NewStandardWriter ( false , false , false , false , false , false , "" , "" , "" )
2021-10-30 15:04:16 +03:00
w . traceFile = traceWriter
w . errorFile = errorWriter
require . NoError ( t , err )
w . Request ( "path" , "input" , "http" , nil )
require . Equal ( t , ` { "template":"path","input":"input","error":"none","type":"http"} ` , traceWriter . String ( ) )
require . Empty ( t , errorWriter . String ( ) )
} )
t . Run ( "ErrorWithWrappedError" , func ( t * testing . T ) {
errorWriter := & testWriteCloser { }
2021-11-22 17:53:25 +05:30
w , err := NewStandardWriter ( false , false , false , false , false , false , "" , "" , "" )
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)" ) ) ,
)
require . Equal ( t , ` { "template":"misconfiguration/tcpconfig.yaml","input":"https://example.com/tcpconfig.html","error":"context deadline exceeded (Client.Timeout exceeded while awaiting headers)","type":"http"} ` , errorWriter . String ( ) )
} )
}
type testWriteCloser struct {
strings . Builder
}
func ( w testWriteCloser ) Close ( ) error {
return nil
}