-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasthttp_to_curl_test.go
40 lines (32 loc) · 1.14 KB
/
fasthttp_to_curl_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
package fasthttp2curl
import (
"fmt"
"testing"
"github.com/brianvoe/gofakeit/v5"
"github.com/go-http-utils/headers"
"github.com/valyala/fasthttp"
)
var encodingHeader = []byte(headers.AcceptEncoding)
var authorizationHeader = []byte(headers.Authorization)
// Test converting a fast http request to a curl command
func TestFastHttpRequest(t *testing.T) {
// mock it up
testUserAgent := []byte(gofakeit.UserAgent())
testHeader := []byte("gzip, brotli")
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(resp)
req.SetRequestURI("https://entropy.rocks/")
req.Header.SetUserAgentBytes(testUserAgent)
req.Header.SetBytesKV(encodingHeader, testHeader)
req.Header.SetBytesKV(authorizationHeader, []byte("authorizationHeader"))
res, err := GetCurlCommandFastHttp(req)
if err != nil {
t.Error(err)
}
expectedString := fmt.Sprintf("curl -X 'GET' -H 'Accept-Encoding: gzip, brotli' -H 'Authorization: authorizationHeader' -H 'User-Agent: %s' 'https://entropy.rocks/'", testUserAgent)
if res.String() != expectedString {
t.Error("expected well formed curl")
}
}