mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
feat: support serverless in rest (#5001)
Signed-off-by: kevin <wanjunfeng@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
67
rest/serverless_test.go
Normal file
67
rest/serverless_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/core/logx/logtest"
|
||||
)
|
||||
|
||||
func TestNewServerless(t *testing.T) {
|
||||
logtest.Discard(t)
|
||||
|
||||
const configYaml = `
|
||||
Name: foo
|
||||
Host: localhost
|
||||
Port: 0
|
||||
`
|
||||
var cnf RestConf
|
||||
assert.Nil(t, conf.LoadFromYamlBytes([]byte(configYaml), &cnf))
|
||||
|
||||
svr, err := NewServer(cnf)
|
||||
assert.NoError(t, err)
|
||||
|
||||
svr.AddRoute(Route{
|
||||
Method: http.MethodGet,
|
||||
Path: "/",
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("Hello World"))
|
||||
},
|
||||
})
|
||||
|
||||
serverless, err := NewServerless(svr)
|
||||
assert.NoError(t, err)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
serverless.Serve(w, r)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, "Hello World", w.Body.String())
|
||||
}
|
||||
|
||||
func TestNewServerlessWithError(t *testing.T) {
|
||||
logtest.Discard(t)
|
||||
|
||||
const configYaml = `
|
||||
Name: foo
|
||||
Host: localhost
|
||||
Port: 0
|
||||
`
|
||||
var cnf RestConf
|
||||
assert.Nil(t, conf.LoadFromYamlBytes([]byte(configYaml), &cnf))
|
||||
|
||||
svr, err := NewServer(cnf)
|
||||
assert.NoError(t, err)
|
||||
|
||||
svr.AddRoute(Route{
|
||||
Method: http.MethodGet,
|
||||
Path: "notstartwith/",
|
||||
Handler: nil,
|
||||
})
|
||||
|
||||
_, err = NewServerless(svr)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
Reference in New Issue
Block a user