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:
Rick 2024-09-25 13:41:11 +08:00 committed by GitHub
parent 71e1aafb1b
commit 296fd4084f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 38 additions and 8 deletions

View File

@ -42,6 +42,7 @@ import (
"github.com/linuxsuren/api-testing/pkg/downloader"
"github.com/linuxsuren/api-testing/pkg/logging"
"github.com/linuxsuren/api-testing/pkg/mock"
atestoauth "github.com/linuxsuren/api-testing/pkg/oauth"
template "github.com/linuxsuren/api-testing/pkg/render"
"github.com/linuxsuren/api-testing/pkg/server"
"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/util"
fakeruntime "github.com/linuxsuren/go-fake-runtime"
atestoauth "github.com/linuxsuren/api-testing/pkg/oauth"
"github.com/linuxsuren/oauth-hub"
"github.com/prometheus/client_golang/prometheus"
@ -469,7 +469,7 @@ func debugHandler(mux *runtime.ServeMux, remoteServer server.RunnerServer) {
Name: sub,
})
if err == nil {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set(util.ContentType, "application/octet-stream")
w.Write(data.Data)
} else {
w.WriteHeader(http.StatusBadRequest)

View File

@ -8,6 +8,7 @@
"name": "atest-ui",
"version": "v0.0.14",
"dependencies": {
"@vueuse/core": "^10.11.0",
"codemirror": "^5.65.17",
"diff-match-patch": "^1.0.5",
"element-plus": "^2.3.7",

View File

@ -85,6 +85,7 @@ const parseResponseBody = (body) => {
}
try {
testResult.value.bodyLength = body.length
testResult.value.bodyObject = JSON.parse(body)
testResult.value.originBodyObject = JSON.parse(body)
} catch {
@ -103,6 +104,7 @@ const handleTestResult = (e) => {
if(isFilePath){
isResponseFile.value = true
} else if(e.body !== ''){
testResult.value.bodyLength = e.body.length
testResult.value.bodyObject = JSON.parse(e.body);
testResult.value.originBodyObject = JSON.parse(e.body);
}
@ -1210,7 +1212,9 @@ Magic.Keys(() => {
<el-tab-pane label="Body" name="body">
<div v-if="testResult.bodyObject">
<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 />
</div>
<div v-else>

View File

@ -31,6 +31,7 @@ export interface TestResult {
body: string
bodyObject: {}
bodyText: string
bodyLength: number
output: string
error: string
statusCode: number

View File

@ -63,7 +63,7 @@ func TestCurlGenerator(t *testing.T) {
Request: atest.Request{
API: fooForTest,
Header: map[string]string{
"Content-Type": util.Plain,
util.ContentType: util.Plain,
"Connection": "keep-alive",
},
},

View File

@ -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[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 {
hv, hErr := render.Render("mock-server-header", v, &h.item)
if hErr != nil {

View File

@ -22,6 +22,7 @@ import (
"strings"
"testing"
"github.com/linuxsuren/api-testing/pkg/util"
"github.com/stretchr/testify/assert"
)
@ -137,7 +138,7 @@ func TestInMemoryServer(t *testing.T) {
assert.NoError(t, err)
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.NotEmpty(t, resp.Header.Get(headerMockServer))

View File

@ -22,6 +22,7 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
@ -229,6 +230,16 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
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) {
if suite.Spec.URL == "" || suite.Spec.Kind != "swagger" {
return
@ -297,6 +308,9 @@ func (r *simpleTestCaseRunner) withResponseRecord(resp *http.Response) (response
Header: make(map[string]string),
Body: string(responseBodyData),
}
// add some headers for convienience
ammendHeaders(resp.Header, responseBodyData)
for key := range resp.Header {
r.simpleResponse.Header[key] = resp.Header.Get(key)
}

View File

@ -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": {
"name": {"type": "string"},
"age": {"type": "integer"}

View File

@ -27,6 +27,7 @@ import (
"os"
"github.com/linuxsuren/api-testing/pkg/apispec"
"github.com/linuxsuren/api-testing/pkg/util"
)
type httpResultWriter struct {
@ -118,7 +119,7 @@ func (w *httpResultWriter) Output(result []ReportResult) (err error) {
} else {
contentType = "application/json"
}
req.Header.Set("Content-Type", contentType)
req.Header.Set(util.ContentType, contentType)
var resp *http.Response
if resp, err = http.DefaultClient.Do(req); err != nil {

View File

@ -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) {
const maxSize = 1024
const maxSize = 5120
prefix := "isFilePath-" + strings.Join([]string{suite, caseName}, "-")
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+"-")
defer tmpFile.Close()
if err != nil {

View File

@ -69,6 +69,7 @@ func GetFirstHeaderValue(header http.Header, key string) (val string) {
// ContentType is the HTTP header key
const (
ContentType = "Content-Type"
ContentLength = "Content-Length"
ContentDisposition = "Content-Disposition"
MultiPartFormData = "multipart/form-data"
Form = "application/x-www-form-urlencoded"