Skip to content

Commit b4f0156

Browse files
authored
Merge pull request #257 from Thiht/fix/lua
fix: convert request to map[string]interface{} before passing it to the global Lua context to handle query params and headers correctly
2 parents f0fc62a + 3ca87d9 commit b4f0156

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

server/templates/lua.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"time"
66

77
"github.com/Thiht/smocker/server/types"
8-
json "github.com/layeh/gopher-json"
8+
goJson "github.com/layeh/gopher-json"
99
log "github.com/sirupsen/logrus"
1010
"github.com/yuin/gluamapper"
1111
lua "github.com/yuin/gopher-lua"
@@ -51,7 +51,12 @@ func (*luaEngine) Execute(request types.Request, script string) (*types.MockResp
5151
return nil, fmt.Errorf("failed to sandbox Lua environment: %w", err)
5252
}
5353

54-
luaState.SetGlobal("request", luar.New(luaState, request))
54+
m, err := StructToMSI(request)
55+
if err != nil {
56+
return nil, fmt.Errorf("failed to convert request as map[string]any: %w", err)
57+
}
58+
59+
luaState.SetGlobal("request", luar.New(luaState, m))
5560
if err := luaState.DoString(script); err != nil {
5661
log.WithError(err).Error("Failed to execute Lua script")
5762
return nil, fmt.Errorf("failed to execute Lua script: %w", err)
@@ -61,7 +66,7 @@ func (*luaEngine) Execute(request types.Request, script string) (*types.MockResp
6166
body := luaResult.RawGetString("body")
6267
if body.Type() == lua.LTTable {
6368
// FIXME: this should depend on the Content-Type of the luaResult
64-
b, _ := json.Encode(body)
69+
b, _ := goJson.Encode(body)
6570
luaResult.RawSetString("body", lua.LString(string(b)))
6671
}
6772

server/templates/utils.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package templates
2+
3+
import "encoding/json"
4+
5+
func StructToMSI(s interface{}) (map[string]interface{}, error) {
6+
bytes, err := json.Marshal(s)
7+
if err != nil {
8+
return nil, err
9+
}
10+
msi := map[string]interface{}{}
11+
err = json.Unmarshal(bytes, &msi)
12+
if err != nil {
13+
return nil, err
14+
}
15+
return msi, nil
16+
}

tests/data/dynamic_mock_list.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
dynamic_response:
2222
engine: lua
2323
script: >
24+
local name;
25+
if request.query_params ~= nil then
26+
name = request.query_params.name[1]
27+
end
2428
return {
2529
body = {
26-
message = "request path "..request.path
30+
message = "request path "..request.path,
31+
name = name,
2732
},
2833
headers = {
2934
["Content-Type"] = "application/json"

tests/features/use_mocks.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,11 @@ testcases:
185185

186186
- type: http
187187
method: GET
188-
url: http://localhost:8080/test2
188+
url: http://localhost:8080/test2?name=test2
189189
assertions:
190190
- result.statuscode ShouldEqual 200
191191
- result.bodyjson.message ShouldEqual "request path /test2"
192+
- result.bodyjson.name ShouldEqual test2
192193

193194
- type: http
194195
method: GET

0 commit comments

Comments
 (0)