Skip to content

Commit

Permalink
feat: support to show the body size on page (#536)
Browse files Browse the repository at this point in the history
increase the body size limit default value to be 5120

Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Sep 25, 2024
1 parent 71e1aaf commit 296fd40
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions console/atest-ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion console/atest-ui/src/views/TestCase.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions console/atest-ui/src/views/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface TestResult {
body: string
bodyObject: {}
bodyText: string
bodyLength: number
output: string
error: string
statusCode: number
Expand Down
2 changes: 1 addition & 1 deletion pkg/generator/curl_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/mock/in_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/mock/in_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"testing"

"github.com/linuxsuren/api-testing/pkg/util"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -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))

Expand Down
14 changes: 14 additions & 0 deletions pkg/runner/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/runner/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
3 changes: 2 additions & 1 deletion pkg/runner/writer_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"os"

"github.com/linuxsuren/api-testing/pkg/apispec"
"github.com/linuxsuren/api-testing/pkg/util"
)

type httpResultWriter struct {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/server/remote_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/util/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 296fd40

Please sign in to comment.