github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/http/test/http_test.go (about)

     1  package http
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	baseHttp "github.com/isyscore/isc-gobase/http"
     7  	"github.com/isyscore/isc-gobase/isc"
     8  	"net/http"
     9  	"testing"
    10  	"unsafe"
    11  )
    12  
    13  type DemoHttpHook struct {
    14  }
    15  
    16  func (*DemoHttpHook) Before(ctx context.Context, req *http.Request) context.Context {
    17  	return ctx
    18  }
    19  
    20  func (*DemoHttpHook) After(ctx context.Context, rsp *http.Response, rspCode int, rspData any, err error) {
    21  
    22  }
    23  
    24  func TestGetSimple(t *testing.T) {
    25  	_, _, data, err := baseHttp.GetSimple("http://10.30.30.78:29013/api/core/license/osinfo")
    26  
    27  	if err != nil {
    28  		fmt.Printf("error = %v\n", err)
    29  		return
    30  	}
    31  	fmt.Println("结果: " + string(data.([]byte)))
    32  
    33  	datas := isc.ToInt(unsafe.Sizeof(data))
    34  
    35  	fmt.Println("====" + isc.ToString(datas))
    36  }
    37  
    38  func Test_urlWithParameter(t *testing.T) {
    39  	type args struct {
    40  		url          string
    41  		parameterMap map[string]string
    42  	}
    43  	tests := []struct {
    44  		name string
    45  		args args
    46  		want string
    47  	}{
    48  		{
    49  			name: "normal.",
    50  			args: args{
    51  				url: "http://127.0.0.1:38080",
    52  				parameterMap: map[string]string{
    53  					"msg": "{\"a\":1}",
    54  				},
    55  			},
    56  			want: "http://127.0.0.1:38080?msg=%7B%22a%22%3A1%7D",
    57  		},
    58  		{
    59  			name: "url with queryParam",
    60  			args: args{
    61  				url: "http://127.0.0.1:38080?example=1",
    62  				parameterMap: map[string]string{
    63  					"msg": "{\"a\":1}",
    64  				},
    65  			},
    66  			want: "http://127.0.0.1:38080?example=1&msg=%7B%22a%22%3A1%7D",
    67  		},
    68  		{
    69  			name: "url with simple",
    70  			args: args{
    71  				url: "www.example.com",
    72  				parameterMap: map[string]string{
    73  					"msg": "{\"a\":1}",
    74  				},
    75  			},
    76  			want: "www.example.com?msg=%7B%22a%22%3A1%7D",
    77  		},
    78  	}
    79  	for _, tt := range tests {
    80  		t.Run(tt.name, func(t *testing.T) {
    81  			if got := baseHttp.UrlWithParameter(tt.args.url, tt.args.parameterMap); got != tt.want {
    82  				t.Errorf("UrlWithParameter() = %v, want %v", got, tt.want)
    83  			}
    84  		})
    85  	}
    86  }