Skip to content

Commit 296fd40

Browse files
authored
feat: support to show the body size on page (#536)
increase the body size limit default value to be 5120 Co-authored-by: rick <[email protected]>
1 parent 71e1aaf commit 296fd40

File tree

12 files changed

+38
-8
lines changed

12 files changed

+38
-8
lines changed

cmd/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"github.com/linuxsuren/api-testing/pkg/downloader"
4343
"github.com/linuxsuren/api-testing/pkg/logging"
4444
"github.com/linuxsuren/api-testing/pkg/mock"
45+
atestoauth "github.com/linuxsuren/api-testing/pkg/oauth"
4546
template "github.com/linuxsuren/api-testing/pkg/render"
4647
"github.com/linuxsuren/api-testing/pkg/server"
4748
"github.com/linuxsuren/api-testing/pkg/service"
@@ -50,7 +51,6 @@ import (
5051
"github.com/linuxsuren/api-testing/pkg/testing/remote"
5152
"github.com/linuxsuren/api-testing/pkg/util"
5253
fakeruntime "github.com/linuxsuren/go-fake-runtime"
53-
atestoauth "github.com/linuxsuren/api-testing/pkg/oauth"
5454
"github.com/linuxsuren/oauth-hub"
5555

5656
"github.com/prometheus/client_golang/prometheus"
@@ -469,7 +469,7 @@ func debugHandler(mux *runtime.ServeMux, remoteServer server.RunnerServer) {
469469
Name: sub,
470470
})
471471
if err == nil {
472-
w.Header().Set("Content-Type", "application/octet-stream")
472+
w.Header().Set(util.ContentType, "application/octet-stream")
473473
w.Write(data.Data)
474474
} else {
475475
w.WriteHeader(http.StatusBadRequest)

console/atest-ui/package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

console/atest-ui/src/views/TestCase.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const parseResponseBody = (body) => {
8585
}
8686
8787
try {
88+
testResult.value.bodyLength = body.length
8889
testResult.value.bodyObject = JSON.parse(body)
8990
testResult.value.originBodyObject = JSON.parse(body)
9091
} catch {
@@ -103,6 +104,7 @@ const handleTestResult = (e) => {
103104
if(isFilePath){
104105
isResponseFile.value = true
105106
} else if(e.body !== ''){
107+
testResult.value.bodyLength = e.body.length
106108
testResult.value.bodyObject = JSON.parse(e.body);
107109
testResult.value.originBodyObject = JSON.parse(e.body);
108110
}
@@ -1210,7 +1212,9 @@ Magic.Keys(() => {
12101212
<el-tab-pane label="Body" name="body">
12111213
<div v-if="testResult.bodyObject">
12121214
<el-input :prefix-icon="Search" @change="responseBodyFilter" v-model="responseBodyFilterText"
1213-
clearable placeholder="$.key" />
1215+
clearable placeholder="$.key">
1216+
<template #prepend v-if="testResult.bodyLength > 0">Body Size: {{testResult.bodyLength}}</template>
1217+
</el-input>
12141218
<JsonViewer :value="testResult.bodyObject" :expand-depth="5" copyable boxed sort />
12151219
</div>
12161220
<div v-else>

console/atest-ui/src/views/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface TestResult {
3131
body: string
3232
bodyObject: {}
3333
bodyText: string
34+
bodyLength: number
3435
output: string
3536
error: string
3637
statusCode: number

pkg/generator/curl_generator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestCurlGenerator(t *testing.T) {
6363
Request: atest.Request{
6464
API: fooForTest,
6565
Header: map[string]string{
66-
"Content-Type": util.Plain,
66+
util.ContentType: util.Plain,
6767
"Connection": "keep-alive",
6868
},
6969
},

pkg/mock/in_memory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (h *advanceHandler) handle(w http.ResponseWriter, req *http.Request) {
292292
h.item.Response.Header = make(map[string]string)
293293
}
294294
h.item.Response.Header[headerMockServer] = fmt.Sprintf("api-testing: %s", version.GetVersion())
295-
h.item.Response.Header["content-length"] = fmt.Sprintf("%d", len(h.item.Response.BodyData))
295+
h.item.Response.Header[util.ContentLength] = fmt.Sprintf("%d", len(h.item.Response.BodyData))
296296
for k, v := range h.item.Response.Header {
297297
hv, hErr := render.Render("mock-server-header", v, &h.item)
298298
if hErr != nil {

pkg/mock/in_memory_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strings"
2323
"testing"
2424

25+
"github.com/linuxsuren/api-testing/pkg/util"
2526
"github.com/stretchr/testify/assert"
2627
)
2728

@@ -137,7 +138,7 @@ func TestInMemoryServer(t *testing.T) {
137138
assert.NoError(t, err)
138139

139140
assert.Equal(t, http.StatusOK, resp.StatusCode)
140-
assert.Equal(t, "176", resp.Header.Get("Content-Length"))
141+
assert.Equal(t, "176", resp.Header.Get(util.ContentLength))
141142
assert.Equal(t, "mock", resp.Header.Get("Server"))
142143
assert.NotEmpty(t, resp.Header.Get(headerMockServer))
143144

pkg/runner/http.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io"
2424
"net/http"
25+
"strconv"
2526
"strings"
2627
"time"
2728

@@ -229,6 +230,16 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
229230
return
230231
}
231232

233+
func ammendHeaders(headers http.Header, body []byte) {
234+
// add content-length if it's missing
235+
if val := headers.Get(util.ContentLength); val == "" {
236+
headers.Add(util.ContentLength, strconv.Itoa(len(body)))
237+
fmt.Printf("add content-length: %d\n", len(body))
238+
} else {
239+
fmt.Printf("content-length already exist: %s\n", val)
240+
}
241+
}
242+
232243
func (r *simpleTestCaseRunner) GetSuggestedAPIs(suite *testing.TestSuite, api string) (result []*testing.TestCase, err error) {
233244
if suite.Spec.URL == "" || suite.Spec.Kind != "swagger" {
234245
return
@@ -297,6 +308,9 @@ func (r *simpleTestCaseRunner) withResponseRecord(resp *http.Response) (response
297308
Header: make(map[string]string),
298309
Body: string(responseBodyData),
299310
}
311+
312+
// add some headers for convienience
313+
ammendHeaders(resp.Header, responseBodyData)
300314
for key := range resp.Header {
301315
r.simpleResponse.Header[key] = resp.Header.Get(key)
302316
}

pkg/runner/http_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,12 @@ func TestGenerateRandomValue(t *testing.T) {
628628
}
629629
}
630630

631+
func TestAmmendHeaders(t *testing.T) {
632+
headers := http.Header{"Content-Type": []string{"application/json"}}
633+
ammendHeaders(headers, []byte("good"))
634+
assert.Equal(t, "4", headers.Get(util.ContentLength))
635+
}
636+
631637
const defaultSchemaForTest = `{"properties": {
632638
"name": {"type": "string"},
633639
"age": {"type": "integer"}

pkg/runner/writer_http.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"os"
2828

2929
"github.com/linuxsuren/api-testing/pkg/apispec"
30+
"github.com/linuxsuren/api-testing/pkg/util"
3031
)
3132

3233
type httpResultWriter struct {
@@ -118,7 +119,7 @@ func (w *httpResultWriter) Output(result []ReportResult) (err error) {
118119
} else {
119120
contentType = "application/json"
120121
}
121-
req.Header.Set("Content-Type", contentType)
122+
req.Header.Set(util.ContentType, contentType)
122123

123124
var resp *http.Response
124125
if resp, err = http.DefaultClient.Do(req); err != nil {

0 commit comments

Comments
 (0)