add support for rulesets in JSON format for frontend use

This commit is contained in:
Kevin Pham
2023-12-05 21:03:27 -06:00
parent 52d12dd1ac
commit 0b33765b4f
5 changed files with 146 additions and 23 deletions

View File

@@ -1,20 +1,24 @@
package ruleset_v2
import (
"encoding/json"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
//"strings"
"testing"
"time"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
//"gopkg.in/yaml.v3"
)
var (
validYAML = `
rules:
validYAML = `rules:
- domains:
- example.com
- www.example.com
@@ -51,6 +55,32 @@ rules:
`
)
func TestYAMLUnmarshal(t *testing.T) {
rs, err := loadRuleFromString(validYAML)
fmt.Println(validYAML)
assert.NoError(t, err, "expected no error loading valid yml")
yml, err := rs.YAML()
assert.NoError(t, err, "expected no error marshalling ruleset")
rs2, err := loadRuleFromString(yml)
assert.NoError(t, err, "expected no error loading yaml marshalled -> unmarshalled -> marshalled ruleset")
assert.Equal(t, 1, rs2.Count(), "expected one rule to be returned after marshalled -> unmarshalled -> marshalled ruleset")
}
func TestJSONUnmarshal(t *testing.T) {
rs, err := loadRuleFromString(validYAML)
assert.NoError(t, err, "expected no error loading valid yml")
j, err := json.Marshal(&rs)
assert.NoError(t, err, "expected no error marshalling ruleset to json")
fmt.Println(string(j))
rs2, err := loadRuleFromString(string(j))
assert.NoError(t, err, "expected no error loading JSON marshalled -> unmarshalled -> marshalled ruleset")
assert.Equal(t, 1, rs2.Count(), "expected one rule to be returned after JSON marshalled -> unmarshalled -> marshalled ruleset")
}
func TestLoadRulesFromRemoteFile(t *testing.T) {
app := fiber.New()
defer app.Shutdown()
@@ -103,13 +133,18 @@ func TestLoadRulesFromRemoteFile(t *testing.T) {
}
}
func loadRuleFromString(yaml string) (Ruleset, error) {
func loadRuleFromString(yamlOrJSON string) (Ruleset, error) {
// Create a temporary file and load it
tmpFile, _ := os.CreateTemp("", "ruleset*.yaml")
var tmpFile *os.File
if strings.HasPrefix(yamlOrJSON, "{") {
tmpFile, _ = os.CreateTemp("", "ruleset*.json")
} else {
tmpFile, _ = os.CreateTemp("", "ruleset*.yaml")
}
defer os.Remove(tmpFile.Name())
tmpFile.WriteString(yaml)
tmpFile.WriteString(yamlOrJSON)
rs := Ruleset{
_rulemap: map[string]*Rule{},