feat: support to show the body size on page (#536)
increase the body size limit default value to be 5120 Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
This commit is contained in:
parent
71e1aafb1b
commit
296fd4084f
|
@ -42,6 +42,7 @@ import (
|
||||||
"github.com/linuxsuren/api-testing/pkg/downloader"
|
"github.com/linuxsuren/api-testing/pkg/downloader"
|
||||||
"github.com/linuxsuren/api-testing/pkg/logging"
|
"github.com/linuxsuren/api-testing/pkg/logging"
|
||||||
"github.com/linuxsuren/api-testing/pkg/mock"
|
"github.com/linuxsuren/api-testing/pkg/mock"
|
||||||
|
atestoauth "github.com/linuxsuren/api-testing/pkg/oauth"
|
||||||
template "github.com/linuxsuren/api-testing/pkg/render"
|
template "github.com/linuxsuren/api-testing/pkg/render"
|
||||||
"github.com/linuxsuren/api-testing/pkg/server"
|
"github.com/linuxsuren/api-testing/pkg/server"
|
||||||
"github.com/linuxsuren/api-testing/pkg/service"
|
"github.com/linuxsuren/api-testing/pkg/service"
|
||||||
|
@ -50,7 +51,6 @@ import (
|
||||||
"github.com/linuxsuren/api-testing/pkg/testing/remote"
|
"github.com/linuxsuren/api-testing/pkg/testing/remote"
|
||||||
"github.com/linuxsuren/api-testing/pkg/util"
|
"github.com/linuxsuren/api-testing/pkg/util"
|
||||||
fakeruntime "github.com/linuxsuren/go-fake-runtime"
|
fakeruntime "github.com/linuxsuren/go-fake-runtime"
|
||||||
atestoauth "github.com/linuxsuren/api-testing/pkg/oauth"
|
|
||||||
"github.com/linuxsuren/oauth-hub"
|
"github.com/linuxsuren/oauth-hub"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
@ -469,7 +469,7 @@ func debugHandler(mux *runtime.ServeMux, remoteServer server.RunnerServer) {
|
||||||
Name: sub,
|
Name: sub,
|
||||||
})
|
})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
w.Header().Set("Content-Type", "application/octet-stream")
|
w.Header().Set(util.ContentType, "application/octet-stream")
|
||||||
w.Write(data.Data)
|
w.Write(data.Data)
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
"name": "atest-ui",
|
"name": "atest-ui",
|
||||||
"version": "v0.0.14",
|
"version": "v0.0.14",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@vueuse/core": "^10.11.0",
|
||||||
"codemirror": "^5.65.17",
|
"codemirror": "^5.65.17",
|
||||||
"diff-match-patch": "^1.0.5",
|
"diff-match-patch": "^1.0.5",
|
||||||
"element-plus": "^2.3.7",
|
"element-plus": "^2.3.7",
|
||||||
|
|
|
@ -85,6 +85,7 @@ const parseResponseBody = (body) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
testResult.value.bodyLength = body.length
|
||||||
testResult.value.bodyObject = JSON.parse(body)
|
testResult.value.bodyObject = JSON.parse(body)
|
||||||
testResult.value.originBodyObject = JSON.parse(body)
|
testResult.value.originBodyObject = JSON.parse(body)
|
||||||
} catch {
|
} catch {
|
||||||
|
@ -103,6 +104,7 @@ const handleTestResult = (e) => {
|
||||||
if(isFilePath){
|
if(isFilePath){
|
||||||
isResponseFile.value = true
|
isResponseFile.value = true
|
||||||
} else if(e.body !== ''){
|
} else if(e.body !== ''){
|
||||||
|
testResult.value.bodyLength = e.body.length
|
||||||
testResult.value.bodyObject = JSON.parse(e.body);
|
testResult.value.bodyObject = JSON.parse(e.body);
|
||||||
testResult.value.originBodyObject = JSON.parse(e.body);
|
testResult.value.originBodyObject = JSON.parse(e.body);
|
||||||
}
|
}
|
||||||
|
@ -1210,7 +1212,9 @@ Magic.Keys(() => {
|
||||||
<el-tab-pane label="Body" name="body">
|
<el-tab-pane label="Body" name="body">
|
||||||
<div v-if="testResult.bodyObject">
|
<div v-if="testResult.bodyObject">
|
||||||
<el-input :prefix-icon="Search" @change="responseBodyFilter" v-model="responseBodyFilterText"
|
<el-input :prefix-icon="Search" @change="responseBodyFilter" v-model="responseBodyFilterText"
|
||||||
clearable placeholder="$.key" />
|
clearable placeholder="$.key">
|
||||||
|
<template #prepend v-if="testResult.bodyLength > 0">Body Size: {{testResult.bodyLength}}</template>
|
||||||
|
</el-input>
|
||||||
<JsonViewer :value="testResult.bodyObject" :expand-depth="5" copyable boxed sort />
|
<JsonViewer :value="testResult.bodyObject" :expand-depth="5" copyable boxed sort />
|
||||||
</div>
|
</div>
|
||||||
<div v-else>
|
<div v-else>
|
||||||
|
|
|
@ -31,6 +31,7 @@ export interface TestResult {
|
||||||
body: string
|
body: string
|
||||||
bodyObject: {}
|
bodyObject: {}
|
||||||
bodyText: string
|
bodyText: string
|
||||||
|
bodyLength: number
|
||||||
output: string
|
output: string
|
||||||
error: string
|
error: string
|
||||||
statusCode: number
|
statusCode: number
|
||||||
|
|
|
@ -63,7 +63,7 @@ func TestCurlGenerator(t *testing.T) {
|
||||||
Request: atest.Request{
|
Request: atest.Request{
|
||||||
API: fooForTest,
|
API: fooForTest,
|
||||||
Header: map[string]string{
|
Header: map[string]string{
|
||||||
"Content-Type": util.Plain,
|
util.ContentType: util.Plain,
|
||||||
"Connection": "keep-alive",
|
"Connection": "keep-alive",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -292,7 +292,7 @@ func (h *advanceHandler) handle(w http.ResponseWriter, req *http.Request) {
|
||||||
h.item.Response.Header = make(map[string]string)
|
h.item.Response.Header = make(map[string]string)
|
||||||
}
|
}
|
||||||
h.item.Response.Header[headerMockServer] = fmt.Sprintf("api-testing: %s", version.GetVersion())
|
h.item.Response.Header[headerMockServer] = fmt.Sprintf("api-testing: %s", version.GetVersion())
|
||||||
h.item.Response.Header["content-length"] = fmt.Sprintf("%d", len(h.item.Response.BodyData))
|
h.item.Response.Header[util.ContentLength] = fmt.Sprintf("%d", len(h.item.Response.BodyData))
|
||||||
for k, v := range h.item.Response.Header {
|
for k, v := range h.item.Response.Header {
|
||||||
hv, hErr := render.Render("mock-server-header", v, &h.item)
|
hv, hErr := render.Render("mock-server-header", v, &h.item)
|
||||||
if hErr != nil {
|
if hErr != nil {
|
||||||
|
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/linuxsuren/api-testing/pkg/util"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -137,7 +138,7 @@ func TestInMemoryServer(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||||
assert.Equal(t, "176", resp.Header.Get("Content-Length"))
|
assert.Equal(t, "176", resp.Header.Get(util.ContentLength))
|
||||||
assert.Equal(t, "mock", resp.Header.Get("Server"))
|
assert.Equal(t, "mock", resp.Header.Get("Server"))
|
||||||
assert.NotEmpty(t, resp.Header.Get(headerMockServer))
|
assert.NotEmpty(t, resp.Header.Get(headerMockServer))
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -229,6 +230,16 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ammendHeaders(headers http.Header, body []byte) {
|
||||||
|
// add content-length if it's missing
|
||||||
|
if val := headers.Get(util.ContentLength); val == "" {
|
||||||
|
headers.Add(util.ContentLength, strconv.Itoa(len(body)))
|
||||||
|
fmt.Printf("add content-length: %d\n", len(body))
|
||||||
|
} else {
|
||||||
|
fmt.Printf("content-length already exist: %s\n", val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (r *simpleTestCaseRunner) GetSuggestedAPIs(suite *testing.TestSuite, api string) (result []*testing.TestCase, err error) {
|
func (r *simpleTestCaseRunner) GetSuggestedAPIs(suite *testing.TestSuite, api string) (result []*testing.TestCase, err error) {
|
||||||
if suite.Spec.URL == "" || suite.Spec.Kind != "swagger" {
|
if suite.Spec.URL == "" || suite.Spec.Kind != "swagger" {
|
||||||
return
|
return
|
||||||
|
@ -297,6 +308,9 @@ func (r *simpleTestCaseRunner) withResponseRecord(resp *http.Response) (response
|
||||||
Header: make(map[string]string),
|
Header: make(map[string]string),
|
||||||
Body: string(responseBodyData),
|
Body: string(responseBodyData),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add some headers for convienience
|
||||||
|
ammendHeaders(resp.Header, responseBodyData)
|
||||||
for key := range resp.Header {
|
for key := range resp.Header {
|
||||||
r.simpleResponse.Header[key] = resp.Header.Get(key)
|
r.simpleResponse.Header[key] = resp.Header.Get(key)
|
||||||
}
|
}
|
||||||
|
|
|
@ -628,6 +628,12 @@ func TestGenerateRandomValue(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAmmendHeaders(t *testing.T) {
|
||||||
|
headers := http.Header{"Content-Type": []string{"application/json"}}
|
||||||
|
ammendHeaders(headers, []byte("good"))
|
||||||
|
assert.Equal(t, "4", headers.Get(util.ContentLength))
|
||||||
|
}
|
||||||
|
|
||||||
const defaultSchemaForTest = `{"properties": {
|
const defaultSchemaForTest = `{"properties": {
|
||||||
"name": {"type": "string"},
|
"name": {"type": "string"},
|
||||||
"age": {"type": "integer"}
|
"age": {"type": "integer"}
|
||||||
|
|
|
@ -27,6 +27,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/linuxsuren/api-testing/pkg/apispec"
|
"github.com/linuxsuren/api-testing/pkg/apispec"
|
||||||
|
"github.com/linuxsuren/api-testing/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type httpResultWriter struct {
|
type httpResultWriter struct {
|
||||||
|
@ -118,7 +119,7 @@ func (w *httpResultWriter) Output(result []ReportResult) (err error) {
|
||||||
} else {
|
} else {
|
||||||
contentType = "application/json"
|
contentType = "application/json"
|
||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", contentType)
|
req.Header.Set(util.ContentType, contentType)
|
||||||
|
|
||||||
var resp *http.Response
|
var resp *http.Response
|
||||||
if resp, err = http.DefaultClient.Do(req); err != nil {
|
if resp, err = http.DefaultClient.Do(req); err != nil {
|
||||||
|
|
|
@ -295,10 +295,11 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *TestResult, er
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleLargeResponseBody(resp runner.SimpleResponse, suite string, caseName string) (reply runner.SimpleResponse, err error) {
|
func handleLargeResponseBody(resp runner.SimpleResponse, suite string, caseName string) (reply runner.SimpleResponse, err error) {
|
||||||
const maxSize = 1024
|
const maxSize = 5120
|
||||||
prefix := "isFilePath-" + strings.Join([]string{suite, caseName}, "-")
|
prefix := "isFilePath-" + strings.Join([]string{suite, caseName}, "-")
|
||||||
|
|
||||||
if len(resp.Body) > maxSize {
|
if len(resp.Body) > maxSize {
|
||||||
|
remoteServerLogger.Logger.Info("response body is too large, will be saved to file", "size", len(resp.Body))
|
||||||
tmpFile, err := os.CreateTemp("", prefix+"-")
|
tmpFile, err := os.CreateTemp("", prefix+"-")
|
||||||
defer tmpFile.Close()
|
defer tmpFile.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -69,6 +69,7 @@ func GetFirstHeaderValue(header http.Header, key string) (val string) {
|
||||||
// ContentType is the HTTP header key
|
// ContentType is the HTTP header key
|
||||||
const (
|
const (
|
||||||
ContentType = "Content-Type"
|
ContentType = "Content-Type"
|
||||||
|
ContentLength = "Content-Length"
|
||||||
ContentDisposition = "Content-Disposition"
|
ContentDisposition = "Content-Disposition"
|
||||||
MultiPartFormData = "multipart/form-data"
|
MultiPartFormData = "multipart/form-data"
|
||||||
Form = "application/x-www-form-urlencoded"
|
Form = "application/x-www-form-urlencoded"
|
||||||
|
|
Loading…
Reference in New Issue