imjoey commented on a change in pull request #1289:
URL: https://github.com/apache/apisix-dashboard/pull/1289#discussion_r557340298



##########
File path: api/internal/handler/route/route_test.go
##########
@@ -1349,3 +1349,146 @@ func Test_Route_With_Script(t *testing.T) {
        _, err = handler.BatchDelete(ctx)
        assert.Nil(t, err)
 }
+
+func Test_Route_With_Script_Luacode(t *testing.T) {
+       // init
+       err := storage.InitETCDClient(conf.ETCDConfig)
+       assert.Nil(t, err)
+       err = store.InitStores()
+       assert.Nil(t, err)
+
+       handler := &Handler{
+               routeStore:    store.GetStore(store.HubKeyRoute),
+               svcStore:      store.GetStore(store.HubKeyService),
+               upstreamStore: store.GetStore(store.HubKeyUpstream),
+               scriptStore:   store.GetStore(store.HubKeyScript),
+       }
+       assert.NotNil(t, handler)
+
+       // create with script of valid lua syntax
+       ctx := droplet.NewContext()
+       route := &entity.Route{}
+       reqBody := `{
+               "id": "1",
+               "uri": "/index.html",
+               "upstream": {
+                       "type": "roundrobin",
+                       "nodes": [{
+                               "host": "www.a.com",
+                               "port": 80,
+                               "weight": 1
+                       }]
+               },
+               "script": "local _M = {} \n function _M.access(api_ctx) \n 
ngx.log(ngx.INFO,\"hit access phase\") \n end \nreturn _M"
+       }`
+       err = json.Unmarshal([]byte(reqBody), route)
+       assert.Nil(t, err)
+       ctx.SetInput(route)
+       _, err = handler.Create(ctx)
+       assert.Nil(t, err)
+
+       // sleep
+       time.Sleep(time.Duration(20) * time.Millisecond)
+
+       // get
+       input := &GetInput{}
+       input.ID = "1"
+       ctx.SetInput(input)
+       ret, err := handler.Get(ctx)
+       stored := ret.(*entity.Route)
+       assert.Nil(t, err)
+       assert.Equal(t, stored.ID, route.ID)
+       assert.Equal(t, "local _M = {} \n function _M.access(api_ctx) \n 
ngx.log(ngx.INFO,\"hit access phase\") \n end \nreturn _M", stored.Script)
+
+       // update via empty script
+       route2 := &UpdateInput{}
+       route2.ID = "1"
+       reqBody = `{
+               "id": "1",
+               "uri": "/index.html",
+               "enable_websocket": true,
+               "upstream": {
+                       "type": "roundrobin",
+                       "nodes": [{
+                               "host": "www.a.com",
+                               "port": 80,
+                               "weight": 1
+                       }]
+               }
+       }`
+
+       err = json.Unmarshal([]byte(reqBody), route2)
+       assert.Nil(t, err)
+       ctx.SetInput(route2)
+       _, err = handler.Update(ctx)
+       assert.Nil(t, err)
+
+       //sleep
+       time.Sleep(time.Duration(100) * time.Millisecond)
+
+       //get, script should be nil
+       input = &GetInput{}
+       input.ID = "1"
+       ctx.SetInput(input)
+       ret, err = handler.Get(ctx)
+       stored = ret.(*entity.Route)
+       assert.Nil(t, err)
+       assert.Equal(t, stored.ID, route.ID)
+       assert.Nil(t, stored.Script)
+
+       // 2nd update via invalid script
+       input3 := &UpdateInput{}
+       input3.ID = "1"
+       reqBody = `{
+               "id": "1",
+               "uri": "/index.html",
+               "enable_websocket": true,
+               "upstream": {
+                       "type": "roundrobin",
+                       "nodes": [{
+                               "host": "www.a.com",
+                               "port": 80,
+                               "weight": 1
+                       }]
+               },
+               "script": "local _M = {} \n function _M.access(api_ctx) \n 
ngx.log(ngx.INFO,\"hit access phase\")"
+       }`
+
+       err = json.Unmarshal([]byte(reqBody), input3)
+       assert.Nil(t, err)
+       ctx.SetInput(input3)
+       _, err = handler.Update(ctx)
+       // err should NOT be nil
+       assert.NotNil(t, err)
+
+       // delete test data
+       inputDel := &BatchDelete{}
+       reqBody = `{"ids": "1"}`
+       err = json.Unmarshal([]byte(reqBody), inputDel)
+       assert.Nil(t, err)
+       ctx.SetInput(inputDel)
+       _, err = handler.BatchDelete(ctx)
+       assert.Nil(t, err)
+
+       // 2nd create with script of invalid lua syntax
+       ctx = droplet.NewContext()
+       route = &entity.Route{}
+       reqBody = `{
+               "id": "1",
+               "uri": "/index.html",
+               "upstream": {
+                       "type": "roundrobin",
+                       "nodes": [{
+                               "host": "www.a.com",
+                               "port": 80,
+                               "weight": 1
+                       }]
+               },
+               "script": "local _M = {} \n function _M.access(api_ctx) \n 
ngx.log(ngx.INFO,\"hit access phase\")"
+       }`
+       err = json.Unmarshal([]byte(reqBody), route)
+       assert.Nil(t, err)
+       ctx.SetInput(route)
+       _, err = handler.Create(ctx)
+       assert.NotNil(t, err)

Review comment:
       @nic-chen thanks for advice. PR renewed.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to