mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
Compare commits
2 Commits
6853d627f4
...
de9ee08be4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de9ee08be4 | ||
|
|
5381450bea |
@@ -5,7 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
@@ -24,7 +23,8 @@ func TestHttpGet(t *testing.T) {
|
||||
|
||||
resp, err := HttpGet(url, header)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
t.Log("net error: " + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
@@ -45,8 +45,10 @@ func TestHttpPost(t *testing.T) {
|
||||
|
||||
resp, err := HttpPost(url, header, nil, bodyParams)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
t.Log("net error: " + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
t.Log("response: ", resp.StatusCode, string(body))
|
||||
}
|
||||
@@ -55,21 +57,18 @@ func TestHttpPostFormData(t *testing.T) {
|
||||
apiUrl := "https://jsonplaceholder.typicode.com/todos"
|
||||
header := map[string]string{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
// "Content-Type": "multipart/form-data",
|
||||
}
|
||||
|
||||
postData := url.Values{}
|
||||
postData.Add("userId", "1")
|
||||
postData.Add("title", "TestToDo")
|
||||
|
||||
// postData := make(map[string]string)
|
||||
// postData["userId"] = "1"
|
||||
// postData["title"] = "title"
|
||||
|
||||
resp, err := HttpPost(apiUrl, header, nil, postData)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
t.Log("net error: " + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
t.Log("response: ", resp.StatusCode, string(body))
|
||||
}
|
||||
@@ -89,8 +88,10 @@ func TestHttpPut(t *testing.T) {
|
||||
|
||||
resp, err := HttpPut(url, header, nil, bodyParams)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
t.Log("net error: ", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
t.Log("response: ", resp.StatusCode, string(body))
|
||||
}
|
||||
@@ -110,8 +111,10 @@ func TestHttpPatch(t *testing.T) {
|
||||
|
||||
resp, err := HttpPatch(url, header, nil, bodyParams)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
t.Log("net error: ", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
t.Log("response: ", resp.StatusCode, string(body))
|
||||
}
|
||||
@@ -120,8 +123,10 @@ func TestHttpDelete(t *testing.T) {
|
||||
url := "https://jsonplaceholder.typicode.com/todos/1"
|
||||
resp, err := HttpDelete(url)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
t.Log("net error: ", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
t.Log("response: ", resp.StatusCode, string(body))
|
||||
}
|
||||
@@ -148,7 +153,8 @@ func TestParseResponse(t *testing.T) {
|
||||
|
||||
resp, err := HttpGet(url, header)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
t.Log("net error: ", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
type Todo struct {
|
||||
@@ -161,8 +167,10 @@ func TestParseResponse(t *testing.T) {
|
||||
toDoResp := &Todo{}
|
||||
err = ParseHttpResponse(resp, toDoResp)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
t.Log("net error: ", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
t.Log("response: ", toDoResp)
|
||||
}
|
||||
|
||||
@@ -179,7 +187,8 @@ func TestHttpClient_Get(t *testing.T) {
|
||||
httpClient := NewHttpClient()
|
||||
resp, err := httpClient.SendRequest(request)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
log.Fatal(err)
|
||||
t.Log("net error: ", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
type Todo struct {
|
||||
@@ -216,7 +225,8 @@ func TestHttpClent_Post(t *testing.T) {
|
||||
httpClient := NewHttpClient()
|
||||
resp, err := httpClient.SendRequest(request)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
t.Log("net error: ", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
@@ -376,8 +386,10 @@ func TestProxy(t *testing.T) {
|
||||
httpClient := NewHttpClientWithConfig(config)
|
||||
resp, err := httpClient.Get("https://www.ipplus360.com/getLocation")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Log("net error: ", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("expected %d, got %d", http.StatusOK, resp.StatusCode)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/mail"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
@@ -24,7 +25,7 @@ var (
|
||||
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
||||
urlMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`)
|
||||
dnsMatcher *regexp.Regexp = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`)
|
||||
emailMatcher *regexp.Regexp = regexp.MustCompile(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`)
|
||||
emailMatcher *regexp.Regexp = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
|
||||
chineseMobileMatcher *regexp.Regexp = regexp.MustCompile(`^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$`)
|
||||
chineseIdMatcher *regexp.Regexp = regexp.MustCompile(`^(\d{17})([0-9]|X|x)$`)
|
||||
chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]")
|
||||
@@ -264,7 +265,10 @@ func IsDns(dns string) bool {
|
||||
// IsEmail check if the string is a email address.
|
||||
// Play: https://go.dev/play/p/Os9VaFlT33G
|
||||
func IsEmail(email string) bool {
|
||||
return emailMatcher.MatchString(email)
|
||||
_, err := mail.ParseAddress(email)
|
||||
return err == nil
|
||||
|
||||
// return emailMatcher.MatchString(email)
|
||||
}
|
||||
|
||||
// IsChineseMobile check if the string is chinese mobile number.
|
||||
|
||||
@@ -285,6 +285,7 @@ func TestIsEmail(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestIsEmail")
|
||||
|
||||
assert.Equal(true, IsEmail("abc@xyz.com"))
|
||||
assert.Equal(false, IsEmail("@abc@xyz.com"))
|
||||
assert.Equal(false, IsEmail("a.b@@com"))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user