test: add unit tests of binary upload (#587)

* test: add unit tests of binary upload

* add more http status code desc

---------

Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
This commit is contained in:
Rick 2024-12-17 22:54:19 +08:00 committed by GitHub
parent b0d18fcdf8
commit efb2235754
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 265 additions and 220 deletions

View File

@ -84,6 +84,7 @@
"404": "404 Not Found",
"405": "405 Method Not Allowed",
"409": "409 Conflict",
"413": "Content Too Large",
"415": "415 Unsupported Media Type",
"422": "422 Unprocessable Content",
"500": "500 Internal Server Error",

View File

@ -1,5 +1,5 @@
/*
Copyright 2023 API Testing Authors.
Copyright 2023-2024 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -191,6 +191,8 @@ func (e RequestBody) Bytes() (data []byte) {
data, err = decodeBase64Body(e.Value, util.PDFBase64Prefix)
} else if strings.HasPrefix(e.Value, util.ZIPBase64Prefix) {
data, err = decodeBase64Body(e.Value, util.ZIPBase64Prefix)
} else if strings.HasPrefix(e.Value, util.BinaryBase64Prefix) {
data, err = decodeBase64Body(e.Value, util.BinaryBase64Prefix)
} else {
data = []byte(e.Value)
}

View File

@ -16,6 +16,7 @@ limitations under the License.
package testing_test
import (
"github.com/linuxsuren/api-testing/pkg/util"
"testing"
atesting "github.com/linuxsuren/api-testing/pkg/testing"
@ -85,3 +86,44 @@ func TestSortedKeysStringMap(t *testing.T) {
assert.Equal(t, "f", obj.GetVerifier("f").Value)
assert.Empty(t, obj.GetValue("not-found"))
}
func TestBodyBytes(t *testing.T) {
const defaultPlainText = "hello"
const defaultBase64Text = "aGVsbG8="
tt := []struct {
name string
rawBody string
expect []byte
}{{
name: "image base64",
rawBody: util.ImageBase64Prefix + defaultBase64Text,
expect: []byte(defaultPlainText),
}, {
name: "pdf",
rawBody: util.PDFBase64Prefix + defaultBase64Text,
expect: []byte(defaultPlainText),
}, {
name: "zip",
rawBody: util.ZIPBase64Prefix + defaultBase64Text,
expect: []byte(defaultPlainText),
}, {
name: "binary",
rawBody: util.BinaryBase64Prefix + defaultBase64Text,
expect: []byte(defaultPlainText),
}, {
name: "raw",
rawBody: defaultPlainText,
expect: []byte(defaultPlainText),
}}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
body := atesting.RequestBody{
Value: tc.rawBody,
}
data := body.Bytes()
assert.Equal(t, tc.expect, data)
assert.False(t, body.IsEmpty())
})
}
}