Path: blob/dev/pkg/operators/extractors/extract_test.go
2070 views
package extractors12import (3"testing"45"github.com/stretchr/testify/require"6)78func TestExtractor_ExtractRegex(t *testing.T) {9e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: RegexExtractor}, Regex: []string{`([A-Z])\w+`}}10err := e.CompileExtractors()11require.Nil(t, err)1213got := e.ExtractRegex("RegEx")14require.Equal(t, map[string]struct{}{"RegEx": {}}, got)1516got = e.ExtractRegex("regex")17require.Equal(t, map[string]struct{}{}, got)18}1920func TestExtractor_ExtractKval(t *testing.T) {21e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: KValExtractor}, KVal: []string{"content_type"}}22err := e.CompileExtractors()23require.Nil(t, err)2425got := e.ExtractKval(map[string]interface{}{"content_type": "text/html"})26require.Equal(t, map[string]struct{}{"text/html": {}}, got)2728got = e.ExtractKval(map[string]interface{}{"authorization": "Basic YWxhZGRpbjpvcGVuc2VzYW1l"})29require.Equal(t, map[string]struct{}{}, got)3031}3233func TestExtractor_ExtractXPath(t *testing.T) {34body := `<!doctype html>35<html>36<head>37<title>Example Domain</title>3839<meta charset="utf-8" />40<meta http-equiv="Content-type" content="text/html; charset=utf-8" />41<meta name="viewport" content="width=device-width, initial-scale=1" />42</head>4344<body>45<div>46<h1>Example Domain</h1>47<p>This domain is for use in illustrative examples in documents. You may use this48domain in literature without prior coordination or asking for permission.</p>49<p><a href="https://www.iana.org/domains/example">More information...</a></p>50</div>51</body>52</html>53`5455e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: XPathExtractor}, XPath: []string{"/html/body/div/p[2]/a"}}56err := e.CompileExtractors()57require.Nil(t, err)5859got := e.ExtractXPath(body)60require.Equal(t, map[string]struct{}{"More information...": {}}, got)6162e = &Extractor{Type: ExtractorTypeHolder{ExtractorType: XPathExtractor}, XPath: []string{"/html/body/div/p[3]/a"}}63got = e.ExtractXPath(body)64require.Equal(t, map[string]struct{}{}, got)65}6667func TestExtractor_ExtractJSON(t *testing.T) {68e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: JSONExtractor}, JSON: []string{".[] | .id"}}69err := e.CompileExtractors()70require.Nil(t, err)7172got := e.ExtractJSON(`[{"id": 1}]`)73require.Equal(t, map[string]struct{}{"1": {}}, got)7475got = e.ExtractJSON(`{"id": 1}`)76require.Equal(t, map[string]struct{}{}, got)77}7879func TestExtractor_ExtractDSL(t *testing.T) {80e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: DSLExtractor}, DSL: []string{"to_upper(hello)"}}81err := e.CompileExtractors()82require.Nil(t, err)8384got := e.ExtractDSL(map[string]interface{}{"hello": "hi"})85require.Equal(t, map[string]struct{}{"HI": {}}, got)8687got = e.ExtractDSL(map[string]interface{}{"hi": "hello"})88require.Equal(t, map[string]struct{}{}, got)89}909192