219 lines
5.2 KiB
Go
219 lines
5.2 KiB
Go
package processor
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
lua "github.com/yuin/gopher-lua"
|
|
)
|
|
|
|
// Test replaceVariables function
|
|
func TestReplaceVariables(t *testing.T) {
|
|
// Setup global variables
|
|
globalVariables = map[string]interface{}{
|
|
"multiplier": 2.5,
|
|
"prefix": "TEST_",
|
|
"enabled": true,
|
|
"disabled": false,
|
|
"count": 42,
|
|
}
|
|
defer func() {
|
|
globalVariables = make(map[string]interface{})
|
|
}()
|
|
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "Replace numeric variable",
|
|
input: "v1 * $multiplier",
|
|
expected: "v1 * 2.5",
|
|
},
|
|
{
|
|
name: "Replace string variable",
|
|
input: `s1 = $prefix .. "value"`,
|
|
expected: `s1 = "TEST_" .. "value"`,
|
|
},
|
|
{
|
|
name: "Replace boolean true",
|
|
input: "enabled = $enabled",
|
|
expected: "enabled = true",
|
|
},
|
|
{
|
|
name: "Replace boolean false",
|
|
input: "disabled = $disabled",
|
|
expected: "disabled = false",
|
|
},
|
|
{
|
|
name: "Replace integer",
|
|
input: "count = $count",
|
|
expected: "count = 42",
|
|
},
|
|
{
|
|
name: "Multiple replacements",
|
|
input: "$count * $multiplier",
|
|
expected: "42 * 2.5",
|
|
},
|
|
{
|
|
name: "No variables",
|
|
input: "v1 * 2",
|
|
expected: "v1 * 2",
|
|
},
|
|
{
|
|
name: "Undefined variable",
|
|
input: "v1 * $undefined",
|
|
expected: "v1 * $undefined",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := replaceVariables(tt.input)
|
|
assert.Equal(t, tt.expected, result)
|
|
})
|
|
}
|
|
}
|
|
|
|
// Test SetVariables with all type cases
|
|
func TestSetVariablesAllTypes(t *testing.T) {
|
|
vars := map[string]interface{}{
|
|
"int_val": 42,
|
|
"int64_val": int64(100),
|
|
"float32_val": float32(3.14),
|
|
"float64_val": 2.718,
|
|
"bool_true": true,
|
|
"bool_false": false,
|
|
"string_val": "hello",
|
|
}
|
|
|
|
SetVariables(vars)
|
|
|
|
// Create Lua state to verify
|
|
L, err := NewLuaState()
|
|
assert.NoError(t, err)
|
|
defer L.Close()
|
|
|
|
// Verify int64
|
|
int64Val := L.GetGlobal("int64_val")
|
|
assert.Equal(t, lua.LTNumber, int64Val.Type())
|
|
assert.Equal(t, 100.0, float64(int64Val.(lua.LNumber)))
|
|
|
|
// Verify float32
|
|
float32Val := L.GetGlobal("float32_val")
|
|
assert.Equal(t, lua.LTNumber, float32Val.Type())
|
|
assert.InDelta(t, 3.14, float64(float32Val.(lua.LNumber)), 0.01)
|
|
|
|
// Verify bool true
|
|
boolTrue := L.GetGlobal("bool_true")
|
|
assert.Equal(t, lua.LTBool, boolTrue.Type())
|
|
assert.True(t, bool(boolTrue.(lua.LBool)))
|
|
|
|
// Verify bool false
|
|
boolFalse := L.GetGlobal("bool_false")
|
|
assert.Equal(t, lua.LTBool, boolFalse.Type())
|
|
assert.False(t, bool(boolFalse.(lua.LBool)))
|
|
|
|
// Verify string
|
|
stringVal := L.GetGlobal("string_val")
|
|
assert.Equal(t, lua.LTString, stringVal.Type())
|
|
assert.Equal(t, "hello", string(stringVal.(lua.LString)))
|
|
}
|
|
|
|
// Test HTTP fetch with test server
|
|
func TestFetchWithTestServer(t *testing.T) {
|
|
// Create test HTTP server
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// Verify request
|
|
assert.Equal(t, "GET", r.Method)
|
|
|
|
// Send response
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte(`{"status": "success"}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
// Test fetch
|
|
L := lua.NewState()
|
|
defer L.Close()
|
|
|
|
L.SetGlobal("fetch", L.NewFunction(fetch))
|
|
|
|
script := `
|
|
response = fetch("` + server.URL + `")
|
|
assert(response ~= nil, "Expected response")
|
|
assert(response.ok == true, "Expected ok to be true")
|
|
assert(response.status == 200, "Expected status 200")
|
|
assert(response.body == '{"status": "success"}', "Expected correct body")
|
|
`
|
|
|
|
err := L.DoString(script)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestFetchWithTestServerPOST(t *testing.T) {
|
|
// Create test HTTP server
|
|
receivedBody := ""
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, "POST", r.Method)
|
|
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
|
|
|
|
// Read body
|
|
buf := make([]byte, 1024)
|
|
n, _ := r.Body.Read(buf)
|
|
receivedBody = string(buf[:n])
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
w.Write([]byte(`{"created": true}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
L := lua.NewState()
|
|
defer L.Close()
|
|
|
|
L.SetGlobal("fetch", L.NewFunction(fetch))
|
|
|
|
script := `
|
|
local opts = {
|
|
method = "POST",
|
|
headers = {["Content-Type"] = "application/json"},
|
|
body = '{"test": "data"}'
|
|
}
|
|
response = fetch("` + server.URL + `", opts)
|
|
assert(response ~= nil, "Expected response")
|
|
assert(response.ok == true, "Expected ok to be true")
|
|
assert(response.status == 201, "Expected status 201")
|
|
`
|
|
|
|
err := L.DoString(script)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, `{"test": "data"}`, receivedBody)
|
|
}
|
|
|
|
func TestFetchWithTestServer404(t *testing.T) {
|
|
// Create test HTTP server that returns 404
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
w.Write([]byte(`{"error": "not found"}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
L := lua.NewState()
|
|
defer L.Close()
|
|
|
|
L.SetGlobal("fetch", L.NewFunction(fetch))
|
|
|
|
script := `
|
|
response = fetch("` + server.URL + `")
|
|
assert(response ~= nil, "Expected response")
|
|
assert(response.ok == false, "Expected ok to be false for 404")
|
|
assert(response.status == 404, "Expected status 404")
|
|
`
|
|
|
|
err := L.DoString(script)
|
|
assert.NoError(t, err)
|
|
}
|