Test: remote handler get & request chan operations / Modify: GET request to remote server should return error http status code
This commit is contained in:
@@ -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())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user