-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
89 lines (69 loc) · 1.78 KB
/
api_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package tests
import (
"bytes"
"io"
"math/rand"
"net/http"
"os"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
const (
fileSize = 5 * 1024 * 1024
defaultEndpoint = "http://localhost:8080/bucket/test"
contentType = "application/x-binary"
)
func TestAPI(t *testing.T) {
suite.Run(t, &APISuite{})
}
type APISuite struct {
suite.Suite
endpoint string
data []byte
}
func (s *APISuite) SetupSuite() {
s.endpoint = os.Getenv("API_ENDPOINT")
if s.endpoint == "" {
s.endpoint = defaultEndpoint
}
s.generateData()
}
func (s *APISuite) TestAPI() {
s.upload()
s.download()
}
func (s *APISuite) upload() {
startTime := time.Now()
s.T().Logf("uploading file: %d bytes", len(s.data))
req, err := http.NewRequest(http.MethodPut, s.endpoint, bytes.NewBuffer(s.data))
s.Require().NoError(err)
req.Header.Set("Content-Type", contentType)
resp, err := http.DefaultClient.Do(req)
s.Require().NoError(err)
s.T().Logf("upload complete for %s", time.Since(startTime))
s.Require().Equal(http.StatusOK, resp.StatusCode)
}
func (s *APISuite) download() {
startTime := time.Now()
s.T().Logf("downloading file: %d bytes", len(s.data))
req, err := http.NewRequest(http.MethodGet, s.endpoint, http.NoBody)
s.Require().NoError(err)
resp, err := http.DefaultClient.Do(req)
s.Require().NoError(err)
defer func() {
s.Assert().NoError(resp.Body.Close())
}()
s.Assert().Equal(http.StatusOK, resp.StatusCode)
s.Assert().Equal(contentType, resp.Header.Get("Content-Type"))
body, err := io.ReadAll(resp.Body)
s.T().Logf("download complete for %s", time.Since(startTime))
s.Require().NoError(err)
s.Assert().Equal(s.data, body)
}
func (s *APISuite) generateData() {
s.data = make([]byte, fileSize)
for i := 0; i < fileSize; i++ {
s.data[i] = byte('a' + rand.Intn(26))
}
}