github.com/ethersphere/bee/v2@v2.2.0/pkg/resolver/client/mock/mock.go (about) 1 // Copyright 2020 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package mock 6 7 import ( 8 "github.com/ethersphere/bee/v2/pkg/resolver/client" 9 "github.com/ethersphere/bee/v2/pkg/swarm" 10 ) 11 12 // Ensure mock Client implements the Client interface. 13 var _ client.Interface = (*Client)(nil) 14 15 // Client is the mock resolver client implementation. 16 type Client struct { 17 isConnected bool 18 endpoint string 19 defaultAddress swarm.Address 20 resolveFn func(string) (swarm.Address, error) 21 } 22 23 // Option is a function that applies an option to a Client. 24 type Option func(*Client) 25 26 // NewClient construct a new mock Client. 27 func NewClient(opts ...Option) *Client { 28 cl := &Client{} 29 30 for _, o := range opts { 31 o(cl) 32 } 33 34 cl.isConnected = true 35 return cl 36 } 37 38 // WithEndpoint will set the endpoint. 39 func WithEndpoint(endpoint string) Option { 40 return func(cl *Client) { 41 cl.endpoint = endpoint 42 } 43 } 44 45 // WitResolveAddress will set the address returned by Resolve. 46 func WitResolveAddress(addr swarm.Address) Option { 47 return func(cl *Client) { 48 cl.defaultAddress = addr 49 } 50 } 51 52 // WithResolveFunc will set the Resolve function implementation. 53 func WithResolveFunc(fn func(string) (swarm.Address, error)) Option { 54 return func(cl *Client) { 55 cl.resolveFn = fn 56 } 57 } 58 59 // IsConnected is the mock IsConnected implementation. 60 func (cl *Client) IsConnected() bool { 61 return cl.isConnected 62 } 63 64 // Endpoint is the mock Endpoint implementation. 65 func (cl *Client) Endpoint() string { 66 return cl.endpoint 67 } 68 69 // Resolve is the mock Resolve implementation 70 func (cl *Client) Resolve(name string) (swarm.Address, error) { 71 if cl.resolveFn == nil { 72 return cl.defaultAddress, nil 73 } 74 return cl.resolveFn(name) 75 } 76 77 // Close is the mock Close implementation. 78 func (cl *Client) Close() error { 79 cl.isConnected = false 80 return nil 81 }