Test: remote handler get & request chan operations / Modify: GET request to remote server should return error http status code
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// GeneralClient 通用 HTTP Client
|
||||
var GeneralClient Client = HTTPClient{}
|
||||
|
||||
// Response 请求的响应或错误信息
|
||||
@@ -22,7 +23,7 @@ type Response struct {
|
||||
|
||||
// Client 请求客户端
|
||||
type Client interface {
|
||||
Request(method, target string, body io.Reader, opts ...Option) Response
|
||||
Request(method, target string, body io.Reader, opts ...Option) *Response
|
||||
}
|
||||
|
||||
// HTTPClient 实现 Client 接口
|
||||
@@ -63,7 +64,6 @@ func WithTimeout(t time.Duration) Option {
|
||||
}
|
||||
|
||||
// WithContext 设置请求上下文
|
||||
// TODO 测试
|
||||
func WithContext(c context.Context) Option {
|
||||
return optionFunc(func(o *options) {
|
||||
o.ctx = c
|
||||
@@ -86,7 +86,7 @@ func WithHeader(header http.Header) Option {
|
||||
}
|
||||
|
||||
// Request 发送HTTP请求
|
||||
func (c HTTPClient) Request(method, target string, body io.Reader, opts ...Option) Response {
|
||||
func (c HTTPClient) Request(method, target string, body io.Reader, opts ...Option) *Response {
|
||||
// 应用额外设置
|
||||
options := newDefaultOption()
|
||||
for _, o := range opts {
|
||||
@@ -107,7 +107,7 @@ func (c HTTPClient) Request(method, target string, body io.Reader, opts ...Optio
|
||||
req, err = http.NewRequest(method, target, body)
|
||||
}
|
||||
if err != nil {
|
||||
return Response{Err: err}
|
||||
return &Response{Err: err}
|
||||
}
|
||||
|
||||
// 添加请求header
|
||||
@@ -121,32 +121,44 @@ func (c HTTPClient) Request(method, target string, body io.Reader, opts ...Optio
|
||||
// 发送请求
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return Response{Err: err}
|
||||
return &Response{Err: err}
|
||||
}
|
||||
|
||||
return Response{Err: nil, Response: resp}
|
||||
return &Response{Err: nil, Response: resp}
|
||||
}
|
||||
|
||||
// GetResponse 检查响应并获取响应正文
|
||||
// todo 测试
|
||||
func (resp Response) GetResponse(expectStatus int) (string, error) {
|
||||
func (resp *Response) GetResponse() (string, error) {
|
||||
if resp.Err != nil {
|
||||
return "", resp.Err
|
||||
}
|
||||
respBody, err := ioutil.ReadAll(resp.Response.Body)
|
||||
if resp.Response.StatusCode != expectStatus {
|
||||
return string(respBody),
|
||||
fmt.Errorf("服务器返回非正常HTTP状态%d", resp.Response.StatusCode)
|
||||
}
|
||||
return string(respBody), err
|
||||
}
|
||||
|
||||
// CheckHTTPResponse 检查请求响应HTTP状态码
|
||||
func (resp *Response) CheckHTTPResponse(status int) *Response {
|
||||
if resp.Err != nil {
|
||||
return resp
|
||||
}
|
||||
|
||||
// 检查HTTP状态码
|
||||
if resp.Response.StatusCode != status {
|
||||
resp.Err = fmt.Errorf("服务器返回非正常HTTP状态%d", resp.Response.StatusCode)
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
type nopRSCloser struct {
|
||||
body io.ReadCloser
|
||||
}
|
||||
|
||||
// GetRSCloser 返回带有空seeker的body reader
|
||||
func (resp Response) GetRSCloser() (response.RSCloser, error) {
|
||||
func (resp *Response) GetRSCloser() (response.RSCloser, error) {
|
||||
if resp.Err != nil {
|
||||
return nil, resp.Err
|
||||
}
|
||||
|
||||
return nopRSCloser{
|
||||
body: resp.Response.Body,
|
||||
}, resp.Err
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/HFO4/cloudreve/pkg/auth"
|
||||
"github.com/stretchr/testify/assert"
|
||||
testMock "github.com/stretchr/testify/mock"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -15,9 +18,9 @@ type ClientMock struct {
|
||||
testMock.Mock
|
||||
}
|
||||
|
||||
func (m ClientMock) Request(method, target string, body io.Reader, opts ...Option) Response {
|
||||
func (m ClientMock) Request(method, target string, body io.Reader, opts ...Option) *Response {
|
||||
args := m.Called(method, target, body, opts)
|
||||
return args.Get(0).(Response)
|
||||
return args.Get(0).(*Response)
|
||||
}
|
||||
|
||||
func TestWithTimeout(t *testing.T) {
|
||||
@@ -42,6 +45,13 @@ func TestWithCredential(t *testing.T) {
|
||||
asserts.EqualValues(10, options.signTTL)
|
||||
}
|
||||
|
||||
func TestWithContext(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
options := newDefaultOption()
|
||||
WithContext(context.Background()).apply(options)
|
||||
asserts.NotNil(options.ctx)
|
||||
}
|
||||
|
||||
func TestHTTPClient_Request(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
client := HTTPClient{}
|
||||
@@ -59,4 +69,103 @@ func TestHTTPClient_Request(t *testing.T) {
|
||||
asserts.Nil(resp.Response)
|
||||
}
|
||||
|
||||
// 正常 带有ctx
|
||||
{
|
||||
resp := client.Request(
|
||||
"GET",
|
||||
"http://cloudreveisnotexist.com",
|
||||
strings.NewReader(""),
|
||||
WithTimeout(time.Duration(1)*time.Microsecond),
|
||||
WithCredential(auth.HMACAuth{SecretKey: []byte("123")}, 10),
|
||||
WithContext(context.Background()),
|
||||
)
|
||||
asserts.Error(resp.Err)
|
||||
asserts.Nil(resp.Response)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestResponse_GetResponse(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
// 直接返回错误
|
||||
{
|
||||
resp := Response{
|
||||
Err: errors.New("error"),
|
||||
}
|
||||
content, err := resp.GetResponse()
|
||||
asserts.Empty(content)
|
||||
asserts.Error(err)
|
||||
}
|
||||
|
||||
// 正常
|
||||
{
|
||||
resp := Response{
|
||||
Response: &http.Response{Body: ioutil.NopCloser(strings.NewReader("123"))},
|
||||
}
|
||||
content, err := resp.GetResponse()
|
||||
asserts.Equal("123", content)
|
||||
asserts.NoError(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponse_CheckHTTPResponse(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
// 直接返回错误
|
||||
{
|
||||
resp := Response{
|
||||
Err: errors.New("error"),
|
||||
}
|
||||
res := resp.CheckHTTPResponse(200)
|
||||
asserts.Error(res.Err)
|
||||
}
|
||||
|
||||
// 404错误
|
||||
{
|
||||
resp := Response{
|
||||
Response: &http.Response{StatusCode: 404},
|
||||
}
|
||||
res := resp.CheckHTTPResponse(200)
|
||||
asserts.Error(res.Err)
|
||||
}
|
||||
|
||||
// 通过
|
||||
{
|
||||
resp := Response{
|
||||
Response: &http.Response{StatusCode: 200},
|
||||
}
|
||||
res := resp.CheckHTTPResponse(200)
|
||||
asserts.NoError(res.Err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponse_GetRSCloser(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
// 直接返回错误
|
||||
{
|
||||
resp := Response{
|
||||
Err: errors.New("error"),
|
||||
}
|
||||
res, err := resp.GetRSCloser()
|
||||
asserts.Error(err)
|
||||
asserts.Nil(res)
|
||||
}
|
||||
|
||||
// 正常
|
||||
{
|
||||
resp := Response{
|
||||
Response: &http.Response{Body: ioutil.NopCloser(strings.NewReader("123"))},
|
||||
}
|
||||
res, err := resp.GetRSCloser()
|
||||
asserts.NoError(err)
|
||||
content, err := ioutil.ReadAll(res)
|
||||
asserts.NoError(err)
|
||||
asserts.Equal("123", string(content))
|
||||
_, err = res.Seek(0, 0)
|
||||
asserts.Error(err)
|
||||
asserts.NoError(res.Close())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ func RemoteCallback(url string, body serializer.RemoteUploadCallback) error {
|
||||
}
|
||||
|
||||
// 检查返回HTTP状态码
|
||||
rawResp, err := resp.GetResponse(200)
|
||||
rawResp, err := resp.CheckHTTPResponse(200).GetResponse()
|
||||
if err != nil {
|
||||
return serializer.NewError(serializer.CodeCallbackError, "服务器返回异常响应", err)
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ func TestRemoteCallback(t *testing.T) {
|
||||
"http://test/test/url",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(Response{
|
||||
).Return(&Response{
|
||||
Err: nil,
|
||||
Response: &http.Response{
|
||||
StatusCode: 200,
|
||||
@@ -75,7 +75,7 @@ func TestRemoteCallback(t *testing.T) {
|
||||
"http://test/test/url",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(Response{
|
||||
).Return(&Response{
|
||||
Err: nil,
|
||||
Response: &http.Response{
|
||||
StatusCode: 200,
|
||||
@@ -99,7 +99,7 @@ func TestRemoteCallback(t *testing.T) {
|
||||
"http://test/test/url",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(Response{
|
||||
).Return(&Response{
|
||||
Err: nil,
|
||||
Response: &http.Response{
|
||||
StatusCode: 404,
|
||||
@@ -123,7 +123,7 @@ func TestRemoteCallback(t *testing.T) {
|
||||
"http://test/test/url",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(Response{
|
||||
).Return(&Response{
|
||||
Err: errors.New("error"),
|
||||
})
|
||||
GeneralClient = clientMock
|
||||
|
||||
Reference in New Issue
Block a user