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