test: add more e2e test cases (#294)

Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
This commit is contained in:
Rick 2023-11-28 17:36:37 +08:00 committed by GitHub
parent 3650f231d2
commit 2f83deddec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 131 additions and 19 deletions

View File

@ -364,7 +364,9 @@ func debugHandler(mux *runtime.ServeMux) {
}
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("Content-Transfer-Encoding", "binary")
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")
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 {
data = []byte(fmt.Sprintf("failed to read atest: %v", err))
data = []byte(fmt.Sprintf("failed to read %q: %v", name, err))
}
} else {
data = []byte("not found atest")
data = []byte(fmt.Sprintf("not found %q", name))
}
w.Write(data)
}

View File

@ -177,7 +177,7 @@ func TestFrontEndHandlerWithLocation(t *testing.T) {
resp := newFakeResponseWriter()
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) {
@ -193,7 +193,7 @@ func TestFrontEndHandlerWithLocation(t *testing.T) {
resp := newFakeResponseWriter()
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())
})
}

View File

@ -216,3 +216,73 @@ items:
"suite": "{{.param.gRPCSuiteName}}",
"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

View File

@ -32,6 +32,7 @@ import (
"log"
"os"
"path"
"path/filepath"
"time"
"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"
opt = &gitOptions{
cache: path.Join(os.TempDir(), store.Name),
cache: filepath.Join(os.TempDir(), store.Name),
targetPath: store.Properties["targetpath"],
name: store.Properties["name"],
email: store.Properties["email"],

View File

@ -170,13 +170,6 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
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 {
return
}
@ -193,11 +186,23 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
}
respType := util.GetFirstHeaderValue(resp.Header, util.ContentType)
if output, err = verifyResponseBodyData(testcase.Name, testcase.Expect, respType, responseBodyData); err != nil {
return
}
err = jsonSchemaValidation(testcase.Expect.Schema, responseBodyData)
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 {
return
}
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
}
@ -328,3 +333,13 @@ func runJob(job *testing.Job, ctx interface{}, current interface{}) (err error)
}
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
}
}

View File

@ -191,7 +191,8 @@ func TestTestCase(t *testing.T) {
},
prepare: func() {
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",
@ -539,6 +540,28 @@ func TestGetSuggestedAPIs(t *testing.T) {
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": {
"name": {"type": "string"},
"age": {"type": "integer"}

View File

@ -84,5 +84,6 @@ const (
JSON = "application/json"
YAML = "application/yaml"
ZIP = "application/zip"
OctetStream = "application/octet-stream"
Plain = "text/plain"
)