-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
fanjindong
committed
Aug 30, 2021
1 parent
f412f33
commit a63749d
Showing
13 changed files
with
575 additions
and
584 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package requests | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
const ( | ||
version = "0.0.1" | ||
userAgent = "go-requests/" + version | ||
author = "fanjindong" | ||
) | ||
|
||
const ( | ||
GET = "GET" | ||
POST = "POST" | ||
PUT = "PUT" | ||
DELETE = "DELETE" | ||
OPTIONS = "OPTIONS" | ||
PATCH = "PATCH" | ||
HEAD = "HEAD" | ||
) | ||
|
||
func Get(url string, opts ...ReqOption) (*Response, error) { | ||
return DefaultClient.Request(GET, url, opts...) | ||
} | ||
|
||
func Post(url string, opts ...ReqOption) (*Response, error) { | ||
return DefaultClient.Request(POST, url, opts...) | ||
} | ||
|
||
func Put(url string, opts ...ReqOption) (*Response, error) { | ||
return DefaultClient.Request(PUT, url, opts...) | ||
} | ||
|
||
func Delete(url string, opts ...ReqOption) (*Response, error) { | ||
return DefaultClient.Request(DELETE, url, opts...) | ||
} | ||
|
||
func ReqOptions(url string, opts ...ReqOption) (*Response, error) { | ||
return DefaultClient.Request(OPTIONS, url, opts...) | ||
} | ||
|
||
func Patch(url string, opts ...ReqOption) (*Response, error) { | ||
return DefaultClient.Request(PATCH, url, opts...) | ||
} | ||
|
||
func Head(url string, opts ...ReqOption) (*Response, error) { | ||
return DefaultClient.Request(HEAD, url, opts...) | ||
} | ||
|
||
type Client struct { | ||
*http.Client | ||
} | ||
|
||
var DefaultClient = &Client{http.DefaultClient} | ||
|
||
func NewClient(opts ...ClientOption) *Client { | ||
c := &Client{&http.Client{}} | ||
for _, opt := range opts { | ||
opt(c) | ||
} | ||
return c | ||
} | ||
|
||
func (s *Client) Request(method, url string, opts ...ReqOption) (*Response, error) { | ||
method = strings.ToUpper(method) | ||
switch method { | ||
case HEAD, GET, POST, DELETE, OPTIONS, PUT, PATCH: | ||
default: | ||
return nil, ErrInvalidMethod | ||
} | ||
|
||
req, err := NewRequest(method, url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, opt := range opts { | ||
err = opt.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
resp, err := s.Do(req.Request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewResponse(resp) | ||
} | ||
|
||
func (s *Client) Get(url string, opts ...ReqOption) (*Response, error) { | ||
return s.Request(GET, url, opts...) | ||
} | ||
|
||
func (s *Client) Post(url string, opts ...ReqOption) (*Response, error) { | ||
return s.Request(POST, url, opts...) | ||
} | ||
|
||
func (s *Client) Put(url string, opts ...ReqOption) (*Response, error) { | ||
return s.Request(PUT, url, opts...) | ||
} | ||
|
||
func (s *Client) Delete(url string, opts ...ReqOption) (*Response, error) { | ||
return s.Request(DELETE, url, opts...) | ||
} | ||
|
||
func (s *Client) ReqOptions(url string, opts ...ReqOption) (*Response, error) { | ||
return s.Request(OPTIONS, url, opts...) | ||
} | ||
|
||
func (s *Client) Patch(url string, opts ...ReqOption) (*Response, error) { | ||
return s.Request(PATCH, url, opts...) | ||
} | ||
|
||
func (s *Client) Head(url string, opts ...ReqOption) (*Response, error) { | ||
return s.Request(HEAD, url, opts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package requests | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
var testUrl = fmt.Sprintf("http://127.0.0.1:%d", port) | ||
|
||
func TestGet(t *testing.T) { | ||
type args struct { | ||
url string | ||
option []ReqOption | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want map[string]string | ||
wantErr bool | ||
}{ | ||
{name: "1", args: args{url: testUrl + "/get", option: []ReqOption{}}, want: map[string]string{}}, | ||
{name: "2", args: args{url: testUrl + "/get", option: []ReqOption{Params{"a": "1"}}}, want: map[string]string{"a": "1"}}, | ||
{name: "3", args: args{url: testUrl + "/get", option: []ReqOption{Params{"a": "1", "b": "x"}}}, want: map[string]string{"a": "1", "b": "x"}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
resp, err := Get(tt.args.url, tt.args.option...) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
got := map[string]string{} | ||
if err := resp.Json(&got); err != nil { | ||
t.Errorf("resp.Json() error = %v", err) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Get() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestPost(t *testing.T) { | ||
type args struct { | ||
url string | ||
option []ReqOption | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want map[string]interface{} | ||
wantErr bool | ||
}{ | ||
{name: "1", args: args{url: testUrl + "/post", option: []ReqOption{}}, want: map[string]interface{}{}}, | ||
{name: "2", args: args{url: testUrl + "/post", option: []ReqOption{Json{"a": "1"}}}, want: map[string]interface{}{"a": "1"}}, | ||
{name: "3", args: args{url: testUrl + "/post", option: []ReqOption{Json{"a": "x", "b": 1.2}}}, want: map[string]interface{}{"a": "x", "b": 1.2}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
resp, err := Post(tt.args.url, tt.args.option...) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Post() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
got := map[string]interface{}{} | ||
if err := resp.Json(&got); err != nil { | ||
t.Errorf("resp.Json() error = %v", err) | ||
return | ||
} | ||
//for k := range got { | ||
// if got[k] != tt.want[k] { | ||
// t.Errorf("Post() k = %v, got = %+v, want %+v", k, got[k].(int), tt.want[k].(int)) | ||
// } | ||
//} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Post() got = %+v, want %+v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestReuseConnection(t *testing.T) { | ||
for i := 0; i < 10; i++ { | ||
resp, err := Get(testUrl) | ||
//resp, err := http.Get(testUrl) | ||
if err != nil { | ||
t.Log(err) | ||
return | ||
} | ||
//data, _ := ioutil.ReadAll(resp.Body) | ||
t.Log(resp.Status, resp.Text()) | ||
//resp.Body.Close() | ||
time.Sleep(1000 * time.Millisecond) | ||
} | ||
} | ||
|
||
//BenchmarkGetRequest-8 27895 43212 ns/op | ||
func BenchmarkGetRequest(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
_, err := Get(testUrl) | ||
if err != nil { | ||
panic(err) | ||
} | ||
//resp.Text() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.