nuclei/pkg/js/compiler/compiler_test.go
Dogan Can Bakir f080d614c3
introduce timeouts config in types.Options (#5228)
* introduce timeout variants

* update instances and add codeexectimeout

* fix test

* default to 10s

* minor

* make timeouts pluggable and rename

* remove residual code

---------

Co-authored-by: Tarun Koyalwar <tarun@projectdiscovery.io>
2024-07-15 15:57:15 +05:30

49 lines
1.1 KiB
Go

package compiler
import (
"context"
"strings"
"testing"
"time"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/gologger/levels"
"github.com/projectdiscovery/nuclei/v3/pkg/types"
)
func TestNewCompilerConsoleDebug(t *testing.T) {
gotString := ""
gologger.DefaultLogger.SetMaxLevel(levels.LevelDebug)
gologger.DefaultLogger.SetWriter(&noopWriter{
Callback: func(data []byte, level levels.Level) {
gotString = string(data)
},
})
compiler := New()
p, err := WrapScriptNCompile("console.log('hello world');", false)
if err != nil {
t.Fatal(err)
}
_, err = compiler.ExecuteWithOptions(p, NewExecuteArgs(), &ExecuteOptions{Context: context.Background(),
TimeoutVariants: &types.Timeouts{JsCompilerExecutionTimeout: time.Duration(20) * time.Second}},
)
if err != nil {
t.Fatal(err)
}
if !strings.HasSuffix(gotString, "hello world") {
t.Fatalf("console.log not working, got=%v", gotString)
}
}
type noopWriter struct {
Callback func(data []byte, level levels.Level)
}
func (n *noopWriter) Write(data []byte, level levels.Level) {
if n.Callback != nil {
n.Callback(data, level)
}
}