-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server_test.go
123 lines (86 loc) · 2.11 KB
/
http_server_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Test utils for HTTP server
package main
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
)
func GetTestServer() (*httptest.Server, error) {
err := InitializeTestVault()
if err != nil {
return nil, err
}
return httptest.NewServer(apiRouter), nil
}
func DoTestRequest(server *httptest.Server, method string, path string, body []byte, session string) (statusCode int, bodyResponse []byte, e error) {
client := server.Client()
pathSpl := strings.Split(path, "?")
actualPath := path
query := ""
if len(pathSpl) == 2 {
actualPath = pathSpl[0]
query = pathSpl[1]
}
apiURL, err := url.JoinPath(server.URL, actualPath)
if query != "" {
apiURL = apiURL + "?" + query
}
if err != nil {
return 0, nil, err
}
var bodyReader io.Reader = nil
if body != nil {
bodyReader = bytes.NewReader(body)
}
req, err := http.NewRequest(method, apiURL, bodyReader)
if err != nil {
return 0, nil, err
}
req.Header.Set("x-session-token", session)
resp, err := client.Do(req)
if err != nil {
return 0, nil, err
}
defer resp.Body.Close()
bodyData, err := io.ReadAll(resp.Body)
if err != nil {
return 0, nil, err
}
return resp.StatusCode, bodyData, nil
}
func DoTestRangeRequest(server *httptest.Server, session string, method string, path string, rangeHeader string) (statusCode int, head http.Header, bodyResponse []byte, e error) {
client := server.Client()
pathSpl := strings.Split(path, "?")
actualPath := path
query := ""
if len(pathSpl) == 2 {
actualPath = pathSpl[0]
query = pathSpl[1]
}
apiURL, err := url.JoinPath(server.URL, actualPath)
if query != "" {
apiURL = apiURL + "?" + query
}
if err != nil {
return 0, nil, nil, err
}
req, err := http.NewRequest(method, apiURL, nil)
if err != nil {
return 0, nil, nil, err
}
req.Header.Set("x-session-token", session)
req.Header.Set("Range", rangeHeader)
resp, err := client.Do(req)
if err != nil {
return 0, nil, nil, err
}
defer resp.Body.Close()
bodyData, err := io.ReadAll(resp.Body)
if err != nil {
return 0, nil, nil, err
}
return resp.StatusCode, resp.Header, bodyData, nil
}