-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support post body bytes and file
- Loading branch information
Showing
10 changed files
with
280 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package httpreq | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ahuigo/gohttptool/httpreq" | ||
) | ||
|
||
type MapString = map[string]string | ||
|
||
// POST: curl -X POST -d ” 'http://local/post?p=1' | ||
func TestPostParams(t *testing.T) { | ||
curl, err := httpreq.R(). | ||
SetQueryParams(map[string]string{"p": "1"}). | ||
SetReq("POST", "http://local/post"). | ||
ToCurl() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !strings.HasPrefix(curl, "curl ") || | ||
!strings.Contains(curl, "?p=1") { | ||
t.Fatal("bad curl: ", curl) | ||
} else { | ||
t.Log(curl) | ||
} | ||
} | ||
|
||
// POST: curl -X POST -H 'Content-Type: application/json' -d '{"name":"ahuigo"}' http://localhost/path | ||
func TestPostJson(t *testing.T) { | ||
curl, err := httpreq.R(). | ||
SetJson(map[string]string{"name": "ahuigo"}). | ||
SetReq("POST", "/path"). | ||
ToCurl() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !strings.HasPrefix(curl, "curl ") || | ||
!strings.Contains(curl, `'Content-Type: application/json`) || | ||
!strings.Contains(curl, `{"name":"ahuigo"}`) { | ||
t.Fatal("bad curl: ", curl) | ||
} else { | ||
t.Log(curl) | ||
} | ||
} | ||
|
||
// Post Data: curl -H 'Content-Type: application/x-www-form-urlencoded' http://local/post -d 'name=Alex' | ||
func TestPostFormUrlEncode(t *testing.T) { | ||
curl, err := httpreq.R(). | ||
SetFormData(map[string]string{"name": "Alex"}). | ||
SetReq("POST", "http://local/post"). | ||
ToCurl() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !strings.HasPrefix(curl, "curl ") || | ||
!strings.Contains(curl, `'Content-Type: application/x-www-form-urlencode`) || | ||
!strings.Contains(curl, "name=Alex") { | ||
t.Fatal("bad curl: ", curl) | ||
} else { | ||
t.Log(curl) | ||
} | ||
|
||
} | ||
|
||
// POST FormData: multipart/form-data; boundary=.... | ||
// curl http://local/post -F 'name=Alex' | ||
func TestPostFormMultipart(t *testing.T) { | ||
curl, err := httpreq.R(). | ||
SetIsMultiPart(true). | ||
SetFormData(map[string]string{"name": "Alex"}). | ||
SetReq("POST", "http://local/post"). | ||
ToCurl() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !strings.HasPrefix(curl, "curl ") || | ||
!strings.Contains(curl, `'Content-Type: multipart/form-data`) || | ||
!strings.Contains(curl, `name="name"`) { | ||
t.Fatal("bad curl: ", curl) | ||
} else { | ||
println(curl) | ||
} | ||
} | ||
|
||
// POST: curl -X POST -H 'Content-Type: text/plain' -d 'hello!' http://local/post | ||
func TestPostPlainData(t *testing.T) { | ||
curl, err := httpreq.R(). | ||
SetContentType(httpreq.ContentTypePlain). | ||
SetBody([]byte("hello!")). | ||
SetReq("POST", "http://local/post"). | ||
ToCurl() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !strings.HasPrefix(curl, "curl ") || | ||
!strings.Contains(curl, `'Content-Type: text/plain`) || | ||
!strings.Contains(curl, `-d 'hello!'`) { | ||
t.Fatal("bad curl: ", curl) | ||
} else { | ||
println(curl) | ||
} | ||
} |
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,2 @@ | ||
|
||
I'm a.txt |
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,11 @@ | ||
package httpreq | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func getTestDataPath(filename string) string { | ||
pwd, _ := os.Getwd() | ||
return filepath.Join(pwd, "./testdata", filename) | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module github.com/ahuigo/gohttptool | ||
|
||
go 1.22.1 | ||
go 1.21.1 | ||
|
||
require github.com/pkg/errors v0.9.1 |
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
Oops, something went wrong.