-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
94 lines (73 loc) · 2.66 KB
/
main_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
90
91
92
93
94
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
var (
initiateUploadId string
key string = "test.webm"
)
// executeRequest, creates a new ResponseRecorder
// then executes the request by calling ServeHTTP in the router
// after which the handler writes the response to the response recorder
// which we can then inspect.
func executeRequest(req *http.Request, s *Server) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
s.Router.ServeHTTP(rr, req)
return rr
}
// checkResponseCode is a simple utility to check the response code
// of the response
func checkResponseCode(t *testing.T, expected, actual int) {
if expected != actual {
t.Errorf("Expected response code %d. Got %d\n", expected, actual)
}
}
func TestInitiateMultipartUploadHandler(t *testing.T) {
// Create a New Server Struct
s := CreateNewServer()
// Mount Handlers
s.MountHandlers()
// Create a New Request
req, _ := http.NewRequest("GET", fmt.Sprintf("/initiate?key=%s", key), nil)
// Execute Request
response := executeRequest(req, s)
// Check the response code
checkResponseCode(t, http.StatusAccepted, response.Code)
// Decode the response body
var responseBody map[string]interface{}
err := json.NewDecoder(response.Body).Decode(&responseBody)
require.NoError(t, err, "Failed to decode response body")
// Assert the presence and type of uploadId key
require.NotNil(t, responseBody["uploadId"], "Missing 'uploadId' key in response")
// Optionally, check the uploadId value if expected
uploadId, ok := responseBody["uploadId"].(string)
require.True(t, ok, "uploadId should be a string")
initiateUploadId = uploadId
}
func TestGetPresignedURLHandler(t *testing.T) {
// Create a New Server Struct
s := CreateNewServer()
// Mount Handlers
s.MountHandlers()
// Create a New Request
req, _ := http.NewRequest("GET", fmt.Sprintf("/presigned?key=%s&partNumber=1&uploadId=%s", key, initiateUploadId), nil)
// Execute Request
response := executeRequest(req, s)
// Check the response code
checkResponseCode(t, http.StatusCreated, response.Code)
// Decode the response body
var responseBody map[string]interface{}
err := json.NewDecoder(response.Body).Decode(&responseBody)
require.NoError(t, err, "Failed to decode response body")
// Assert the presence and type of uploadId key
require.NotNil(t, responseBody["uploadId"], "Missing 'uploadId' key in response")
// Optionally, check the uploadId value if expected
uploadId, ok := responseBody["uploadId"].(string)
require.True(t, ok, "uploadId should be a string")
require.Equal(t, initiateUploadId, uploadId, "Incorrect uploadId value")
}