You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
355 lines
7.0 KiB
Go
355 lines
7.0 KiB
Go
package gogen
|
|
|
|
import (
|
|
goformat "go/format"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
|
)
|
|
|
|
const testApiTemplate = `
|
|
info(
|
|
title: doc title
|
|
desc: >
|
|
doc description first part,
|
|
doc description second part<
|
|
version: 1.0
|
|
)
|
|
|
|
type Request struct {
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
}
|
|
|
|
type Response struct {
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
}
|
|
|
|
service A-api {
|
|
@server(
|
|
handler: GreetHandler
|
|
)
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
@server(
|
|
handler: NoResponseHandler
|
|
)
|
|
get /greet/get(Request) returns
|
|
}
|
|
`
|
|
|
|
const testMultiServiceTemplate = `
|
|
info(
|
|
title: doc title
|
|
desc: >
|
|
doc description first part,
|
|
doc description second part<
|
|
version: 1.0
|
|
)
|
|
|
|
type Request struct {
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
}
|
|
|
|
type Response struct {
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
}
|
|
|
|
service A-api {
|
|
@server(
|
|
handler: GreetHandler
|
|
)
|
|
get /greet/from/:name(Request) returns (Response)
|
|
}
|
|
|
|
service A-api {
|
|
@server(
|
|
handler: NoResponseHandler
|
|
)
|
|
get /greet/get(Request) returns
|
|
}
|
|
`
|
|
|
|
const apiNoInfo = `
|
|
type Request struct {
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
}
|
|
|
|
type Response struct {
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
}
|
|
|
|
service A-api {
|
|
@server(
|
|
handler: GreetHandler
|
|
)
|
|
get /greet/from/:name(Request) returns (Response)
|
|
}
|
|
`
|
|
|
|
const invalidApiFile = `
|
|
type Request struct {
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
}
|
|
|
|
type Response struct {
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
}
|
|
|
|
service A-api
|
|
@server(
|
|
handler: GreetHandler
|
|
)
|
|
get /greet/from/:name(Request) returns (Response)
|
|
}
|
|
`
|
|
|
|
const anonymousAnnotation = `
|
|
type Request struct {
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
}
|
|
|
|
type Response struct {
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
}
|
|
|
|
service A-api {
|
|
@handler GreetHandler
|
|
get /greet/from/:name(Request) returns (Response)
|
|
}
|
|
`
|
|
|
|
const apiHasMiddleware = `
|
|
type Request struct {
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
}
|
|
|
|
type Response struct {
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
}
|
|
|
|
@server(
|
|
middleware: TokenValidate
|
|
)
|
|
service A-api {
|
|
@handler GreetHandler
|
|
get /greet/from/:name(Request) returns (Response)
|
|
}
|
|
`
|
|
|
|
const apiJwt = `
|
|
type Request struct {
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
}
|
|
|
|
type Response struct {
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
}
|
|
|
|
@server(
|
|
jwt: Auth
|
|
)
|
|
service A-api {
|
|
@handler GreetHandler
|
|
get /greet/from/:name(Request) returns (Response)
|
|
}
|
|
`
|
|
|
|
const apiJwtWithMiddleware = `
|
|
type Request struct {
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
|
}
|
|
|
|
type Response struct {
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
}
|
|
|
|
@server(
|
|
jwt: Auth
|
|
middleware: TokenValidate
|
|
)
|
|
service A-api {
|
|
@handler GreetHandler
|
|
get /greet/from/:name(Request) returns (Response)
|
|
}
|
|
`
|
|
|
|
const apiHasNoRequest = `
|
|
service A-api {
|
|
@handler GreetHandler
|
|
post /greet/ping ()
|
|
}
|
|
`
|
|
|
|
func TestParser(t *testing.T) {
|
|
filename := "greet.api"
|
|
err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
|
|
assert.Nil(t, err)
|
|
defer os.Remove(filename)
|
|
|
|
parser, err := parser.NewParser(filename)
|
|
assert.Nil(t, err)
|
|
|
|
api, err := parser.Parse()
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, len(api.Types), 2)
|
|
assert.Equal(t, len(api.Service.Routes), 2)
|
|
|
|
assert.Equal(t, api.Service.Routes[0].Path, "/greet/from/:name")
|
|
assert.Equal(t, api.Service.Routes[1].Path, "/greet/get")
|
|
|
|
assert.Equal(t, api.Service.Routes[1].RequestType.Name, "Request")
|
|
assert.Equal(t, api.Service.Routes[1].ResponseType.Name, "")
|
|
|
|
validate(t, filename)
|
|
}
|
|
|
|
func TestMultiService(t *testing.T) {
|
|
filename := "greet.api"
|
|
err := ioutil.WriteFile(filename, []byte(testMultiServiceTemplate), os.ModePerm)
|
|
assert.Nil(t, err)
|
|
defer os.Remove(filename)
|
|
|
|
parser, err := parser.NewParser(filename)
|
|
assert.Nil(t, err)
|
|
|
|
api, err := parser.Parse()
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, len(api.Service.Routes), 2)
|
|
assert.Equal(t, len(api.Service.Groups), 2)
|
|
|
|
validate(t, filename)
|
|
}
|
|
|
|
func TestApiNoInfo(t *testing.T) {
|
|
filename := "greet.api"
|
|
err := ioutil.WriteFile(filename, []byte(apiNoInfo), os.ModePerm)
|
|
assert.Nil(t, err)
|
|
defer os.Remove(filename)
|
|
|
|
parser, err := parser.NewParser(filename)
|
|
assert.Nil(t, err)
|
|
|
|
_, err = parser.Parse()
|
|
assert.Nil(t, err)
|
|
|
|
validate(t, filename)
|
|
}
|
|
|
|
func TestInvalidApiFile(t *testing.T) {
|
|
filename := "greet.api"
|
|
err := ioutil.WriteFile(filename, []byte(invalidApiFile), os.ModePerm)
|
|
assert.Nil(t, err)
|
|
defer os.Remove(filename)
|
|
|
|
parser, err := parser.NewParser(filename)
|
|
assert.Nil(t, err)
|
|
|
|
_, err = parser.Parse()
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
func TestAnonymousAnnotation(t *testing.T) {
|
|
filename := "greet.api"
|
|
err := ioutil.WriteFile(filename, []byte(anonymousAnnotation), os.ModePerm)
|
|
assert.Nil(t, err)
|
|
defer os.Remove(filename)
|
|
|
|
parser, err := parser.NewParser(filename)
|
|
assert.Nil(t, err)
|
|
|
|
api, err := parser.Parse()
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, len(api.Service.Routes), 1)
|
|
assert.Equal(t, api.Service.Routes[0].Annotations[0].Value, "GreetHandler")
|
|
|
|
validate(t, filename)
|
|
}
|
|
|
|
func TestApiHasMiddleware(t *testing.T) {
|
|
filename := "greet.api"
|
|
err := ioutil.WriteFile(filename, []byte(apiHasMiddleware), os.ModePerm)
|
|
assert.Nil(t, err)
|
|
defer os.Remove(filename)
|
|
|
|
parser, err := parser.NewParser(filename)
|
|
assert.Nil(t, err)
|
|
|
|
_, err = parser.Parse()
|
|
assert.Nil(t, err)
|
|
|
|
validate(t, filename)
|
|
}
|
|
|
|
func TestApiHasJwt(t *testing.T) {
|
|
filename := "jwt.api"
|
|
err := ioutil.WriteFile(filename, []byte(apiJwt), os.ModePerm)
|
|
assert.Nil(t, err)
|
|
defer os.Remove(filename)
|
|
|
|
parser, err := parser.NewParser(filename)
|
|
assert.Nil(t, err)
|
|
|
|
_, err = parser.Parse()
|
|
assert.Nil(t, err)
|
|
|
|
validate(t, filename)
|
|
}
|
|
|
|
func TestApiHasJwtAndMiddleware(t *testing.T) {
|
|
filename := "jwt.api"
|
|
err := ioutil.WriteFile(filename, []byte(apiJwtWithMiddleware), os.ModePerm)
|
|
assert.Nil(t, err)
|
|
defer os.Remove(filename)
|
|
|
|
parser, err := parser.NewParser(filename)
|
|
assert.Nil(t, err)
|
|
|
|
_, err = parser.Parse()
|
|
assert.Nil(t, err)
|
|
|
|
validate(t, filename)
|
|
}
|
|
|
|
func TestApiHasNoRequestBody(t *testing.T) {
|
|
filename := "greet.api"
|
|
err := ioutil.WriteFile(filename, []byte(apiHasNoRequest), os.ModePerm)
|
|
assert.Nil(t, err)
|
|
defer os.Remove(filename)
|
|
|
|
parser, err := parser.NewParser(filename)
|
|
assert.Nil(t, err)
|
|
|
|
_, err = parser.Parse()
|
|
assert.Nil(t, err)
|
|
|
|
validate(t, filename)
|
|
}
|
|
|
|
func validate(t *testing.T, api string) {
|
|
dir := "_go"
|
|
err := DoGenProject(api, dir, true)
|
|
defer os.RemoveAll(dir)
|
|
assert.Nil(t, err)
|
|
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
if strings.HasSuffix(path, ".go") {
|
|
code, err := ioutil.ReadFile(path)
|
|
assert.Nil(t, err)
|
|
assert.Nil(t, validateCode(string(code)))
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func validateCode(code string) error {
|
|
_, err := goformat.Source([]byte(code))
|
|
return err
|
|
}
|