github.com/gogf/gf/v2@v2.7.4/net/gclient/gclient_z_unit_issue_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gclient_test
     8  
     9  import (
    10  	"fmt"
    11  	"strings"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/gogf/gf/v2/frame/g"
    16  	"github.com/gogf/gf/v2/net/gclient"
    17  	"github.com/gogf/gf/v2/net/ghttp"
    18  	"github.com/gogf/gf/v2/test/gtest"
    19  	"github.com/gogf/gf/v2/util/guid"
    20  )
    21  
    22  func Test_Issue3748(t *testing.T) {
    23  	s := g.Server(guid.S())
    24  	s.BindHandler("/", func(r *ghttp.Request) {
    25  		r.Response.Write(
    26  			r.GetBody(),
    27  		)
    28  	})
    29  	s.SetDumpRouterMap(false)
    30  	s.Start()
    31  	defer s.Shutdown()
    32  
    33  	clientHost := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
    34  	time.Sleep(100 * time.Millisecond)
    35  
    36  	gtest.C(t, func(t *gtest.T) {
    37  		client := gclient.New()
    38  		client.SetHeader("Content-Type", "application/json")
    39  		data := map[string]interface{}{
    40  			"name":  "@file:",
    41  			"value": "json",
    42  		}
    43  		client.SetPrefix(clientHost)
    44  		content := client.PostContent(ctx, "/", data)
    45  		t.Assert(content, `{"name":"@file:","value":"json"}`)
    46  	})
    47  
    48  	gtest.C(t, func(t *gtest.T) {
    49  		client := gclient.New()
    50  		client.SetHeader("Content-Type", "application/xml")
    51  		data := map[string]interface{}{
    52  			"name":  "@file:",
    53  			"value": "xml",
    54  		}
    55  		client.SetPrefix(clientHost)
    56  		content := client.PostContent(ctx, "/", data)
    57  		t.Assert(content, `<doc><name>@file:</name><value>xml</value></doc>`)
    58  	})
    59  
    60  	gtest.C(t, func(t *gtest.T) {
    61  		client := gclient.New()
    62  		client.SetHeader("Content-Type", "application/x-www-form-urlencoded")
    63  		data := map[string]interface{}{
    64  			"name":  "@file:",
    65  			"value": "x-www-form-urlencoded",
    66  		}
    67  		client.SetPrefix(clientHost)
    68  		content := client.PostContent(ctx, "/", data)
    69  		t.Assert(strings.Contains(content, `Content-Disposition: form-data; name="value"`), true)
    70  		t.Assert(strings.Contains(content, `Content-Disposition: form-data; name="name"`), true)
    71  		t.Assert(strings.Contains(content, "\r\n@file:"), true)
    72  		t.Assert(strings.Contains(content, "\r\nx-www-form-urlencoded"), true)
    73  	})
    74  
    75  	gtest.C(t, func(t *gtest.T) {
    76  		client := gclient.New()
    77  		data := "@file:"
    78  		client.SetPrefix(clientHost)
    79  		_, err := client.Post(ctx, "/", data)
    80  		t.AssertNil(err)
    81  	})
    82  }