| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- /*
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package fake
- import (
- "context"
- "net/url"
- "strings"
- vault "github.com/hashicorp/vault/api"
- )
- type MockNewRequestFn func(method, requestPath string) *vault.Request
- type MockRawRequestWithContextFn func(ctx context.Context, r *vault.Request) (*vault.Response, error)
- type MockSetTokenFn func(v string)
- type MockTokenFn func() string
- type MockClearTokenFn func()
- type MockSetNamespaceFn func(namespace string)
- type MockAddHeaderFn func(key, value string)
- func NewMockNewRequestFn(req *vault.Request) MockNewRequestFn {
- return func(method, requestPath string) *vault.Request {
- return req
- }
- }
- func NewMockNewRequestListFn(req *vault.Request) MockNewRequestFn {
- return func(method, requestPath string) *vault.Request {
- urlPath := url.URL{
- Path: requestPath,
- }
- req.URL = &urlPath
- req.Params = make(url.Values)
- return req
- }
- }
- // An RequestFn operates on the supplied Request. You might use an RequestFn to
- // test or update the contents of an Request.
- type RequestFn func(req *vault.Request) error
- func NewMockRawRequestWithContextFn(res *vault.Response, err error, ofn ...RequestFn) MockRawRequestWithContextFn {
- return func(_ context.Context, r *vault.Request) (*vault.Response, error) {
- for _, fn := range ofn {
- if err := fn(r); err != nil {
- return res, err
- }
- }
- return res, err
- }
- }
- type VaultListResponse struct {
- Metadata *vault.Response
- Data *vault.Response
- }
- func NewMockRawRequestListWithContextFn(res map[string]VaultListResponse, err error) MockRawRequestWithContextFn {
- return func(_ context.Context, r *vault.Request) (*vault.Response, error) {
- pathList := strings.Split(r.URL.Path, "/")
- path := "default"
- if pathList[4] != "" {
- path = strings.Join(pathList[4:], "/")
- }
- if strings.Contains(r.URL.Path, "metadata") {
- resp := res[path].Metadata
- return resp, err
- }
- resp := res[path].Data
- return resp, err
- }
- }
- func NewSetTokenFn(ofn ...func(v string)) MockSetTokenFn {
- return func(v string) {
- for _, fn := range ofn {
- fn(v)
- }
- }
- }
- func NewTokenFn(v string) MockTokenFn {
- return func() string {
- return v
- }
- }
- func NewClearTokenFn() MockClearTokenFn {
- return func() {}
- }
- func NewSetNamespaceFn() MockSetNamespaceFn {
- return func(namespace string) {}
- }
- func NewAddHeaderFn() MockAddHeaderFn {
- return func(key, value string) {}
- }
- type VaultClient struct {
- MockNewRequest MockNewRequestFn
- MockRawRequestWithContext MockRawRequestWithContextFn
- MockSetToken MockSetTokenFn
- MockToken MockTokenFn
- MockClearToken MockClearTokenFn
- MockSetNamespace MockSetNamespaceFn
- MockAddHeader MockAddHeaderFn
- }
- func (c *VaultClient) NewRequest(method, requestPath string) *vault.Request {
- return c.MockNewRequest(method, requestPath)
- }
- func (c *VaultClient) RawRequestWithContext(ctx context.Context, r *vault.Request) (*vault.Response, error) {
- return c.MockRawRequestWithContext(ctx, r)
- }
- func (c *VaultClient) SetToken(v string) {
- c.MockSetToken(v)
- }
- func (c *VaultClient) Token() string {
- return c.MockToken()
- }
- func (c *VaultClient) ClearToken() {
- c.MockClearToken()
- }
- func (c *VaultClient) SetNamespace(namespace string) {
- c.MockSetNamespace(namespace)
- }
- func (c *VaultClient) AddHeader(key, value string) {
- c.MockAddHeader(key, value)
- }
|