Skip to content

Commit 717d892

Browse files
committed
Merge remote-tracking branch 'origin/master' into dependabot/go_modules/github.com/expr-lang/expr-1.17.3
2 parents 3c90c7d + b572c78 commit 717d892

27 files changed

+1826
-1596
lines changed

cmd/mock-compose.go

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,49 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20-
"github.com/linuxsuren/api-testing/pkg/mock"
21-
"github.com/spf13/cobra"
22-
"os"
23-
"os/signal"
24-
"syscall"
20+
"github.com/linuxsuren/api-testing/pkg/mock"
21+
"github.com/spf13/cobra"
22+
"os"
23+
"os/signal"
24+
"syscall"
2525
)
2626

2727
func createMockComposeCmd() (c *cobra.Command) {
28-
c = &cobra.Command{
29-
Use: "mock-compose",
30-
Short: "Mock multiple servers",
31-
Args: cobra.ExactArgs(1),
32-
RunE: func(cmd *cobra.Command, args []string) (err error) {
33-
reader := mock.NewLocalFileReader(args[0])
28+
c = &cobra.Command{
29+
Use: "mock-compose",
30+
Short: "Mock multiple servers",
31+
Args: cobra.ExactArgs(1),
32+
RunE: func(cmd *cobra.Command, args []string) (err error) {
33+
reader := mock.NewLocalFileReader(args[0])
3434

35-
var server *mock.Server
36-
if server, err = reader.Parse(); err != nil {
37-
return
38-
}
35+
var server *mock.Server
36+
if server, err = reader.Parse(); err != nil {
37+
return
38+
}
3939

40-
var subServers []mock.DynamicServer
41-
for _, proxy := range server.Proxies {
42-
subProxy := &mock.Server{
43-
Proxies: []mock.Proxy{proxy},
44-
}
40+
var subServers []mock.DynamicServer
41+
for _, proxy := range server.Proxies {
42+
subProxy := &mock.Server{
43+
Proxies: []mock.Proxy{proxy},
44+
}
4545

46-
subReader := mock.NewObjectReader(subProxy)
47-
subServer := mock.NewInMemoryServer(c.Context(), proxy.Port)
48-
go subServer.Start(subReader, proxy.Prefix)
49-
subServers = append(subServers, subServer)
50-
}
46+
subReader := mock.NewObjectReader(subProxy)
47+
subServer := mock.NewInMemoryServer(c.Context(), proxy.Port)
48+
go subServer.Start(subReader, proxy.Prefix)
49+
subServers = append(subServers, subServer)
50+
}
5151

52-
clean := make(chan os.Signal, 1)
53-
signal.Notify(clean, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
54-
select {
55-
case <-c.Context().Done():
56-
case <-clean:
57-
}
58-
for _, server := range subServers {
59-
server.Stop()
60-
}
61-
return
62-
},
63-
}
64-
return
52+
clean := make(chan os.Signal, 1)
53+
signal.Notify(clean, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
54+
select {
55+
case <-c.Context().Done():
56+
case <-clean:
57+
}
58+
for _, server := range subServers {
59+
server.Stop()
60+
}
61+
return
62+
},
63+
}
64+
return
6565
}

cmd/server.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"context"
2323
"errors"
2424
"fmt"
25-
"github.com/linuxsuren/api-testing/pkg/apispec"
25+
"math"
2626
"net"
2727
"net/http"
2828
"os"
@@ -33,6 +33,8 @@ import (
3333
"syscall"
3434
"time"
3535

36+
"github.com/linuxsuren/api-testing/pkg/apispec"
37+
3638
"github.com/linuxsuren/api-testing/pkg/runner"
3739
"github.com/linuxsuren/api-testing/pkg/util/home"
3840

@@ -336,7 +338,8 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
336338
server.RegisterMockHandlerFromEndpoint(ctx, mux, gRPCServerAddr, []grpc.DialOption{grpc.WithTransportCredentials(creds)}),
337339
server.RegisterDataServerHandlerFromEndpoint(ctx, mux, gRPCServerAddr, []grpc.DialOption{grpc.WithTransportCredentials(creds)}))
338340
} else {
339-
dialOption := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
341+
dialOption := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials()),
342+
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt))}
340343
err = errors.Join(
341344
server.RegisterRunnerHandlerFromEndpoint(ctx, mux, gRPCServerAddr, dialOption),
342345
server.RegisterMockHandlerFromEndpoint(ctx, mux, gRPCServerAddr, dialOption),

console/atest-ui/package-lock.json

Lines changed: 6 additions & 6 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: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ const parseResponseBody = (body: any) => {
112112
}
113113
}
114114
115+
const currentContentType = ref('')
116+
const currentImageContent = ref('')
117+
115118
/**
116119
* Handles test result data from API response
117120
*
@@ -124,15 +127,23 @@ const parseResponseBody = (body: any) => {
124127
*/
125128
const handleTestResult = (e: any): void => {
126129
testResult.value = e;
130+
currentContentType.value = e.header.find((h: Pair) => h.key.toLowerCase() === 'content-type')?.value || '';
131+
if (currentContentType.value.startsWith('image/')) {
132+
API.DownloadResponseFile({
133+
body: e.body}, (e) => {
134+
if (e && e.data) {
135+
currentImageContent.value = `data:${currentContentType.value};base64,${e.data}`;
136+
} else {
137+
console.error('No data to display as image.');
138+
}
139+
});
140+
}
127141
128142
if (!isHistoryTestCase.value) {
129143
handleTestResultError(e)
130144
}
131-
const isFilePath = e.body.startsWith("isFilePath-")
132-
133-
if(isFilePath){
134-
isResponseFile.value = true
135-
} else if(e.body !== ''){
145+
isResponseFile.value = e.body.startsWith("isFilePath-")
146+
if(!isResponseFile.value && e.body !== ''){
136147
testResult.value.bodyLength = e.body.length
137148
try {
138149
// Try to parse as JSON, fallback to plain text if parsing fails
@@ -149,11 +160,11 @@ const handleTestResult = (e: any): void => {
149160
}
150161
}
151162
152-
Cache.SetTestCaseResponseCache(suite + '-' + name, {
153-
body: testResult.value.bodyObject,
154-
output: e.output,
155-
statusCode: testResult.value.statusCode
156-
} as TestCaseResponse)
163+
Cache.SetTestCaseResponseCache(suite + '-' + name, {
164+
body: testResult.value.bodyObject,
165+
output: e.output,
166+
statusCode: testResult.value.statusCode
167+
} as TestCaseResponse)
157168
158169
parameters.value = [];
159170
}
@@ -436,7 +447,9 @@ function downloadResponseFile(){
436447
} else {
437448
console.error('No data to download.');
438449
}
439-
})
450+
}, (e) => {
451+
UIAPI.ErrorTip(e);
452+
});
440453
}
441454
442455
function setDefaultValues(e) {
@@ -1373,7 +1386,8 @@ const renameTestCase = (name: string) => {
13731386
</div>
13741387
<div v-else>
13751388
<Codemirror v-if="!isResponseFile" v-model="testResult.bodyText"/>
1376-
<div v-if="isResponseFile" style="padding-top: 10px;">
1389+
<img v-if="currentContentType.startsWith('image')" :src="currentImageContent" style="max-width: 100%; max-height: 500px;"/>
1390+
<div v-else-if="isResponseFile" style="padding-top: 10px;">
13771391
<el-row>
13781392
<el-col :span="10">
13791393
<div>Response body is too large, please download to view.</div>

console/atest-ui/src/views/__test__/net.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ beforeEach(() => {
2828

2929
describe('net', () => {
3030
test('GetVersion', () => {
31-
fetchMock.mockResponseOnce(`{"version":"v0.0.2"}`)
31+
fetchMock.mockResponseOnce(`{"version":"v0.0.2"}`, {
32+
headers: {
33+
'Content-Type': 'application/json'
34+
}
35+
})
3236
API.GetVersion((d) => {
3337
expect(d.version).toEqual('v0.0.2')
3438
})

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async function DefaultResponseProcess(response: any): Promise<any> {
6767

6868
// For OK responses, handle content based on Content-Type
6969
const contentType = response.headers.get('Content-Type') ?? '';
70-
if (contentType.startsWith('text/plain')) {
70+
if (isTextReadableExpectJSON(contentType)) {
7171
// For text/plain, directly return the text
7272
return await response.text();
7373
}
@@ -90,6 +90,11 @@ async function DefaultResponseProcess(response: any): Promise<any> {
9090
}
9191
}
9292

93+
const isTextReadableExpectJSON = (contentType: string): boolean => {
94+
// Check if the content type is text-based
95+
return contentType.startsWith('text/') || contentType === 'application/javascript';
96+
}
97+
9398
interface AppVersion {
9499
version: string
95100
commit: string
@@ -823,16 +828,10 @@ interface ResponseFile {
823828
function DownloadResponseFile(testcase: ResponseFile,
824829
callback: (d: any) => void, errHandle?: (e: any) => void | null) {
825830
const requestOptions = {
826-
method: 'POST',
827831
headers: {
828832
'X-Store-Name': Cache.GetCurrentStore().name,
829833
'X-Auth': getToken()
830-
},
831-
body: JSON.stringify({
832-
response: {
833-
body: testcase.body,
834-
}
835-
})
834+
}
836835
}
837836
fetch(`/api/v1/downloadFile/${testcase.body}`, requestOptions)
838837
.then(DefaultResponseProcess)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface Suite {
3737

3838
export interface TestResult {
3939
body: string
40-
bodyObject: {}
40+
bodyObject: {} | null
4141
bodyText: string
4242
bodyLength: number
4343
output: string
@@ -46,7 +46,7 @@ export interface TestResult {
4646
header: Pair[]
4747

4848
// inner fields
49-
originBodyObject:{}
49+
originBodyObject:{} | null
5050
}
5151

5252
export interface Pair {

pkg/generator/curl_generator.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,56 @@ limitations under the License.
1616
package generator
1717

1818
import (
19-
"bytes"
20-
_ "embed"
21-
"net/http"
22-
"strings"
23-
"text/template"
19+
"bytes"
20+
_ "embed"
21+
"net/http"
22+
"strings"
23+
"text/template"
2424

25-
"github.com/linuxsuren/api-testing/pkg/testing"
25+
"github.com/linuxsuren/api-testing/pkg/testing"
2626
)
2727

2828
type curlGenerator struct {
2929
}
3030

3131
func NewCurlGenerator() CodeGenerator {
32-
return &curlGenerator{}
32+
return &curlGenerator{}
3333
}
3434

3535
func (g *curlGenerator) Generate(testSuite *testing.TestSuite, testcase *testing.TestCase) (result string, err error) {
36-
if testcase.Request.Method == "" {
37-
testcase.Request.Method = http.MethodGet
38-
}
36+
if testcase.Request.Method == "" {
37+
testcase.Request.Method = http.MethodGet
38+
}
3939

40-
if !strings.HasSuffix(testcase.Request.API, "?") {
41-
testcase.Request.API += "?"
42-
}
40+
if !strings.HasSuffix(testcase.Request.API, "?") {
41+
testcase.Request.API += "?"
42+
}
4343

44-
queryKeys := testcase.Request.Query.Keys()
45-
for _, k := range queryKeys {
46-
testcase.Request.API += k + "=" + testcase.Request.Query.GetValue(k) + "&"
47-
}
44+
queryKeys := testcase.Request.Query.Keys()
45+
for _, k := range queryKeys {
46+
testcase.Request.API += k + "=" + testcase.Request.Query.GetValue(k) + "&"
47+
}
4848

49-
testcase.Request.API = strings.TrimSuffix(testcase.Request.API, "&")
50-
testcase.Request.API = strings.TrimSuffix(testcase.Request.API, "?")
51-
if err = testcase.Request.Render(nil, ""); err != nil {
52-
return
53-
}
49+
testcase.Request.API = strings.TrimSuffix(testcase.Request.API, "&")
50+
testcase.Request.API = strings.TrimSuffix(testcase.Request.API, "?")
51+
if err = testcase.Request.Render(nil, ""); err != nil {
52+
return
53+
}
5454

55-
var tpl *template.Template
56-
if tpl, err = template.New("curl template").Parse(curlTemplate); err == nil {
57-
buf := new(bytes.Buffer)
58-
if err = tpl.Execute(buf, testcase); err == nil {
59-
result = strings.TrimSpace(buf.String())
55+
var tpl *template.Template
56+
if tpl, err = template.New("curl template").Parse(curlTemplate); err == nil {
57+
buf := new(bytes.Buffer)
58+
if err = tpl.Execute(buf, testcase); err == nil {
59+
result = strings.TrimSpace(buf.String())
6060

61-
result = strings.TrimSuffix(result, " \\")
62-
}
63-
}
64-
return
61+
result = strings.TrimSuffix(result, " \\")
62+
}
63+
}
64+
return
6565
}
6666

6767
func init() {
68-
RegisterCodeGenerator("curl", NewCurlGenerator())
68+
RegisterCodeGenerator("curl", NewCurlGenerator())
6969
}
7070

7171
//go:embed data/curl.tpl

0 commit comments

Comments
 (0)