| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 | 
							- package transport
 
- import (
 
- 	"encoding/json"
 
- 	"flag"
 
- 	"net/http"
 
- 	"net/url"
 
- 	"strings"
 
- 	"testing"
 
- 	"time"
 
- )
 
- /**
 
-  * @Author : dixuanhuang
 
-  * @File : http_test.go
 
-  * @Date : 2020/8/4 11:26 上午
 
-  * @Description:
 
- **/
 
- func TestRegister(t *testing.T)  {
 
- 	if !flag.Parsed(){
 
- 		flag.Parse()
 
- 	}
 
- 	args := flag.Args()
 
- 	postUrl := "http://127.0.0.1:30036/register"
 
- 	if len(args) > 0{
 
- 		postUrl = args[0]
 
- 	}
 
- 	body := map[string]string{
 
- 		"email":"aoho1@mail.com",
 
- 		"password":"aoho",
 
- 		"username": "aoho",
 
- 	}
 
- 	result, err := httpPost(postUrl, body)
 
- 	if err != nil{
 
- 		t.Errorf("http post err %s", err)
 
- 		t.FailNow()
 
- 	}
 
- 	t.Logf("result is %v", result)
 
- }
 
- func TestLogin(t *testing.T)  {
 
- 	if !flag.Parsed(){
 
- 		flag.Parse()
 
- 	}
 
- 	args := flag.Args()
 
- 	postUrl := "http://127.0.0.1:30036/login"
 
- 	if len(args) > 0{
 
- 		postUrl = args[0]
 
- 	}
 
- 	body := map[string]string{
 
- 		"email":"aoho@mail.com",
 
- 		"password":"aoho1",
 
- 	}
 
- 	result, err := httpPost(postUrl, body)
 
- 	if err != nil{
 
- 		t.Errorf("http post err %s", err)
 
- 		t.FailNow()
 
- 	}
 
- 	t.Logf("result is %v", result)
 
- }
 
- func httpPost(postUrl string, body map[string]string) (interface{}, error) {
 
- 	// 超时时间:5秒
 
- 	client := &http.Client{Timeout: 5 * time.Second}
 
- 	dataUrlVal := url.Values{}
 
- 	for k, v := range body {
 
- 		dataUrlVal.Add(k, v)
 
- 	}
 
- 	req,err := http.NewRequest("POST", postUrl, strings.NewReader(dataUrlVal.Encode()))
 
- 	if err != nil{
 
- 		return nil, err
 
- 	}
 
- 	req.Header.Add("Content-Type","application/x-www-form-urlencoded")
 
- 	//提交请求
 
- 	resp, err := client.Do(req)
 
- 	if err != nil{
 
- 		return nil, err
 
- 	}
 
- 	defer resp.Body.Close()
 
- 	//读取返回值
 
- 	decode := json.NewDecoder(resp.Body)
 
- 	var result interface{}
 
- 	err = decode.Decode(&result)
 
- 	if err != nil{
 
- 		return nil, err
 
- 	}
 
- 	return result, nil
 
- }
 
 
  |