diff --git a/cmd/server.go b/cmd/server.go index f5f7442..b3277f6 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -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) } diff --git a/cmd/server_test.go b/cmd/server_test.go index a99d404..e25a68b 100644 --- a/cmd/server_test.go +++ b/cmd/server_test.go @@ -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()) }) } diff --git a/e2e/test-suite-common.yaml b/e2e/test-suite-common.yaml index 719e1a2..cb1a65d 100644 --- a/e2e/test-suite-common.yaml +++ b/e2e/test-suite-common.yaml @@ -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 diff --git a/extensions/store-git/pkg/server.go b/extensions/store-git/pkg/server.go index 6941e67..f0c7169 100644 --- a/extensions/store-git/pkg/server.go +++ b/extensions/store-git/pkg/server.go @@ -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"], diff --git a/pkg/runner/http.go b/pkg/runner/http.go index b657209..34afad8 100644 --- a/pkg/runner/http.go +++ b/pkg/runner/http.go @@ -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 + } +} diff --git a/pkg/runner/http_test.go b/pkg/runner/http_test.go index d214a9e..52ef577 100644 --- a/pkg/runner/http_test.go +++ b/pkg/runner/http_test.go @@ -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"} diff --git a/pkg/util/default.go b/pkg/util/default.go index 528309d..c68ad5a 100644 --- a/pkg/util/default.go +++ b/pkg/util/default.go @@ -84,5 +84,6 @@ const ( JSON = "application/json" YAML = "application/yaml" ZIP = "application/zip" + OctetStream = "application/octet-stream" Plain = "text/plain" )