test: add more e2e test cases (#294)
Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
This commit is contained in:
parent
3650f231d2
commit
2f83deddec
|
@ -364,7 +364,9 @@ func debugHandler(mux *runtime.ServeMux) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *serverOption) getAtestBinary(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
|
func (o *serverOption) getAtestBinary(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
|
||||||
w.Header().Set(util.ContentDisposition, "attachment; filename=atest")
|
name := util.EmptyThenDefault(r.URL.Query().Get("name"), "atest")
|
||||||
|
|
||||||
|
w.Header().Set(util.ContentDisposition, fmt.Sprintf("attachment; filename=%s", name))
|
||||||
w.Header().Set(util.ContentType, "application/octet-stream")
|
w.Header().Set(util.ContentType, "application/octet-stream")
|
||||||
w.Header().Set("Content-Transfer-Encoding", "binary")
|
w.Header().Set("Content-Transfer-Encoding", "binary")
|
||||||
w.Header().Set("Expires", "0")
|
w.Header().Set("Expires", "0")
|
||||||
|
@ -372,12 +374,12 @@ func (o *serverOption) getAtestBinary(w http.ResponseWriter, r *http.Request, pa
|
||||||
w.Header().Set("Cache-Control", "no-cache")
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
|
|
||||||
var data []byte
|
var data []byte
|
||||||
if atestPath, err := o.execer.LookPath("atest"); err == nil {
|
if atestPath, err := o.execer.LookPath(name); err == nil {
|
||||||
if data, err = os.ReadFile(atestPath); err != nil {
|
if data, err = os.ReadFile(atestPath); err != nil {
|
||||||
data = []byte(fmt.Sprintf("failed to read atest: %v", err))
|
data = []byte(fmt.Sprintf("failed to read %q: %v", name, err))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
data = []byte("not found atest")
|
data = []byte(fmt.Sprintf("not found %q", name))
|
||||||
}
|
}
|
||||||
w.Write(data)
|
w.Write(data)
|
||||||
}
|
}
|
||||||
|
|
|
@ -177,7 +177,7 @@ func TestFrontEndHandlerWithLocation(t *testing.T) {
|
||||||
resp := newFakeResponseWriter()
|
resp := newFakeResponseWriter()
|
||||||
|
|
||||||
opt.getAtestBinary(resp, req, map[string]string{})
|
opt.getAtestBinary(resp, req, map[string]string{})
|
||||||
assert.Equal(t, "not found atest", resp.GetBody().String())
|
assert.Equal(t, `not found "atest"`, resp.GetBody().String())
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("download atest, failed to read", func(t *testing.T) {
|
t.Run("download atest, failed to read", func(t *testing.T) {
|
||||||
|
@ -193,7 +193,7 @@ func TestFrontEndHandlerWithLocation(t *testing.T) {
|
||||||
resp := newFakeResponseWriter()
|
resp := newFakeResponseWriter()
|
||||||
|
|
||||||
opt.getAtestBinary(resp, req, map[string]string{})
|
opt.getAtestBinary(resp, req, map[string]string{})
|
||||||
assert.Equal(t, "failed to read atest: open : no such file or directory", resp.GetBody().String())
|
assert.Equal(t, `failed to read "atest": open : no such file or directory`, resp.GetBody().String())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -216,3 +216,73 @@ items:
|
||||||
"suite": "{{.param.gRPCSuiteName}}",
|
"suite": "{{.param.gRPCSuiteName}}",
|
||||||
"testcase": "{{.param.gRPCCaseName}}"
|
"testcase": "{{.param.gRPCCaseName}}"
|
||||||
}
|
}
|
||||||
|
- name: version
|
||||||
|
request:
|
||||||
|
api: /GetVersion
|
||||||
|
method: POST
|
||||||
|
- name: popularHeaders
|
||||||
|
request:
|
||||||
|
api: /PopularHeaders
|
||||||
|
method: POST
|
||||||
|
expect:
|
||||||
|
verify:
|
||||||
|
- any(data.data, {.key == "Content-Type"})
|
||||||
|
- any(data.data, {.key == "Authorization"})
|
||||||
|
- name: functionsQuery
|
||||||
|
request:
|
||||||
|
api: /FunctionsQuery
|
||||||
|
method: POST
|
||||||
|
body: |
|
||||||
|
{"name": "rand"}
|
||||||
|
expect:
|
||||||
|
verify:
|
||||||
|
- any(data.data, {.key == "randNumeric"})
|
||||||
|
- any(data.data, {.key == "randAlpha"})
|
||||||
|
- len(data.data) >= 8
|
||||||
|
- name: findUnknownFunction
|
||||||
|
request:
|
||||||
|
api: /FunctionsQuery
|
||||||
|
method: POST
|
||||||
|
body: |
|
||||||
|
{"name": "{{randAlpha 8}}"}
|
||||||
|
expect:
|
||||||
|
verify:
|
||||||
|
- len(data.data) == 0
|
||||||
|
- name: listCodeGenerator
|
||||||
|
request:
|
||||||
|
api: /ListCodeGenerator
|
||||||
|
method: POST
|
||||||
|
expect:
|
||||||
|
verify:
|
||||||
|
- any(data.data, {.key == "curl"})
|
||||||
|
- len(data.data) >= 3
|
||||||
|
|
||||||
|
## Other
|
||||||
|
- name: downloadTool
|
||||||
|
request:
|
||||||
|
api: |
|
||||||
|
{{.param.server}}/get
|
||||||
|
expect:
|
||||||
|
header:
|
||||||
|
Content-Type: application/octet-stream
|
||||||
|
Content-Disposition: attachment; filename=atest
|
||||||
|
Content-Transfer-Encoding: binary
|
||||||
|
- name: downloadExtGit
|
||||||
|
request:
|
||||||
|
api: |
|
||||||
|
{{.param.server}}/get?name=atest-store-git
|
||||||
|
expect:
|
||||||
|
header:
|
||||||
|
Content-Type: application/octet-stream
|
||||||
|
Content-Disposition: attachment; filename=atest-store-git
|
||||||
|
Content-Transfer-Encoding: binary
|
||||||
|
- name: oauth
|
||||||
|
request:
|
||||||
|
api: |
|
||||||
|
{{.param.server}}/oauth2/token
|
||||||
|
expect:
|
||||||
|
statusCode: 404
|
||||||
|
- name: debugCmdLine
|
||||||
|
request:
|
||||||
|
api: |
|
||||||
|
{{.param.server}}/debug/pprof/cmdline
|
||||||
|
|
|
@ -32,6 +32,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
|
@ -280,7 +281,7 @@ func (s *gitClient) getClient(ctx context.Context) (opt *gitOptions, err error)
|
||||||
insecure := store.Properties["insecure"] == "true"
|
insecure := store.Properties["insecure"] == "true"
|
||||||
|
|
||||||
opt = &gitOptions{
|
opt = &gitOptions{
|
||||||
cache: path.Join(os.TempDir(), store.Name),
|
cache: filepath.Join(os.TempDir(), store.Name),
|
||||||
targetPath: store.Properties["targetpath"],
|
targetPath: store.Properties["targetpath"],
|
||||||
name: store.Properties["name"],
|
name: store.Properties["name"],
|
||||||
email: store.Properties["email"],
|
email: store.Properties["email"],
|
||||||
|
|
|
@ -170,13 +170,6 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
|
||||||
|
|
||||||
r.log.Debug("status code: %d\n", resp.StatusCode)
|
r.log.Debug("status code: %d\n", resp.StatusCode)
|
||||||
|
|
||||||
var responseBodyData []byte
|
|
||||||
if responseBodyData, err = r.withResponseRecord(resp); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
record.Body = string(responseBodyData)
|
|
||||||
r.log.Trace("response body: %s\n", record.Body)
|
|
||||||
|
|
||||||
if err = testcase.Expect.Render(dataContext); err != nil {
|
if err = testcase.Expect.Render(dataContext); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -193,11 +186,23 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
|
||||||
}
|
}
|
||||||
|
|
||||||
respType := util.GetFirstHeaderValue(resp.Header, util.ContentType)
|
respType := util.GetFirstHeaderValue(resp.Header, util.ContentType)
|
||||||
|
|
||||||
|
var responseBodyData []byte
|
||||||
|
if isNonBinaryContent(respType) {
|
||||||
|
if responseBodyData, err = r.withResponseRecord(resp); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
record.Body = string(responseBodyData)
|
||||||
|
r.log.Trace("response body: %s\n", record.Body)
|
||||||
|
|
||||||
if output, err = verifyResponseBodyData(testcase.Name, testcase.Expect, respType, responseBodyData); err != nil {
|
if output, err = verifyResponseBodyData(testcase.Name, testcase.Expect, respType, responseBodyData); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = jsonSchemaValidation(testcase.Expect.Schema, responseBodyData)
|
err = jsonSchemaValidation(testcase.Expect.Schema, responseBodyData)
|
||||||
|
} else {
|
||||||
|
r.log.Trace(fmt.Sprintf("skip to read the body due to it is not struct content: %q\n", respType))
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -328,3 +333,13 @@ func runJob(job *testing.Job, ctx interface{}, current interface{}) (err error)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isNonBinaryContent detect if the content belong to binary
|
||||||
|
func isNonBinaryContent(contentType string) bool {
|
||||||
|
switch contentType {
|
||||||
|
case util.JSON, util.YAML, util.Plain:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -191,7 +191,8 @@ func TestTestCase(t *testing.T) {
|
||||||
},
|
},
|
||||||
prepare: func() {
|
prepare: func() {
|
||||||
gock.New(urlLocalhost).
|
gock.New(urlLocalhost).
|
||||||
Get("/foo").Reply(http.StatusOK).BodyString("foo")
|
Get("/foo").Reply(http.StatusOK).
|
||||||
|
SetHeader(util.ContentType, util.Plain).BodyString("foo")
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
name: "not match with header",
|
name: "not match with header",
|
||||||
|
@ -539,6 +540,28 @@ func TestGetSuggestedAPIs(t *testing.T) {
|
||||||
assert.Equal(t, strings.ToUpper(method), method)
|
assert.Equal(t, strings.ToUpper(method), method)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsStructContent(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
contentType string
|
||||||
|
expectOk bool
|
||||||
|
}{{
|
||||||
|
contentType: util.JSON,
|
||||||
|
expectOk: true,
|
||||||
|
}, {
|
||||||
|
contentType: util.YAML,
|
||||||
|
expectOk: true,
|
||||||
|
}, {
|
||||||
|
contentType: util.OctetStream,
|
||||||
|
expectOk: false,
|
||||||
|
}}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.contentType, func(t *testing.T) {
|
||||||
|
ok := isNonBinaryContent(tt.contentType)
|
||||||
|
assert.Equal(t, tt.expectOk, ok)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const defaultSchemaForTest = `{"properties": {
|
const defaultSchemaForTest = `{"properties": {
|
||||||
"name": {"type": "string"},
|
"name": {"type": "string"},
|
||||||
"age": {"type": "integer"}
|
"age": {"type": "integer"}
|
||||||
|
|
|
@ -84,5 +84,6 @@ const (
|
||||||
JSON = "application/json"
|
JSON = "application/json"
|
||||||
YAML = "application/yaml"
|
YAML = "application/yaml"
|
||||||
ZIP = "application/zip"
|
ZIP = "application/zip"
|
||||||
|
OctetStream = "application/octet-stream"
|
||||||
Plain = "text/plain"
|
Plain = "text/plain"
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue