-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_client_test.go
167 lines (141 loc) · 5.22 KB
/
rest_client_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package zulip_test
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/wakumaku/go-zulip"
)
func TestRestClientDoRequest(t *testing.T) {
email := "email@test"
apiKey := "apikey"
auth := email + ":" + apiKey
base64Auth := base64.StdEncoding.EncodeToString([]byte(auth))
expectedUserAgent := "test/user-agent"
expectedContentType := "application/x-www-form-urlencoded"
expectedAccept := "application/json"
expectedMethod := http.MethodPost
expectedEndpoint := "/endpoint"
expectedBody := `key=value` // url.Values.Encode() format
expectedHeaders := http.Header{
"Content-Type": []string{expectedContentType},
"User-Agent": []string{expectedUserAgent},
"Accept": []string{expectedAccept},
"Authorization": []string{
"Basic " + base64Auth,
},
"Content-Length": []string{fmt.Sprintf("%d", len(expectedBody))},
"Accept-Encoding": []string{"gzip"},
}
requestRecorder := struct {
headers http.Header
method string
path string
body []byte
}{
headers: make(http.Header),
body: make([]byte, 0),
}
handlerTest := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// store request data to check later
requestRecorder.method = r.Method
requestRecorder.path = r.URL.Path
requestRecorder.headers = r.Header
body, _ := io.ReadAll(r.Body)
requestRecorder.body = body
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"result": "success"}`))
})
mockServer := httptest.NewServer(handlerTest)
defer mockServer.Close()
baseURL := mockServer.URL
client, err := zulip.NewClient(zulip.Credentials(baseURL, email, apiKey),
zulip.WithCustomUserAgent(expectedUserAgent),
)
assert.NoError(t, err)
msg := map[string]any{
"key": "value", // will become url.Values.Encode() format
}
var resp zulip.APIResponseBase
err = client.DoRequest(context.TODO(), expectedMethod, expectedEndpoint, msg, &resp,
zulip.WithTimeout(10*time.Second),
)
assert.NoError(t, err)
assert.Equal(t, zulip.ResultSuccess, resp.Result())
assert.Equal(t, expectedMethod, requestRecorder.method)
assert.Equal(t, expectedEndpoint, requestRecorder.path)
assert.Equal(t, expectedHeaders, requestRecorder.headers)
assert.Equal(t, expectedBody, string(requestRecorder.body))
}
func TestRestClientDoRequestFile(t *testing.T) {
email := "email@test"
apiKey := "apikey"
auth := email + ":" + apiKey
base64Auth := base64.StdEncoding.EncodeToString([]byte(auth))
expectedUserAgent := "test/user-agent"
expectedContentType := "multipart/form-data; boundary=a106139c086e0e50b2d83cbfc544d0326e7e7e2d19ffce4ae54f01c99ade"
expectedAccept := "application/json"
expectedMethod := http.MethodPost
expectedEndpoint := "/endpoint"
expectedBody := `--939e8e6271d5c6e29e77a3d261655eb34fcf1ca62384ca607c4671c0b3a3\r\nContent-Disposition: form-data; name=\"filename\"; filename=\"file.txt\"\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nfile content\r\n--939e8e6271d5c6e29e77a3d261655eb34fcf1ca62384ca607c4671c0b3a3--\r\n`
expectedHeaders := http.Header{
"Content-Type": []string{expectedContentType},
"User-Agent": []string{expectedUserAgent},
"Accept": []string{expectedAccept},
"Authorization": []string{
"Basic " + base64Auth,
},
"Content-Length": []string{fmt.Sprintf("%d", len(expectedBody))},
"Accept-Encoding": []string{"gzip"},
}
requestRecorder := struct {
headers http.Header
method string
path string
body []byte
}{
headers: make(http.Header),
body: make([]byte, 0),
}
handlerTest := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// store request data to check later
requestRecorder.method = r.Method
requestRecorder.path = r.URL.Path
requestRecorder.headers = r.Header
body, _ := io.ReadAll(r.Body)
requestRecorder.body = body
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"result": "success"}`))
})
mockServer := httptest.NewServer(handlerTest)
defer mockServer.Close()
baseURL := mockServer.URL
client, err := zulip.NewClient(zulip.Credentials(baseURL, email, apiKey),
zulip.WithCustomUserAgent(expectedUserAgent),
)
assert.NoError(t, err)
fileName := "file.txt"
msg := bytes.NewReader([]byte("file content"))
var resp zulip.APIResponseBase
err = client.DoFileRequest(context.TODO(), expectedMethod, expectedEndpoint, fileName, msg, &resp,
zulip.WithTimeout(10*time.Second),
)
assert.NoError(t, err)
assert.Equal(t, zulip.ResultSuccess, resp.Result())
assert.Equal(t, expectedMethod, requestRecorder.method)
assert.Equal(t, expectedEndpoint, requestRecorder.path)
// We cannot check all the headers because the boundary is random (Mimetype multipart/form-data)
// assert.Equal(t, expectedHeaders, requestRecorder.headers)
// assert.Equal(t, expectedBody, string(requestRecorder.body))
// Check only the headers that are not random
assert.Equal(t, expectedHeaders.Get("User-Agent"), requestRecorder.headers.Get("User-Agent"))
assert.Equal(t, expectedHeaders.Get("Accept"), requestRecorder.headers.Get("Accept"))
assert.Equal(t, expectedHeaders.Get("Authorization"), requestRecorder.headers.Get("Authorization"))
assert.Equal(t, expectedHeaders.Get("Accept-Encoding"), requestRecorder.headers.Get("Accept-Encoding"))
}