nix-amer/internal/cnf/nginx_test.go

255 lines
6.2 KiB
Go
Raw Permalink Normal View History

package cnf
import (
"bytes"
"testing"
)
func TestNginxGet(t *testing.T) {
tests := []struct {
raw string
key string
}{
{"key value;\n", "key"},
{"section {\n\tkey value;\n}\n", "section.key"},
{"ignore {\n\tignore xxx;\n}\nsection {\n\tkey value;\n}\n", "section.key"},
}
for i, test := range tests {
parser := new(nginx)
reader := bytes.NewBufferString(test.raw)
// try to extract value
_, err := parser.ReadFrom(reader)
if err != nil {
t.Errorf("[%d] parse error: %s", i, err)
continue
}
// extract value
value, found := parser.Get(test.key)
if !found {
t.Errorf("[%d] expected a result, got none", i)
continue
}
// check value
if value != "value" {
t.Errorf("[%d] expected 'value' got '%s'", i, value)
}
}
}
func TestNginxSetPathExists(t *testing.T) {
tests := []struct {
raw string
key string
value string
}{
{"key value;\n", "key", "newvalue"},
{"section {\n\tkey value;\n}\n", "section.key", "newvalue"},
{"ignore {\n\tignore xxx;\n}\nsection {\n\tkey value;\n}\n", "section.key", "newvalue"},
}
for i, test := range tests {
parser := new(nginx)
reader := bytes.NewBufferString(test.raw)
// try to extract value
_, err := parser.ReadFrom(reader)
if err != nil {
t.Errorf("[%d] parse error: %s", i, err)
continue
}
// update value
if !parser.Set(test.key, test.value) {
t.Errorf("[%d] cannot set '%s' to '%s'", i, test.key, test.value)
continue
}
// check new value
value, found := parser.Get(test.key)
if !found {
t.Errorf("[%d] expected a result, got none", i)
continue
}
// check value
if value != test.value {
t.Errorf("[%d] expected '%s' got '%s'", i, test.value, value)
}
}
}
func TestNginxSetCreatePath(t *testing.T) {
tests := []struct {
raw string
key string
ignore string // path to field that must be present after transformation
value string
}{
{"ignore xxx;\n", "key", "ignore", "newvalue"},
{"ignore xxx;\nsection {\n\tkey value;\n}\n", "section.key", "ignore", "newvalue"},
{"section {\n\tkey value;\n\tignore xxx;\n}\n", "section.key", "section.ignore", "newvalue"},
2018-11-15 11:04:45 +00:00
{"ignoresec {\n\tignore xxx;\n}\n\nsection {\n}\n", "section.key", "ignoresec.ignore", "newvalue"},
{"ignoresec {\n\tignore xxx;\n}\n\nsection {\n\tkey value;\n}\n", "section.key", "ignoresec.ignore", "newvalue"},
}
for i, test := range tests {
parser := new(nginx)
reader := bytes.NewBufferString(test.raw)
// try to extract value
_, err := parser.ReadFrom(reader)
if err != nil {
t.Errorf("[%d] parse error: %s", i, err)
continue
}
// update value
if !parser.Set(test.key, test.value) {
t.Errorf("[%d] cannot set '%s' to '%s'", i, test.key, test.value)
continue
}
// check new value
value, found := parser.Get(test.key)
if !found {
t.Errorf("[%d] expected a result, got none", i)
continue
}
// check value
if value != test.value {
t.Errorf("[%d] expected '%s' got '%s'", i, test.value, value)
continue
}
// check that ignore field is still there
value, found = parser.Get(test.ignore)
if !found {
t.Errorf("[%d] expected ignore field, got none", i)
continue
}
// check value
if value != "xxx" {
t.Errorf("[%d] expected ignore value to be '%s' got '%s'", i, "xxx", value)
continue
}
}
}
2018-11-15 11:04:45 +00:00
func TestNginxSetCreateEncode(t *testing.T) {
tests := []struct {
raw string
key string
value string
encoded string
}{
{"ignore xxx;\n", "key", "newvalue", "ignore\t\txxx;\nkey\t\tnewvalue;\n"},
{"ignore xxx;\nsection {\n\tkey value;\n}\n", "section.key", "newvalue", "ignore\t\txxx;\nsection {\n\tkey\t\tnewvalue;\n}\n\n"},
{"section {\n\tkey value;\n\tignore xxx;\n}\n", "section.key", "newvalue", "section {\n\tkey\t\tnewvalue;\n\tignore\t\txxx;\n}\n\n"},
{"ignoresec {\n\tignore xxx;\n}\n\nsection {\n}\n", "section.key", "newvalue", "ignoresec {\n\tignore\t\txxx;\n}\n\nsection {\n\tkey\t\tnewvalue;\n}\n\n"},
{"ignoresec {\n\tignore xxx;\n}\n\nsection {\n\tkey value;\n}\n", "section.key", "newvalue", "ignoresec {\n\tignore\t\txxx;\n}\n\nsection {\n\tkey\t\tnewvalue;\n}\n\n"},
{
"; comment 1\n\nk1 v1;\n#comment 2\nsec1 {\n}\nsec2 {\nignore xxx;\n sec3 {\nignore2 yyy;\n}\n\n}\n\n",
"key",
"newvalue",
"; comment 1\nk1\t\tv1;\n#comment 2\nsec1 {\n}\n\nsec2 {\n\tignore\t\txxx;\n\tsec3 {\n\t\tignore2\t\tyyy;\n\t}\n\n}\n\nkey\t\tnewvalue;\n"},
{
"; comment 1\n\nk1 v1;\n#comment 2\nsec1 {\n}\nsec2 {\nignore xxx;\n sec3 {\nignore2 yyy;\n}\n\n}\n\n",
"section.key",
"newvalue",
"; comment 1\nk1\t\tv1;\n#comment 2\nsec1 {\n}\n\nsec2 {\n\tignore\t\txxx;\n\tsec3 {\n\t\tignore2\t\tyyy;\n\t}\n\n}\n\nsection {\n\tkey\t\tnewvalue;\n}\n\n"},
}
for i, test := range tests {
parser := new(nginx)
r, w := bytes.NewBufferString(test.raw), new(bytes.Buffer)
// try to extract value
_, err := parser.ReadFrom(r)
if err != nil {
t.Errorf("[%d] parse error: %s", i, err)
continue
}
// update value
if !parser.Set(test.key, test.value) {
t.Errorf("[%d] cannot set '%s' to '%s'", i, test.key, test.value)
continue
}
// check new value
value, found := parser.Get(test.key)
if !found {
t.Errorf("[%d] expected a result, got none", i)
continue
}
// check value
if value != test.value {
t.Errorf("[%d] expected '%s' got '%s'", i, test.value, value)
continue
}
// writeToBuffer
_, err = parser.WriteTo(w)
if err != nil {
t.Errorf("[%d] unexpected write error <%s>", i, err)
continue
}
encoded := w.String()
// check value
if encoded != test.encoded {
t.Errorf("[%d] wrong encoded value \n-=-=-= HAVE =-=-=-\n%s\n-=-=-= WANT =-=-=-\n%s\n-=-=-=-=-=\n", i, escape(test.encoded), escape(encoded))
continue
}
// try to write
}
}
func escape(in string) string {
out := make([]rune, 0)
for _, char := range in {
if char == '\\' {
out = append(out, []rune("\\\\")...)
} else if char == '\n' {
out = append(out, []rune("\\n")...)
} else if char == '\r' {
out = append(out, []rune("\\r")...)
} else if char == '\t' {
out = append(out, []rune("\\t")...)
} else if char == '\033' {
out = append(out, []rune("\\033")...)
} else {
out = append(out, char)
}
}
return string(out)
}