Skip to content

Commit 99c705f

Browse files
committed
support to upload pdf file
1 parent 317c4d6 commit 99c705f

File tree

14 files changed

+611
-421
lines changed

14 files changed

+611
-421
lines changed

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

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,33 @@ Magic.Keys(() => {
2828
data-intro="You can search your desired template functions.">{{ t('button.toolbox') }}</el-button>
2929
</el-affix>
3030

31-
<el-dialog v-model="dialogVisible" :title="t('title.templateQuery')" width="40%" draggable destroy-on-close>
32-
<template #footer>
33-
<el-input v-model="query" placeholder="Query after enter" v-on:keyup.enter="queryFuncs" />
34-
<span class="dialog-footer">
35-
<el-table :data="funcs" style="width: 100%">
36-
<el-table-column label="Key" width="250">
37-
<template #default="scope">
38-
<el-input v-model="scope.row.key" placeholder="Value" />
39-
</template>
40-
</el-table-column>
41-
<el-table-column label="Value">
42-
<template #default="scope">
43-
<div style="display: flex; align-items: center">
44-
<el-input v-model="scope.row.value" placeholder="Value" />
45-
</div>
46-
</template>
47-
</el-table-column>
48-
</el-table>
49-
</span>
50-
</template>
31+
<el-dialog v-model="dialogVisible" :title="t('title.templateQuery')" width="50%" draggable destroy-on-close>
32+
<el-input
33+
v-model="query" placeholder="Query after enter" v-on:keyup.enter="queryFuncs">
34+
<template #append v-if="funcs.length > 0">
35+
{{ funcs.length }}
36+
</template>
37+
</el-input>
38+
<span class="dialog-footer">
39+
<el-table :data="funcs">
40+
<el-table-column label="Name" width="250">
41+
<template #default="scope">
42+
{{ scope.row.key }}
43+
</template>
44+
</el-table-column>
45+
<el-table-column label="Function">
46+
<template #default="scope">
47+
{{ scope.row.value }}
48+
</template>
49+
</el-table-column>
50+
<el-table-column label="Usage">
51+
<template #default="scope">
52+
<div style="display: flex; align-items: center">
53+
<el-input v-model="scope.row.description" readonly />
54+
</div>
55+
</template>
56+
</el-table-column>
57+
</el-table>
58+
</span>
5159
</el-dialog>
5260
</template>

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,15 @@ function formChange() {
721721
}
722722
}
723723
724+
const filepathChange = () => {
725+
const items = testCaseWithSuite.value.data.request.filepath.split("=")
726+
if (items && items.length > 1) {
727+
testCaseWithSuite.value.data.request.form = [{
728+
key: items[0],
729+
value: items[1]
730+
} as Pair]
731+
}
732+
}
724733
const bodyType = ref(1)
725734
function bodyTypeChange(e: number) {
726735
let contentType = ""
@@ -733,14 +742,7 @@ function bodyTypeChange(e: number) {
733742
break;
734743
case 6:
735744
contentType = 'multipart/form-data'
736-
737-
const items = testCaseWithSuite.value.data.request.filepath.split("=")
738-
if (items && items.length > 1) {
739-
testCaseWithSuite.value.data.request.form = [{
740-
key: items[0],
741-
value: items[1]
742-
} as Pair]
743-
}
745+
filepathChange()
744746
break;
745747
}
746748
@@ -1025,7 +1027,7 @@ Magic.Keys(() => {
10251027
<el-row>
10261028
<el-col :span="4">Filename:</el-col>
10271029
<el-col :span="20">
1028-
<el-input v-model="testCaseWithSuite.data.request.filepath" placeholder="file=sample.txt" />
1030+
<el-input v-model="testCaseWithSuite.data.request.filepath" placeholder="file=sample.txt" @change="filepathChange" />
10291031
</el-col>
10301032
</el-row>
10311033
</div>

pkg/render/data/templateUsage.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
randImage: |
2+
{{ randImage width height }}
3+
randAscii: |
4+
{{ randAscii count }}
5+
randPdf: |
6+
{{ randPdf }}

pkg/render/image.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ import (
3030
)
3131

3232
func generateRandomImage(width, height int) (data string, err error) {
33+
// avoid unexpected image size
34+
if width < 4 {
35+
width = 4
36+
}
37+
if height < 4 {
38+
height = 4
39+
}
40+
if width > 2048 {
41+
width = 2048
42+
}
43+
if height > 2048 {
44+
height = 2048
45+
}
46+
3347
img := image.NewRGBA(image.Rect(0, 0, width, height))
3448
blockSize := int(math.Max(float64(width), float64(height)) / 4)
3549
rand.Seed(time.Now().UnixNano())

pkg/render/image_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2024 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language 24 permissions and
14+
limitations under the License.
15+
*/
16+
package render
17+
18+
import (
19+
"encoding/base64"
20+
"strings"
21+
"testing"
22+
23+
"github.com/linuxsuren/api-testing/pkg/util"
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func TestRandImage(t *testing.T) {
28+
tests := []struct {
29+
width, height int
30+
}{{
31+
width: 0,
32+
height: 10,
33+
}, {
34+
width: 10,
35+
height: -1,
36+
}, {
37+
width: 10240,
38+
height: 10240,
39+
}}
40+
for _, tt := range tests {
41+
data, err := generateRandomImage(tt.width, tt.height)
42+
assert.NoError(t, err, err)
43+
44+
imageStr := strings.TrimPrefix(string(data), util.ImageBase64Prefix)
45+
_, err = base64.StdEncoding.DecodeString(imageStr)
46+
assert.NoError(t, err, err)
47+
}
48+
}

pkg/render/pdf.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,46 @@ limitations under the License.
1515
*/
1616
package render
1717

18-
func generateRandomPdf() (data []byte, err error) {
18+
import (
19+
"bytes"
20+
"encoding/base64"
21+
"fmt"
22+
"time"
23+
24+
"github.com/flopp/go-findfont"
25+
"github.com/linuxsuren/api-testing/pkg/util"
26+
"github.com/signintech/gopdf"
27+
)
28+
29+
func generateRandomPdf() (data string, err error) {
30+
pdf := gopdf.GoPdf{}
31+
pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
32+
33+
var fontPath string
34+
if fontPath, err = findfont.Find(""); err == nil {
35+
if err = pdf.AddTTFFont("wts11", fontPath); err != nil {
36+
return
37+
}
38+
39+
if err = pdf.SetFont("wts11", "", 14); err != nil {
40+
return
41+
}
42+
} else {
43+
return
44+
}
45+
46+
pdf.AddHeader(func() {})
47+
pdf.AddFooter(func() {})
48+
pdf.AddPage()
49+
if err = pdf.Text(fmt.Sprintf("Generated by atest at %v", time.Now())); err != nil {
50+
err = fmt.Errorf("failed to generate pdf content: %v", err)
51+
return
52+
}
53+
54+
buf := new(bytes.Buffer)
55+
if _, err = pdf.WriteTo(buf); err == nil {
56+
fmt.Println("write pdf successfully")
57+
data = fmt.Sprintf("%s%s", util.PDFBase64Prefix, base64.StdEncoding.EncodeToString(buf.Bytes()))
58+
}
1959
return
2060
}

pkg/render/pdf_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2024 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language 24 permissions and
14+
limitations under the License.
15+
*/
16+
package render
17+
18+
import (
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
func TestRandPdf(t *testing.T) {
25+
data, err := generateRandomPdf()
26+
assert.NotEmpty(t, data)
27+
assert.NoError(t, err, err)
28+
}

pkg/render/template.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/md5"
2222
"crypto/rand"
2323
"crypto/sha256"
24+
_ "embed"
2425
"encoding/base64"
2526
"encoding/hex"
2627
"encoding/json"
@@ -38,6 +39,7 @@ import (
3839
"github.com/Masterminds/sprig/v3"
3940
"github.com/linuxsuren/api-testing/pkg/secret"
4041
"github.com/linuxsuren/api-testing/pkg/util"
42+
"gopkg.in/yaml.v3"
4143
)
4244

4345
var secretGetter secret.SecretGetter
@@ -97,6 +99,15 @@ func FuncMap() template.FuncMap {
9799
return funcs
98100
}
99101

102+
// FuncUsage returns the usage of target template function
103+
func FuncUsage(funcName string) (usage string) {
104+
usageMap := make(map[string]string)
105+
if err := yaml.Unmarshal(templateUsage, usageMap); err == nil {
106+
usage = usageMap[funcName]
107+
}
108+
return
109+
}
110+
100111
// RenderThenPrint renders the template then prints the result
101112
func RenderThenPrint(name, text string, ctx interface{}, w io.Writer) (err error) {
102113
var report string
@@ -251,3 +262,6 @@ func rasEncryptWithPublicKey(content, key string) (string, error) {
251262

252263
return base64.StdEncoding.EncodeToString(encryptedData), nil
253264
}
265+
266+
//go:embed data/templateUsage.yaml
267+
var templateUsage []byte

pkg/render/template_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,12 @@ func TestRasEncryptWithPublicKey(t *testing.T) {
280280
t.Fatalf("Decrypted message does not match original. Got: %s, want: %s", decryptedMessage, message)
281281
}
282282
}
283+
284+
func TestFuncUsages(t *testing.T) {
285+
funcs := []string{"randImage"}
286+
287+
for _, f := range funcs {
288+
usage := FuncUsage(f)
289+
assert.NotEmpty(t, usage)
290+
}
291+
}

pkg/server/remote_server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,9 @@ func (s *server) FunctionsQuery(ctx context.Context, in *SimpleQuery) (reply *Pa
10101010
lowerCaseName := strings.ToLower(name)
10111011
if in.Name == "" || strings.Contains(lowerCaseName, in.Name) {
10121012
reply.Data = append(reply.Data, &Pair{
1013-
Key: name,
1014-
Value: fmt.Sprintf("%v", reflect.TypeOf(fn)),
1013+
Key: name,
1014+
Value: fmt.Sprintf("%v", reflect.TypeOf(fn)),
1015+
Description: render.FuncUsage(name),
10151016
})
10161017
}
10171018
}

0 commit comments

Comments
 (0)