github.com/gospider007/requests@v0.0.0-20240506025355-c73d46169a23/test/request/data_test.go (about)

     1  package main
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gospider007/gson"
     7  	"github.com/gospider007/requests"
     8  )
     9  
    10  func TestSendDataWithMap(t *testing.T) {
    11  	dataBody := map[string]any{
    12  		"name": "test",
    13  	}
    14  	resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
    15  		Data: dataBody,
    16  	})
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	jsonData, err := resp.Json()
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	if jsonData.Get("headers.Content-Type").String() != "application/x-www-form-urlencoded" {
    25  		t.Fatal("json data error")
    26  	}
    27  	if jsonData.Get("form.name").String() != "test" {
    28  		t.Fatal("json data error")
    29  	}
    30  }
    31  func TestSendDataWithString(t *testing.T) {
    32  	dataBody := `{"name":"test"}`
    33  	resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
    34  		Data: dataBody,
    35  	})
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	jsonData, err := resp.Json()
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	if jsonData.Get("headers.Content-Type").String() != "application/x-www-form-urlencoded" {
    44  		t.Fatal("json data error")
    45  	}
    46  	if jsonData.Get("form.name").String() != "test" {
    47  		t.Fatal("json data error")
    48  	}
    49  }
    50  func TestSendDataWithStruct(t *testing.T) {
    51  	dataBody := struct{ Name string }{"test"}
    52  	resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
    53  		Data: dataBody,
    54  	})
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	jsonData, err := resp.Json()
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	if jsonData.Get("headers.Content-Type").String() != "application/x-www-form-urlencoded" {
    63  		t.Fatal("json data error")
    64  	}
    65  	if jsonData.Get("form.Name").String() != "test" {
    66  		t.Fatal("json data error")
    67  	}
    68  }
    69  func TestSendDataWithGson(t *testing.T) {
    70  	dataBody, err := gson.Decode(struct{ Name string }{"test"})
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
    75  		Data: dataBody,
    76  	})
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	jsonData, err := resp.Json()
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  	if jsonData.Get("headers.Content-Type").String() != "application/x-www-form-urlencoded" {
    85  		t.Fatal("json data error")
    86  	}
    87  	if jsonData.Get("form.Name").String() != "test" {
    88  		t.Fatal("json data error")
    89  	}
    90  }
    91  func TestSendDataWithEmptiyMap(t *testing.T) {
    92  	resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
    93  		Data: map[string]string{},
    94  	})
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  	jsonData, err := resp.Json()
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	if jsonData.Get("headers.Content-Type").String() != "application/x-www-form-urlencoded" {
   103  		t.Fatal("json data error")
   104  	}
   105  }