forked from zeromicro/go-zero
-
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.
feat: add httpc/Service for convinience (zeromicro#1641)
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package httpc | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type ( | ||
Service interface { | ||
Do(r *http.Request, opts ...Option) (*http.Response, error) | ||
Get(url string, opts ...Option) (*http.Response, error) | ||
Post(url, contentType string, body io.Reader, opts ...Option) (*http.Response, error) | ||
} | ||
|
||
namedService struct { | ||
name string | ||
opts []Option | ||
} | ||
) | ||
|
||
func NewService(name string, opts ...Option) Service { | ||
return namedService{ | ||
name: name, | ||
opts: opts, | ||
} | ||
} | ||
|
||
func (s namedService) Do(r *http.Request, opts ...Option) (*http.Response, error) { | ||
return Do(s.name, r, append(s.opts, opts...)...) | ||
} | ||
|
||
func (s namedService) Get(url string, opts ...Option) (*http.Response, error) { | ||
return Get(s.name, url, append(s.opts, opts...)...) | ||
} | ||
|
||
func (s namedService) Post(url, contentType string, body io.Reader, opts ...Option) ( | ||
*http.Response, error) { | ||
return Post(s.name, url, contentType, body, append(s.opts, 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,40 @@ | ||
package httpc | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNamedService_Do(t *testing.T) { | ||
svr := httptest.NewServer(http.RedirectHandler("/foo", http.StatusMovedPermanently)) | ||
req, err := http.NewRequest(http.MethodGet, svr.URL, nil) | ||
assert.Nil(t, err) | ||
service := NewService("foo") | ||
_, err = service.Do(req) | ||
// too many redirects | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestNamedService_Get(t *testing.T) { | ||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
})) | ||
service := NewService("foo") | ||
resp, err := service.Get(svr.URL, func(cli *http.Client) { | ||
cli.Transport = http.DefaultTransport | ||
}) | ||
assert.Nil(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
} | ||
|
||
func TestNamedService_Post(t *testing.T) { | ||
svr := httptest.NewServer(http.NotFoundHandler()) | ||
service := NewService("foo") | ||
_, err := service.Post("tcp://bad request", "application/json", nil) | ||
assert.NotNil(t, err) | ||
resp, err := service.Post(svr.URL, "application/json", nil) | ||
assert.Nil(t, err) | ||
assert.Equal(t, http.StatusNotFound, resp.StatusCode) | ||
} |