From 745082fff1b2892c10731a752f98208a607b65aa Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 29 Dec 2021 11:39:48 +0800 Subject: [PATCH] fix: update api url for http timeout issue in the task of github actions --- netutil/request_test.go | 110 ++++++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 44 deletions(-) diff --git a/netutil/request_test.go b/netutil/request_test.go index f417dd7..868b4de 100644 --- a/netutil/request_test.go +++ b/netutil/request_test.go @@ -11,36 +11,33 @@ import ( ) func TestHttpGet(t *testing.T) { - _, e := HttpGet("", nil) - if e == nil { - t.FailNow() + url := "https://jsonplaceholder.typicode.com/todos/1" + header := map[string]string{ + "Content-Type": "application/json", } - url := "https://gutendex.com/books?" - queryParams := make(map[string]interface{}) - queryParams["ids"] = "1" - - resp, err := HttpGet(url, nil, queryParams) + resp, err := HttpGet(url, header) if err != nil { - fmt.Println("error: ", err) - //t.FailNow() + log.Fatal(err) + t.FailNow() } body, _ := ioutil.ReadAll(resp.Body) fmt.Println("response: ", resp.StatusCode, string(body)) - } func TestHttpPost(t *testing.T) { - url := "http://api.postcodes.io/postcodes" - type Postcode struct { - Postcodes []string `json:"postcodes"` - } - postcode := Postcode{[]string{"OX49 5NU"}} - bodyParams, _ := json.Marshal(postcode) + url := "https://jsonplaceholder.typicode.com/todos" header := map[string]string{ "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) if err != nil { log.Fatal(err) @@ -51,34 +48,55 @@ func TestHttpPost(t *testing.T) { } func TestHttpPut(t *testing.T) { - url := "http://public-api-v1.aspirantzhang.com/users/10420" - type User struct { - Name string `json:"name"` - Email string `json:"email"` - } - user := User{ - "test", - "test@test.com", - } - bodyParams, _ := json.Marshal(user) + 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, "TestPutToDo"} + bodyParams, _ := json.Marshal(todo) + resp, err := HttpPut(url, header, nil, bodyParams) if err != nil { - fmt.Println("error: ", err) - //t.FailNow() + log.Fatal(err) + 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) fmt.Println("response: ", resp.StatusCode, string(body)) } 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) if err != nil { - fmt.Println("error: ", err) - //t.FailNow() + log.Fatal(err) + t.FailNow() } body, _ := ioutil.ReadAll(resp.Body) fmt.Println("response: ", resp.StatusCode, string(body)) @@ -100,25 +118,29 @@ func TestConvertMapToQueryString(t *testing.T) { } func TestParseResponse(t *testing.T) { - url := "http://public-api-v1.aspirantzhang.com/users" - type User struct { - Id int `json:"id"` - Name string `json:"name"` - Email string `json:"email"` + url := "https://jsonplaceholder.typicode.com/todos/1" + header := map[string]string{ + "Content-Type": "application/json", } - type UserResp struct { - Data []User `json:"data"` - } - resp, err := HttpGet(url) + + resp, err := HttpGet(url, header) if err != nil { log.Fatal(err) t.FailNow() } - userResp := &UserResp{} - err = ParseHttpResponse(resp, userResp) + + type Todo struct { + Id int `json:"id"` + UserId int `json:"userId"` + Title string `json:"title"` + Completed bool `json:"completed"` + } + + toDoResp := &Todo{} + err = ParseHttpResponse(resp, toDoResp) if err != nil { log.Fatal(err) t.FailNow() } - fmt.Println(userResp.Data) + fmt.Println("response: ", toDoResp) }