mirror of https://github.com/ceph/go-ceph
rgw/admin: add ability to mock HTTP Client
We can now use a mocked client to be consumed by unit tests as well as various users of the API. Signed-off-by: Sébastien Han <seb@redhat.com>
This commit is contained in:
parent
53b3842e29
commit
42a9620c93
|
@ -0,0 +1,18 @@
|
|||
package admin
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// MockClient is the mock of the HTTP Client
|
||||
// It can be used to mock HTTP request/response from the rgw admin ops API
|
||||
type MockClient struct {
|
||||
// MockDo is a type that mock the Do method from the HTTP package
|
||||
MockDo MockDoType
|
||||
}
|
||||
|
||||
// MockDoType is a custom type that allows setting the function that our Mock Do func will run instead
|
||||
type MockDoType func(req *http.Request) (*http.Response, error)
|
||||
|
||||
// Do is the mock client's `Do` func
|
||||
func (m *MockClient) Do(req *http.Request) (*http.Response, error) { return m.MockDo(req) }
|
|
@ -1,10 +1,13 @@
|
|||
package admin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -135,3 +138,24 @@ func (suite *RadosGWTestSuite) TestUser() {
|
|||
assert.NoError(suite.T(), err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetUserMockAPI(t *testing.T) {
|
||||
r := ioutil.NopCloser(bytes.NewReader(fakeUserResponse))
|
||||
mockClient := &MockClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
if req.URL.RawQuery == "format=json&uid=dashboard-admin" && req.Method == http.MethodGet && req.URL.Path == "127.0.0.1/admin/user" {
|
||||
return &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: r,
|
||||
}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unexpected request: %q. method %q. path %q", req.URL.RawQuery, req.Method, req.URL.Path)
|
||||
},
|
||||
}
|
||||
|
||||
api, err := New("127.0.0.1", "accessKey", "secretKey", mockClient)
|
||||
assert.NoError(t, err)
|
||||
u, err := api.GetUser(context.TODO(), User{ID: "dashboard-admin"})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "dashboard-admin", u.DisplayName, u)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue