1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

fix: update api url for http timeout issue in the task of github actions

This commit is contained in:
dudaodong
2021-12-29 11:39:48 +08:00
parent 24b8da360e
commit 745082fff1
+69 -47
View File
@@ -11,36 +11,33 @@ import (
) )
func TestHttpGet(t *testing.T) { func TestHttpGet(t *testing.T) {
_, e := HttpGet("", nil) url := "https://jsonplaceholder.typicode.com/todos/1"
if e == nil { header := map[string]string{
t.FailNow() "Content-Type": "application/json",
} }
url := "https://gutendex.com/books?" resp, err := HttpGet(url, header)
queryParams := make(map[string]interface{})
queryParams["ids"] = "1"
resp, err := HttpGet(url, nil, queryParams)
if err != nil { if err != nil {
fmt.Println("error: ", err) log.Fatal(err)
//t.FailNow() t.FailNow()
} }
body, _ := ioutil.ReadAll(resp.Body) body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response: ", resp.StatusCode, string(body)) fmt.Println("response: ", resp.StatusCode, string(body))
} }
func TestHttpPost(t *testing.T) { func TestHttpPost(t *testing.T) {
url := "http://api.postcodes.io/postcodes" url := "https://jsonplaceholder.typicode.com/todos"
type Postcode struct {
Postcodes []string `json:"postcodes"`
}
postcode := Postcode{[]string{"OX49 5NU"}}
bodyParams, _ := json.Marshal(postcode)
header := map[string]string{ header := map[string]string{
"Content-Type": "application/json", "Content-Type": "application/json",
} }
type Todo struct {
UserId int `json:"userId"`
Title string `json:"title"`
}
todo := Todo{1, "TestAddToDo"}
bodyParams, _ := json.Marshal(todo)
resp, err := HttpPost(url, header, nil, bodyParams) resp, err := HttpPost(url, header, nil, bodyParams)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@@ -51,34 +48,55 @@ func TestHttpPost(t *testing.T) {
} }
func TestHttpPut(t *testing.T) { func TestHttpPut(t *testing.T) {
url := "http://public-api-v1.aspirantzhang.com/users/10420" url := "https://jsonplaceholder.typicode.com/todos/1"
type User struct {
Name string `json:"name"`
Email string `json:"email"`
}
user := User{
"test",
"test@test.com",
}
bodyParams, _ := json.Marshal(user)
header := map[string]string{ header := map[string]string{
"Content-Type": "application/json", "Content-Type": "application/json",
} }
type Todo struct {
Id int `json:"id"`
UserId int `json:"userId"`
Title string `json:"title"`
}
todo := Todo{1, 1, "TestPutToDo"}
bodyParams, _ := json.Marshal(todo)
resp, err := HttpPut(url, header, nil, bodyParams) resp, err := HttpPut(url, header, nil, bodyParams)
if err != nil { if err != nil {
fmt.Println("error: ", err) log.Fatal(err)
//t.FailNow() t.FailNow()
}
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response: ", resp.StatusCode, string(body))
}
func TestHttpPatch(t *testing.T) {
url := "https://jsonplaceholder.typicode.com/todos/1"
header := map[string]string{
"Content-Type": "application/json",
}
type Todo struct {
Id int `json:"id"`
UserId int `json:"userId"`
Title string `json:"title"`
}
todo := Todo{1, 1, "TestPatchToDo"}
bodyParams, _ := json.Marshal(todo)
resp, err := HttpPatch(url, header, nil, bodyParams)
if err != nil {
log.Fatal(err)
t.FailNow()
} }
body, _ := ioutil.ReadAll(resp.Body) body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response: ", resp.StatusCode, string(body)) fmt.Println("response: ", resp.StatusCode, string(body))
} }
func TestHttpDelete(t *testing.T) { func TestHttpDelete(t *testing.T) {
url := "http://public-api-v1.aspirantzhang.com/users/10420" url := "https://jsonplaceholder.typicode.com/todos/1"
resp, err := HttpDelete(url) resp, err := HttpDelete(url)
if err != nil { if err != nil {
fmt.Println("error: ", err) log.Fatal(err)
//t.FailNow() t.FailNow()
} }
body, _ := ioutil.ReadAll(resp.Body) body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response: ", resp.StatusCode, string(body)) fmt.Println("response: ", resp.StatusCode, string(body))
@@ -100,25 +118,29 @@ func TestConvertMapToQueryString(t *testing.T) {
} }
func TestParseResponse(t *testing.T) { func TestParseResponse(t *testing.T) {
url := "http://public-api-v1.aspirantzhang.com/users" url := "https://jsonplaceholder.typicode.com/todos/1"
type User struct { header := map[string]string{
"Content-Type": "application/json",
}
resp, err := HttpGet(url, header)
if err != nil {
log.Fatal(err)
t.FailNow()
}
type Todo struct {
Id int `json:"id"` Id int `json:"id"`
Name string `json:"name"` UserId int `json:"userId"`
Email string `json:"email"` Title string `json:"title"`
Completed bool `json:"completed"`
} }
type UserResp struct {
Data []User `json:"data"` toDoResp := &Todo{}
} err = ParseHttpResponse(resp, toDoResp)
resp, err := HttpGet(url)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
t.FailNow() t.FailNow()
} }
userResp := &UserResp{} fmt.Println("response: ", toDoResp)
err = ParseHttpResponse(resp, userResp)
if err != nil {
log.Fatal(err)
t.FailNow()
}
fmt.Println(userResp.Data)
} }