Initial cloud-services repo - gateway service + pkg modules
This commit is contained in:
171
pkg/utils/multipart_parser_test.go
Normal file
171
pkg/utils/multipart_parser_test.go
Normal file
@@ -0,0 +1,171 @@
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"fiskerinc.com/modules/utils"
|
||||
)
|
||||
|
||||
func TestMultipartParser(t *testing.T) {
|
||||
err := execMultipartParser()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMutipartParser(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
for i := 0; i < b.N; i++ {
|
||||
execMultipartParser()
|
||||
}
|
||||
}
|
||||
|
||||
func execMultipartParser() error {
|
||||
|
||||
r := getRequestPOSTFormFile()
|
||||
|
||||
mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !strings.HasPrefix(mediaType, "multipart/") {
|
||||
return errors.New("requires multipart")
|
||||
}
|
||||
|
||||
if len(params["boundary"]) > 70 {
|
||||
return errors.New("boundary too long")
|
||||
}
|
||||
|
||||
info, err := utils.FindFilePart(r, params["boundary"], "file", nil)
|
||||
if info.Part == nil {
|
||||
return errors.New("file not found")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
encryptor := func(input []byte) []byte {
|
||||
return input
|
||||
}
|
||||
pReader, pWriter := io.Pipe()
|
||||
defer pReader.Close()
|
||||
defer info.Part.Close()
|
||||
|
||||
go utils.ChunkFilePart(pWriter, info, encryptor)
|
||||
err = func() error {
|
||||
_, err := ioutil.ReadAll(pReader)
|
||||
return err
|
||||
}()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getRequestPOSTFormFile() *http.Request {
|
||||
postData :=
|
||||
`--xxx
|
||||
Content-Disposition: form-data; name="field1"
|
||||
|
||||
value1
|
||||
--xxx
|
||||
Content-Disposition: form-data; name="file"; filename="test.txt"
|
||||
Content-Type: text/plain
|
||||
|
||||
We're leavin' together
|
||||
But still it's farewell
|
||||
And maybe we'll come back
|
||||
To Earth, who can tell?
|
||||
I guess there is no one to blame
|
||||
We're leaving ground (leaving ground)
|
||||
Will things ever be the same again?
|
||||
It's the final countdown
|
||||
The final countdown
|
||||
|
||||
We're leavin' together
|
||||
But still it's farewell
|
||||
And maybe we'll come back
|
||||
To Earth, who can tell?
|
||||
I guess there is no one to blame
|
||||
We're leaving ground (leaving ground)
|
||||
Will things ever be the same again?
|
||||
It's the final countdown
|
||||
The final countdown
|
||||
|
||||
We're leavin' together
|
||||
But still it's farewell
|
||||
And maybe we'll come back
|
||||
To Earth, who can tell?
|
||||
I guess there is no one to blame
|
||||
We're leaving ground (leaving ground)
|
||||
Will things ever be the same again?
|
||||
It's the final countdown
|
||||
The final countdown
|
||||
|
||||
We're leavin' together
|
||||
But still it's farewell
|
||||
And maybe we'll come back
|
||||
To Earth, who can tell?
|
||||
I guess there is no one to blame
|
||||
We're leaving ground (leaving ground)
|
||||
Will things ever be the same again?
|
||||
It's the final countdown
|
||||
The final countdown
|
||||
|
||||
We're leavin' together
|
||||
But still it's farewell
|
||||
And maybe we'll come back
|
||||
To Earth, who can tell?
|
||||
I guess there is no one to blame
|
||||
We're leaving ground (leaving ground)
|
||||
Will things ever be the same again?
|
||||
It's the final countdown
|
||||
The final countdown
|
||||
|
||||
We're leavin' together
|
||||
But still it's farewell
|
||||
And maybe we'll come back
|
||||
To Earth, who can tell?
|
||||
I guess there is no one to blame
|
||||
We're leaving ground (leaving ground)
|
||||
Will things ever be the same again?
|
||||
It's the final countdown
|
||||
The final countdown
|
||||
|
||||
We're leavin' together
|
||||
But still it's farewell
|
||||
And maybe we'll come back
|
||||
To Earth, who can tell?
|
||||
I guess there is no one to blame
|
||||
We're leaving ground (leaving ground)
|
||||
Will things ever be the same again?
|
||||
It's the final countdown
|
||||
The final countdown
|
||||
|
||||
We're leavin' together
|
||||
But still it's farewell
|
||||
And maybe we'll come back
|
||||
To Earth, who can tell?
|
||||
I guess there is no one to blame
|
||||
We're leaving ground (leaving ground)
|
||||
Will things ever be the same again?
|
||||
It's the final countdown
|
||||
The final countdown
|
||||
|
||||
--xxx--
|
||||
`
|
||||
request, err := http.NewRequest(http.MethodPost, "/", ioutil.NopCloser(strings.NewReader(postData)))
|
||||
if err != nil {
|
||||
fmt.Print(err)
|
||||
}
|
||||
request.Header.Add("Content-Type", "multipart/form-data; boundary=xxx")
|
||||
return request
|
||||
}
|
||||
Reference in New Issue
Block a user