github.com/gospider007/requests@v0.0.0-20240506025355-c73d46169a23/test/request/json_test.go (about) 1 package main 2 3 import ( 4 "log" 5 "testing" 6 7 "github.com/gospider007/gson" 8 "github.com/gospider007/requests" 9 "github.com/gospider007/tools" 10 ) 11 12 func TestSendJsonWithMap(t *testing.T) { 13 jsonBody := map[string]any{ 14 "name": "test", 15 } 16 resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{ 17 Json: jsonBody, 18 }) 19 if err != nil { 20 t.Fatal(err) 21 } 22 jsonData, err := resp.Json() 23 if err != nil { 24 t.Fatal(err) 25 } 26 if jsonData.Get("headers.Content-Type").String() != "application/json" { 27 t.Fatal("json data error") 28 } 29 bodyJson, err := gson.Decode(jsonBody) 30 if err != nil { 31 t.Fatal(err) 32 } 33 if bodyJson.String() != jsonData.Get("data").String() { 34 t.Fatal("json data error") 35 } 36 } 37 func TestSendJsonWithString(t *testing.T) { 38 jsonBody := `{"name":"test"}` 39 resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{ 40 Json: jsonBody, 41 }) 42 if err != nil { 43 t.Fatal(err) 44 } 45 jsonData, err := resp.Json() 46 if err != nil { 47 t.Fatal(err) 48 } 49 if jsonData.Get("headers.Content-Type").String() != "application/json" { 50 t.Fatal("json data error") 51 } 52 if jsonBody != jsonData.Get("data").String() { 53 t.Fatal("json data error") 54 } 55 } 56 func TestSendJsonWithStruct(t *testing.T) { 57 jsonBody := struct{ Name string }{"test"} 58 resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{ 59 Json: jsonBody, 60 }) 61 if err != nil { 62 t.Fatal(err) 63 } 64 jsonData, err := resp.Json() 65 if err != nil { 66 t.Fatal(err) 67 } 68 if jsonData.Get("headers.Content-Type").String() != "application/json" { 69 t.Fatal("json data error") 70 } 71 bodyJson, err := gson.Decode(jsonBody) 72 if err != nil { 73 t.Fatal(err) 74 } 75 if bodyJson.String() != jsonData.Get("data").String() { 76 t.Fatal("json data error") 77 } 78 } 79 func TestSendJsonWithGson(t *testing.T) { 80 bodyJson, err := gson.Decode(struct{ Name string }{"test"}) 81 if err != nil { 82 t.Fatal(err) 83 } 84 resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{ 85 Json: bodyJson, 86 }) 87 if err != nil { 88 t.Fatal(err) 89 } 90 jsonData, err := resp.Json() 91 if err != nil { 92 t.Fatal(err) 93 } 94 if jsonData.Get("headers.Content-Type").String() != "application/json" { 95 t.Fatal("json data error") 96 } 97 if bodyJson.String() != jsonData.Get("data").String() { 98 t.Fatal("json data error") 99 } 100 } 101 func TestSendJsonWithOrder(t *testing.T) { 102 orderMap := requests.NewOrderMap() 103 orderMap.Set("age", "1") 104 orderMap.Set("age4", "4") 105 orderMap.Set("Name", "test") 106 orderMap.Set("age2", "2") 107 orderMap.Set("age3", []string{"22", "121"}) 108 109 bodyJson, err := gson.Encode(orderMap) 110 if err != nil { 111 t.Fatal(err) 112 } 113 resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{ 114 Json: orderMap, 115 }) 116 if err != nil { 117 t.Fatal(err) 118 } 119 jsonData, err := resp.Json() 120 if err != nil { 121 t.Fatal(err) 122 } 123 if jsonData.Get("headers.Content-Type").String() != "application/json" { 124 t.Fatal("json data error") 125 } 126 if tools.BytesToString(bodyJson) != jsonData.Get("data").String() { 127 log.Print(jsonData.Get("data").String()) 128 t.Fatal("json data error") 129 } 130 } 131 132 func TestSendJsonWithEmptiyMap(t *testing.T) { 133 resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{ 134 Form: map[string]string{}, 135 }) 136 if err != nil { 137 t.Fatal(err) 138 } 139 if resp.StatusCode() != 200 { 140 t.Fatal("status code error") 141 } 142 }