Skip to content

Commit aa43cda

Browse files
authored
Add some http functions (#24)
1 parent 381641f commit aa43cda

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

httputil/client.go

+21
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,24 @@ func (c *httpClient) Download(path, url string, alwaysDownload bool) error {
170170
}
171171
return os.WriteFile(path, data, fs.ModePerm)
172172
}
173+
174+
func (c *httpClient) HttpPostData(url string, data []byte) (resp *http.Response, err error) {
175+
return c.sendData(http.MethodPost, url, data)
176+
}
177+
178+
func (c *httpClient) HttpPut(url string, data []byte) (resp *http.Response, err error) {
179+
return c.sendData(http.MethodPut, url, data)
180+
}
181+
182+
func (c *httpClient) HttpDelete(url string, data []byte) (resp *http.Response, err error) {
183+
return c.sendData(http.MethodDelete, url, data)
184+
}
185+
186+
func (c *httpClient) sendData(method string, url string, data []byte) (resp *http.Response, err error) {
187+
req, err := http.NewRequest(method, url, bytes.NewReader(data))
188+
if err != nil {
189+
return nil, err
190+
}
191+
req.Header.Set("Content-Type", "application/json")
192+
return c.defaultClient.Do(req)
193+
}

httputil/http.go

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ type HttpClient interface {
3636
HttpPostWithoutRedirect(url string, data url.Values) (resp *http.Response, err error)
3737
// Download if the local file does not exist and the alwaysDownload is false, downloads the remote file to local path
3838
Download(path, url string, alwaysDownload bool) error
39+
// HttpPostData send a post request with data
40+
HttpPostData(url string, data []byte) (resp *http.Response, err error)
41+
// HttpPut send a put request with data
42+
HttpPut(url string, data []byte) (resp *http.Response, err error)
43+
// HttpDelete send a delete request with data
44+
HttpDelete(url string, data []byte) (resp *http.Response, err error)
3945
}
4046

4147
// NewTLSConfig create a tls config

httputil/http_test.go

+123
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,129 @@ func TestNewHttpClient(t *testing.T) {
445445
}
446446
}
447447

448+
func TestHttpPostData(t *testing.T) {
449+
initDefaultClient()
450+
mux := http.NewServeMux()
451+
mux.HandleFunc("/post_data", func(w http.ResponseWriter, r *http.Request) {
452+
data, _ := io.ReadAll(r.Body)
453+
fmt.Fprintf(w, string(data))
454+
})
455+
server := httptest.NewServer(mux)
456+
defer server.Close()
457+
458+
testCases := []struct {
459+
name string
460+
path string
461+
expectBody string
462+
}{
463+
{"return data hello", "/post_data", "hello"},
464+
{"return data world", "/post_data", "world"},
465+
}
466+
467+
for _, tc := range testCases {
468+
t.Run(tc.name, func(t *testing.T) {
469+
reqUrl := server.URL + tc.path
470+
resp, err := testHttpClient.HttpPostData(reqUrl, []byte(tc.expectBody))
471+
if err != nil {
472+
t.Errorf("HttpPostData: request error, url=%s err=%v", reqUrl, err)
473+
return
474+
}
475+
defer resp.Body.Close()
476+
data, err := io.ReadAll(resp.Body)
477+
if err != nil {
478+
t.Errorf("HttpPostData: read response body error, url=%s err=%v", reqUrl, err)
479+
return
480+
}
481+
actual := string(data)
482+
if tc.expectBody != actual {
483+
t.Errorf("HttpPostData: expect body => %s, but actual body => %s url=%s", tc.expectBody, actual, reqUrl)
484+
}
485+
})
486+
}
487+
}
488+
489+
func TestHttpPut(t *testing.T) {
490+
initDefaultClient()
491+
mux := http.NewServeMux()
492+
mux.HandleFunc("/put_data", func(w http.ResponseWriter, r *http.Request) {
493+
data, _ := io.ReadAll(r.Body)
494+
fmt.Fprintf(w, string(data))
495+
})
496+
server := httptest.NewServer(mux)
497+
defer server.Close()
498+
499+
testCases := []struct {
500+
name string
501+
path string
502+
expectBody string
503+
}{
504+
{"return data hello", "/put_data", "hello"},
505+
{"return data world", "/put_data", "world"},
506+
}
507+
508+
for _, tc := range testCases {
509+
t.Run(tc.name, func(t *testing.T) {
510+
reqUrl := server.URL + tc.path
511+
resp, err := testHttpClient.HttpPut(reqUrl, []byte(tc.expectBody))
512+
if err != nil {
513+
t.Errorf("HttpPut: request error, url=%s err=%v", reqUrl, err)
514+
return
515+
}
516+
defer resp.Body.Close()
517+
data, err := io.ReadAll(resp.Body)
518+
if err != nil {
519+
t.Errorf("HttpPut: read response body error, url=%s err=%v", reqUrl, err)
520+
return
521+
}
522+
actual := string(data)
523+
if tc.expectBody != actual {
524+
t.Errorf("HttpPut: expect body => %s, but actual body => %s url=%s", tc.expectBody, actual, reqUrl)
525+
}
526+
})
527+
}
528+
}
529+
530+
func TestHttpDelete(t *testing.T) {
531+
initDefaultClient()
532+
mux := http.NewServeMux()
533+
mux.HandleFunc("/delete_data", func(w http.ResponseWriter, r *http.Request) {
534+
data, _ := io.ReadAll(r.Body)
535+
fmt.Fprintf(w, string(data))
536+
})
537+
server := httptest.NewServer(mux)
538+
defer server.Close()
539+
540+
testCases := []struct {
541+
name string
542+
path string
543+
expectBody string
544+
}{
545+
{"return data hello", "/delete_data", "hello"},
546+
{"return data world", "/delete_data", "world"},
547+
}
548+
549+
for _, tc := range testCases {
550+
t.Run(tc.name, func(t *testing.T) {
551+
reqUrl := server.URL + tc.path
552+
resp, err := testHttpClient.HttpPut(reqUrl, []byte(tc.expectBody))
553+
if err != nil {
554+
t.Errorf("HttpDelete: request error, url=%s err=%v", reqUrl, err)
555+
return
556+
}
557+
defer resp.Body.Close()
558+
data, err := io.ReadAll(resp.Body)
559+
if err != nil {
560+
t.Errorf("HttpDelete: read response body error, url=%s err=%v", reqUrl, err)
561+
return
562+
}
563+
actual := string(data)
564+
if tc.expectBody != actual {
565+
t.Errorf("HttpDelete: expect body => %s, but actual body => %s url=%s", tc.expectBody, actual, reqUrl)
566+
}
567+
})
568+
}
569+
}
570+
448571
func initDefaultClient() {
449572
testHttpClient, _ = NewHttpClient(true, "", false)
450573
}

0 commit comments

Comments
 (0)