mirror of
https://github.com/gospider007/requests.git
synced 2025-12-24 13:57:52 +08:00
sync
This commit is contained in:
@@ -1,105 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/gson"
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func TestSendDataWithMap(t *testing.T) {
|
||||
dataBody := map[string]any{
|
||||
"name": "test",
|
||||
}
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Data: dataBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("headers.Content-Type").String() != "application/x-www-form-urlencoded" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
if jsonData.Get("form.name").String() != "test" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendDataWithString(t *testing.T) {
|
||||
dataBody := `{"name":"test"}`
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Data: dataBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("headers.Content-Type").String() != "application/x-www-form-urlencoded" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
if jsonData.Get("form.name").String() != "test" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendDataWithStruct(t *testing.T) {
|
||||
dataBody := struct{ Name string }{"test"}
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Data: dataBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("headers.Content-Type").String() != "application/x-www-form-urlencoded" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
if jsonData.Get("form.Name").String() != "test" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendDataWithGson(t *testing.T) {
|
||||
dataBody, err := gson.Decode(struct{ Name string }{"test"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Data: dataBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("headers.Content-Type").String() != "application/x-www-form-urlencoded" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
if jsonData.Get("form.Name").String() != "test" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendDataWithEmptiyMap(t *testing.T) {
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Data: map[string]string{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("headers.Content-Type").String() != "application/x-www-form-urlencoded" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func TestSendFileWithReader(t *testing.T) {
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Form: map[string]any{
|
||||
"file": requests.File{
|
||||
Content: bytes.NewBuffer([]byte("test")), //support: io.Reader, string, []byte
|
||||
FileName: "test.txt",
|
||||
ContentType: "text/plain",
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasPrefix(jsonData.Get("headers.Content-Type").String(), "multipart/form-data") {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
if jsonData.Get("files.file").String() != "test" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/gson"
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func TestSendFormWithMap(t *testing.T) {
|
||||
dataBody := map[string]any{
|
||||
"name": "test",
|
||||
}
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Form: dataBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasPrefix(jsonData.Get("headers.Content-Type").String(), "multipart/form-data") {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
if jsonData.Get("form.name").String() != "test" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendFormWithString(t *testing.T) {
|
||||
dataBody := `{"name":"test"}`
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Form: dataBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasPrefix(jsonData.Get("headers.Content-Type").String(), "multipart/form-data") {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
if jsonData.Get("form.name").String() != "test" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendFormWithStruct(t *testing.T) {
|
||||
dataBody := struct{ Name string }{"test"}
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Form: dataBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasPrefix(jsonData.Get("headers.Content-Type").String(), "multipart/form-data") {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
if jsonData.Get("form.Name").String() != "test" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendFormWithGson(t *testing.T) {
|
||||
dataBody, err := gson.Decode(struct{ Name string }{"test"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Form: dataBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasPrefix(jsonData.Get("headers.Content-Type").String(), "multipart/form-data") {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
if jsonData.Get("form.Name").String() != "test" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendFormWithOrderMap(t *testing.T) {
|
||||
orderMap := requests.NewOrderMap()
|
||||
orderMap.Set("name", "test")
|
||||
orderMap.Set("age", 11)
|
||||
orderMap.Set("sex", "boy")
|
||||
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Form: orderMap,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasPrefix(jsonData.Get("headers.Content-Type").String(), "multipart/form-data") {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
// log.Print(jsonData)
|
||||
if jsonData.Get("form.name").String() != "test" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendFileWithEmptiyMap(t *testing.T) {
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Form: map[string]string{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
t.Fatal("status code error")
|
||||
}
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/gson"
|
||||
"github.com/gospider007/requests"
|
||||
"github.com/gospider007/tools"
|
||||
)
|
||||
|
||||
func TestSendJsonWithMap(t *testing.T) {
|
||||
jsonBody := map[string]any{
|
||||
"name": "test",
|
||||
}
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Json: jsonBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("headers.Content-Type").String() != "application/json" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
bodyJson, err := gson.Decode(jsonBody)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if bodyJson.String() != jsonData.Get("data").String() {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendJsonWithString(t *testing.T) {
|
||||
jsonBody := `{"name":"test"}`
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Json: jsonBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("headers.Content-Type").String() != "application/json" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
if jsonBody != jsonData.Get("data").String() {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendJsonWithStruct(t *testing.T) {
|
||||
jsonBody := struct{ Name string }{"test"}
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Json: jsonBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("headers.Content-Type").String() != "application/json" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
bodyJson, err := gson.Decode(jsonBody)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if bodyJson.String() != jsonData.Get("data").String() {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendJsonWithGson(t *testing.T) {
|
||||
bodyJson, err := gson.Decode(struct{ Name string }{"test"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Json: bodyJson,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("headers.Content-Type").String() != "application/json" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
if bodyJson.String() != jsonData.Get("data").String() {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendJsonWithOrder(t *testing.T) {
|
||||
orderMap := requests.NewOrderMap()
|
||||
orderMap.Set("age", "1")
|
||||
orderMap.Set("age4", "4")
|
||||
orderMap.Set("Name", "test")
|
||||
orderMap.Set("age2", "2")
|
||||
orderMap.Set("age3", []string{"22", "121"})
|
||||
|
||||
bodyJson, err := gson.Encode(orderMap)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Json: orderMap,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("headers.Content-Type").String() != "application/json" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
if tools.BytesToString(bodyJson) != jsonData.Get("data").String() {
|
||||
log.Print(jsonData.Get("data").String())
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendJsonWithEmptiyMap(t *testing.T) {
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Form: map[string]string{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
t.Fatal("status code error")
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func TestLocalAddr(t *testing.T) {
|
||||
resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
ClientOption: requests.ClientOption{
|
||||
DialOption: requests.DialOption{
|
||||
LocalAddr: &net.TCPAddr{ //set dns server
|
||||
IP: net.ParseIP("192.168.1.239"),
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
t.Fatal("http status code is not 200")
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func TestMethodGet(t *testing.T) {
|
||||
resp, err := requests.Get(nil, "https://httpbin.org/anything")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("method").String() != http.MethodGet {
|
||||
t.Fatal("method error")
|
||||
}
|
||||
}
|
||||
func TestMethodPost(t *testing.T) {
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("method").String() != http.MethodPost {
|
||||
t.Fatal("method error")
|
||||
}
|
||||
}
|
||||
func TestMethodPost2(t *testing.T) {
|
||||
resp, err := requests.Request(nil, "post", "https://httpbin.org/anything")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("method").String() != http.MethodPost {
|
||||
t.Fatal("method error")
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/gson"
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func TestSendParamsWithMap(t *testing.T) {
|
||||
dataBody := map[string]any{
|
||||
"name": "test",
|
||||
}
|
||||
resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Params: dataBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// log.Print(jsonData)
|
||||
if jsonData.Get("args.name").String() != "test" {
|
||||
t.Fatal("params args error")
|
||||
}
|
||||
}
|
||||
func TestSendParamsWithString(t *testing.T) {
|
||||
dataBody := `{"name":"test"}`
|
||||
resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Params: dataBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("args.name").String() != "test" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendParamsWithStruct(t *testing.T) {
|
||||
dataBody := struct{ Name string }{"test"}
|
||||
resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Params: dataBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("args.Name").String() != "test" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendParamsWithGson(t *testing.T) {
|
||||
dataBody, err := gson.Decode(struct{ Name string }{"test"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Params: dataBody,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("args.Name").String() != "test" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendParamsWithEmptiyMap(t *testing.T) {
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Params: map[string]string{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
t.Fatal("status code error")
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func TestStream(t *testing.T) {
|
||||
resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Stream: true,
|
||||
ClientOption: requests.ClientOption{
|
||||
Logger: func(l requests.Log) {
|
||||
log.Print(l)
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.IsStream() {
|
||||
// con, err := io.ReadAll(resp.Body())
|
||||
// if err != nil {
|
||||
// t.Fatal(err)
|
||||
// }
|
||||
// resp.ReadBody()
|
||||
// bBody := bytes.NewBuffer(nil)
|
||||
// io.Copy(bBody, resp.Body())
|
||||
|
||||
// t.Log(string(con))
|
||||
// t.Log(resp.Text())
|
||||
time.Sleep(2 * time.Second)
|
||||
resp.CloseBody()
|
||||
time.Sleep(2 * time.Second)
|
||||
if resp.StatusCode() != 200 {
|
||||
t.Fatal("resp.StatusCode()!= 200")
|
||||
}
|
||||
} else {
|
||||
t.Fatal("resp.IsStream() is false")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user