Skip to content

Commit

Permalink
test: setup for loading large files in HTTP server
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Dec 7, 2023
1 parent 816d6bb commit 3474af8
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions pkg/server/large_requests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package server

import (
"bytes"
"compress/gzip"
"context"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestServer__LargeRequests(t *testing.T) {
timeout, _ := time.ParseDuration("30s")
handler, _ := ConfigureHandlers()

svr := &http.Server{
Addr: "0.0.0.0:15551",
Handler: handler,
ReadTimeout: timeout,
ReadHeaderTimeout: timeout,
WriteTimeout: timeout,
IdleTimeout: timeout,
}
go svr.ListenAndServe()
t.Cleanup(func() {
svr.Shutdown(context.Background())
})

// Read each file
files := []string{
filepath.Join("testdata", "10k_record.json.gz"),
filepath.Join("testdata", "25k_record.json.gz"),
filepath.Join("testdata", "50k_record.json.gz"),
}
for i := range files {
path := files[i]

t.Run("validate "+path, func(t *testing.T) {
var body bytes.Buffer
w := multipart.NewWriter(&body)
part, err := w.CreateFormFile("file", filepath.Base(path))
require.NoError(t, err)
_, err = io.Copy(part, open(t, path))
require.NoError(t, err)
require.NoError(t, w.Close()) // flush

req, err := http.NewRequest("POST", "http://localhost:15551/validator", &body)
require.NoError(t, err)
req.Header.Set("Content-Type", w.FormDataContentType())

resp, err := http.DefaultClient.Do(req)
if resp != nil && resp.StatusCode != http.StatusOK {
if resp != nil && resp.Body != nil {
bs, _ := io.ReadAll(resp.Body)
t.Logf("Response: %v", string(bs))
}
require.Equal(t, http.StatusOK, resp.StatusCode)
}
require.NoError(t, err)
})

t.Run("convert "+path, func(t *testing.T) {
var body bytes.Buffer
w := multipart.NewWriter(&body)
part, err := w.CreateFormFile("file", filepath.Base(path))
require.NoError(t, err)
_, err = io.Copy(part, open(t, path))
require.NoError(t, err)
w.WriteField("format", "metro")
require.NoError(t, w.Close()) // flush

req, err := http.NewRequest("POST", "http://localhost:15551/convert", &body)
require.NoError(t, err)
req.Header.Set("Content-Type", w.FormDataContentType())

resp, err := http.DefaultClient.Do(req)
if resp != nil && resp.StatusCode != http.StatusOK {
if resp != nil && resp.Body != nil {
bs, _ := io.ReadAll(resp.Body)
t.Logf("Response: %v", string(bs))
}
require.Equal(t, http.StatusOK, resp.StatusCode)
}
require.NoError(t, err)
})
}
}

func open(t *testing.T, path string) io.Reader {
t.Helper()

fd, err := os.Open(path)
require.NoError(t, err)
t.Cleanup(func() { fd.Close() })

r, err := gzip.NewReader(fd)
require.NoError(t, err)

return r
}
Binary file added pkg/server/testdata/10k_record.json.gz
Binary file not shown.
Binary file added pkg/server/testdata/25k_record.json.gz
Binary file not shown.
Binary file added pkg/server/testdata/50k_record.json.gz
Binary file not shown.

0 comments on commit 3474af8

Please sign in to comment.