github.com/gospider007/requests@v0.0.0-20240506025355-c73d46169a23/test/request/params_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 TestSendParamsWithMap(t *testing.T) { 11 dataBody := map[string]any{ 12 "name": "test", 13 } 14 resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{ 15 Params: 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 // log.Print(jsonData) 25 if jsonData.Get("args.name").String() != "test" { 26 t.Fatal("params args error") 27 } 28 } 29 func TestSendParamsWithString(t *testing.T) { 30 dataBody := `{"name":"test"}` 31 resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{ 32 Params: dataBody, 33 }) 34 if err != nil { 35 t.Fatal(err) 36 } 37 jsonData, err := resp.Json() 38 if err != nil { 39 t.Fatal(err) 40 } 41 if jsonData.Get("args.name").String() != "test" { 42 t.Fatal("json data error") 43 } 44 } 45 func TestSendParamsWithStruct(t *testing.T) { 46 dataBody := struct{ Name string }{"test"} 47 resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{ 48 Params: dataBody, 49 }) 50 if err != nil { 51 t.Fatal(err) 52 } 53 jsonData, err := resp.Json() 54 if err != nil { 55 t.Fatal(err) 56 } 57 if jsonData.Get("args.Name").String() != "test" { 58 t.Fatal("json data error") 59 } 60 } 61 func TestSendParamsWithGson(t *testing.T) { 62 dataBody, err := gson.Decode(struct{ Name string }{"test"}) 63 if err != nil { 64 t.Fatal(err) 65 } 66 resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{ 67 Params: dataBody, 68 }) 69 if err != nil { 70 t.Fatal(err) 71 } 72 jsonData, err := resp.Json() 73 if err != nil { 74 t.Fatal(err) 75 } 76 if jsonData.Get("args.Name").String() != "test" { 77 t.Fatal("json data error") 78 } 79 } 80 81 func TestSendParamsWithEmptiyMap(t *testing.T) { 82 resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{ 83 Params: map[string]string{}, 84 }) 85 if err != nil { 86 t.Fatal(err) 87 } 88 if resp.StatusCode() != 200 { 89 t.Fatal("status code error") 90 } 91 }