-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add http2curl_resty * feat: add readme and license --------- Co-authored-by: kinggo <[email protected]>
- Loading branch information
1 parent
7054838
commit da8dabd
Showing
6 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package http2curl | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/go-resty/resty/v2" | ||
"io" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
func GetCurlCommandResty(req *resty.Request) (*CurlCommand, error) { | ||
if req.RawRequest.URL == nil { | ||
return nil, ErrorURINull | ||
} | ||
|
||
command := CurlCommand{} | ||
command.append("curl") | ||
schema := req.RawRequest.URL.Scheme | ||
|
||
requestURL := req.URL | ||
if schema == "" { | ||
schema = "http" | ||
if req.RawRequest.TLS != nil { | ||
schema = "https" | ||
} | ||
requestURL = schema + "://" + req.RawRequest.Host + req.RawRequest.URL.Path | ||
} | ||
if schema == "https" { | ||
command.append("-k") | ||
} | ||
|
||
command.append("-X", bashEscape(req.Method)) | ||
|
||
if req.Body != nil { | ||
var buff bytes.Buffer | ||
_, err := buff.ReadFrom(bytes.NewReader(req.Body.([]byte))) | ||
if err != nil { | ||
return nil, fmt.Errorf("GetCurlCommandResty: buffer read from body error: %w", err) | ||
} | ||
|
||
req.Body = io.NopCloser(bytes.NewReader(buff.Bytes())) | ||
if len(buff.String()) > 0 { | ||
bodyEscaped := bashEscape(buff.String()) | ||
command.append("-d", bodyEscaped) | ||
} | ||
} | ||
var keys []string | ||
fmt.Println(req.Header) | ||
fmt.Println(req.RawRequest.Header) | ||
|
||
for k := range req.RawRequest.Header { | ||
keys = append(keys, k) | ||
} | ||
|
||
sort.Strings(keys) | ||
|
||
for _, k := range keys { | ||
command.append("-H", bashEscape(fmt.Sprintf("%s: %s", k, strings.Join(req.Header[k], " ")))) | ||
} | ||
|
||
command.append(bashEscape(requestURL)) | ||
command.append("--compressed") | ||
return &command, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package http2curl | ||
|
||
import ( | ||
"github.com/cloudwego/hertz/pkg/common/test/assert" | ||
"github.com/go-resty/resty/v2" | ||
"testing" | ||
) | ||
|
||
func TestGetCurlCommandResty(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("GET", func(t *testing.T) { | ||
var req *resty.Request | ||
client := resty.New() | ||
resp, _ := client.R(). | ||
SetQueryParams(map[string]string{ | ||
"a": "1", | ||
"b": "2", | ||
}).SetHeader("a", "2").Get("https://example.com/index") | ||
req = resp.Request | ||
|
||
c, err := GetCurlCommandResty(req) | ||
assert.DeepEqual(t, nil, err) | ||
assert.DeepEqual(t, "curl -k -X 'GET' -H 'A: 2' -H 'User-Agent: go-resty/2.12.0 (https://github.com/go-resty/resty)' 'https://example.com/index?a=1&b=2' --compressed", c.String()) | ||
}) | ||
t.Run("POST", func(t *testing.T) { | ||
var req *resty.Request | ||
client := resty.New() | ||
resp, _ := client.R(). | ||
SetHeader("Content-Type", "application/json"). | ||
SetBody([]byte(`{"a":"b"}`)). | ||
Post("https://example.com/index") | ||
req = resp.Request | ||
command, err := GetCurlCommandResty(req) | ||
assert.DeepEqual(t, nil, err) | ||
assert.DeepEqual(t, "curl -k -X 'POST' -d '{\"a\":\"b\"}' -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'User-Agent: go-resty/2.12.0 (https://github.com/go-resty/resty)' 'https://example.com/index' --compressed", command.String()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015-2023 Jeevanandam M., https://myjeeva.com <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters