github.com/godaddy-x/freego@v1.0.156/http_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/godaddy-x/freego/utils/sdk"
     6  	"testing"
     7  )
     8  
     9  //go test -v http_test.go -bench=BenchmarkPubkey -benchmem -count=10
    10  
    11  const domain = "http://localhost:8090"
    12  
    13  const access_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxNjc2NjIzNTg5NDQ2NTE2NzM3IiwiYXVkIjoiIiwiaXNzIjoiIiwiaWF0IjowLCJleHAiOjE2ODk3ODI3ODgsImRldiI6IkFQUCIsImp0aSI6ImtVYWhIMjV5RDhWMXU0ZnQ4YzEvRVE9PSIsImV4dCI6IiJ9.sV7vhjp7nRJuo6XGU4mrVyHVaM2EzqiX7tGGuPcim+o="
    14  const token_secret = "uHsLjyEFQ8Ik4X+Hy*kT^j#lKHDxs9XcK/wAWhc8#lK!ZC@diQSvcunC5KckkxQ="
    15  const token_expire = 1689782788
    16  
    17  var httpSDK = &sdk.HttpSDK{
    18  	Debug:     false,
    19  	Domain:    domain,
    20  	KeyPath:   "/key",
    21  	LoginPath: "/login",
    22  }
    23  
    24  func TestGetPublicKey(t *testing.T) {
    25  	//publicKey, err := httpSDK.GetPublicKey()
    26  	//if err != nil {
    27  	//	fmt.Println(err)
    28  	//}
    29  	//fmt.Println("服务端公钥: ", publicKey)
    30  }
    31  
    32  func TestECCLogin(t *testing.T) {
    33  	requestData := map[string]string{"username": "1234567890123456", "password": "1234567890123456"}
    34  	responseData := sdk.AuthToken{}
    35  	if err := httpSDK.PostByECC("/login", &requestData, &responseData); err != nil {
    36  		fmt.Println(err)
    37  	}
    38  	fmt.Println(responseData)
    39  }
    40  
    41  func TestGetUser(t *testing.T) {
    42  	httpSDK.AuthObject(&map[string]string{"username": "1234567890123456", "password": "1234567890123456"})
    43  	httpSDK.AuthToken(sdk.AuthToken{Token: access_token, Secret: token_secret, Expired: token_expire})
    44  	requestObj := map[string]interface{}{"uid": 123, "name": "我爱中国/+_=/1df", "limit": 20, "offset": 5}
    45  	responseData := map[string]string{}
    46  	if err := httpSDK.PostByAuth("/getUser", &requestObj, &responseData, true); err != nil {
    47  		fmt.Println(err)
    48  	}
    49  	fmt.Println(responseData)
    50  }
    51  
    52  func TestHAX(t *testing.T) {
    53  	//httpSDK.AuthObject(&map[string]string{"username": "1234567890123456", "password": "1234567890123456"})
    54  	//httpSDK.AuthToken(sdk.AuthToken{Token: access_token, Secret: token_secret})
    55  	requestObj := map[string]interface{}{"uid": 123, "name": "我爱中国/+_=/1df", "limit": 20, "offset": 5}
    56  	responseData := map[string]string{}
    57  	if err := httpSDK.PostByHAX("/login", &requestObj, &responseData); err != nil {
    58  		fmt.Println(err)
    59  	}
    60  	fmt.Println(responseData)
    61  }
    62  
    63  func BenchmarkGetUser(b *testing.B) {
    64  	b.StopTimer()
    65  	b.StartTimer()
    66  	for i := 0; i < b.N; i++ { //use b.N for looping
    67  		httpSDK.AuthToken(sdk.AuthToken{Token: access_token, Secret: token_secret, Expired: token_expire})
    68  		requestObj := map[string]interface{}{"uid": 123, "name": "我爱中国/+_=/1df", "limit": 20, "offset": 5}
    69  		responseData := map[string]string{}
    70  		if err := httpSDK.PostByAuth("/getUser", &requestObj, &responseData, false); err != nil {
    71  			fmt.Println(err)
    72  		}
    73  	}
    74  }
    75  
    76  // go test http_test.go -bench=BenchmarkPubkey  -benchmem -count=10 -cpuprofile cpuprofile.out -memprofile memprofile.out
    77  // go test http_test.go -bench=BenchmarkGetUser  -benchmem -count=10
    78  func BenchmarkECCLogin(b *testing.B) {
    79  	b.StopTimer()
    80  	b.StartTimer()
    81  	for i := 0; i < b.N; i++ { //use b.N for looping
    82  		requestData := map[string]string{"username": "1234567890123456", "password": "1234567890123456"}
    83  		responseData := sdk.AuthToken{}
    84  		if err := httpSDK.PostByECC("/login", &requestData, &responseData); err != nil {
    85  			fmt.Println(err)
    86  		}
    87  	}
    88  }