Skip to content

Commit

Permalink
Add some http functions (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstmdev committed May 19, 2024
1 parent 381641f commit aa43cda
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
21 changes: 21 additions & 0 deletions httputil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,24 @@ func (c *httpClient) Download(path, url string, alwaysDownload bool) error {
}
return os.WriteFile(path, data, fs.ModePerm)
}

func (c *httpClient) HttpPostData(url string, data []byte) (resp *http.Response, err error) {
return c.sendData(http.MethodPost, url, data)
}

func (c *httpClient) HttpPut(url string, data []byte) (resp *http.Response, err error) {
return c.sendData(http.MethodPut, url, data)
}

func (c *httpClient) HttpDelete(url string, data []byte) (resp *http.Response, err error) {
return c.sendData(http.MethodDelete, url, data)
}

func (c *httpClient) sendData(method string, url string, data []byte) (resp *http.Response, err error) {
req, err := http.NewRequest(method, url, bytes.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
return c.defaultClient.Do(req)
}
6 changes: 6 additions & 0 deletions httputil/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ type HttpClient interface {
HttpPostWithoutRedirect(url string, data url.Values) (resp *http.Response, err error)
// Download if the local file does not exist and the alwaysDownload is false, downloads the remote file to local path
Download(path, url string, alwaysDownload bool) error
// HttpPostData send a post request with data
HttpPostData(url string, data []byte) (resp *http.Response, err error)
// HttpPut send a put request with data
HttpPut(url string, data []byte) (resp *http.Response, err error)
// HttpDelete send a delete request with data
HttpDelete(url string, data []byte) (resp *http.Response, err error)
}

// NewTLSConfig create a tls config
Expand Down
123 changes: 123 additions & 0 deletions httputil/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,129 @@ func TestNewHttpClient(t *testing.T) {
}
}

func TestHttpPostData(t *testing.T) {
initDefaultClient()
mux := http.NewServeMux()
mux.HandleFunc("/post_data", func(w http.ResponseWriter, r *http.Request) {
data, _ := io.ReadAll(r.Body)
fmt.Fprintf(w, string(data))
})
server := httptest.NewServer(mux)
defer server.Close()

testCases := []struct {
name string
path string
expectBody string
}{
{"return data hello", "/post_data", "hello"},
{"return data world", "/post_data", "world"},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
reqUrl := server.URL + tc.path
resp, err := testHttpClient.HttpPostData(reqUrl, []byte(tc.expectBody))
if err != nil {
t.Errorf("HttpPostData: request error, url=%s err=%v", reqUrl, err)
return
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("HttpPostData: read response body error, url=%s err=%v", reqUrl, err)
return
}
actual := string(data)
if tc.expectBody != actual {
t.Errorf("HttpPostData: expect body => %s, but actual body => %s url=%s", tc.expectBody, actual, reqUrl)
}
})
}
}

func TestHttpPut(t *testing.T) {
initDefaultClient()
mux := http.NewServeMux()
mux.HandleFunc("/put_data", func(w http.ResponseWriter, r *http.Request) {
data, _ := io.ReadAll(r.Body)
fmt.Fprintf(w, string(data))
})
server := httptest.NewServer(mux)
defer server.Close()

testCases := []struct {
name string
path string
expectBody string
}{
{"return data hello", "/put_data", "hello"},
{"return data world", "/put_data", "world"},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
reqUrl := server.URL + tc.path
resp, err := testHttpClient.HttpPut(reqUrl, []byte(tc.expectBody))
if err != nil {
t.Errorf("HttpPut: request error, url=%s err=%v", reqUrl, err)
return
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("HttpPut: read response body error, url=%s err=%v", reqUrl, err)
return
}
actual := string(data)
if tc.expectBody != actual {
t.Errorf("HttpPut: expect body => %s, but actual body => %s url=%s", tc.expectBody, actual, reqUrl)
}
})
}
}

func TestHttpDelete(t *testing.T) {
initDefaultClient()
mux := http.NewServeMux()
mux.HandleFunc("/delete_data", func(w http.ResponseWriter, r *http.Request) {
data, _ := io.ReadAll(r.Body)
fmt.Fprintf(w, string(data))
})
server := httptest.NewServer(mux)
defer server.Close()

testCases := []struct {
name string
path string
expectBody string
}{
{"return data hello", "/delete_data", "hello"},
{"return data world", "/delete_data", "world"},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
reqUrl := server.URL + tc.path
resp, err := testHttpClient.HttpPut(reqUrl, []byte(tc.expectBody))
if err != nil {
t.Errorf("HttpDelete: request error, url=%s err=%v", reqUrl, err)
return
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("HttpDelete: read response body error, url=%s err=%v", reqUrl, err)
return
}
actual := string(data)
if tc.expectBody != actual {
t.Errorf("HttpDelete: expect body => %s, but actual body => %s url=%s", tc.expectBody, actual, reqUrl)
}
})
}
}

func initDefaultClient() {
testHttpClient, _ = NewHttpClient(true, "", false)
}

0 comments on commit aa43cda

Please sign in to comment.