client

package
v1.1.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2024 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Client

It's an implementation of http.Client

You can use it to make requests to the server and easy to mock in tests using client_mock package.

Example of usage

repository.go:

type Repository struct {
  client Client
}

func New(client Client) *Repository {
  return &Repository{
    client: client,
  }
}

func (repo *Repository) CustomFunction(ctx context.Context, userName string) (string, error) {
  response, err := repo.client.Post(context.Background(), "http://test.com/users", []byte(userName), map[string]string{
    "Content-Type": "application/json",
  })
  if err != nil {
    return "", fmt.Errorf("error: %v", err)
  }
  return http.StatusText(response.StatusCode), nil
}

repository_test.go:

func TestCustomFunction(t *testing.T) {
  var (
    url      = "http://test.com/users"
    body     = []byte("test")
    err      error
    response = &http.Response{
      StatusCode: http.StatusOK,
    }
    headers = map[string]string{
      "Content-Type": "application/json",
    }

    mockedClient = client_mock.New()
    mockedRepo = New(mockedClient)
  )

  mockedClient.ExpectPost(url, body, err, response, headers)

  result, err := mockedRepo.CustomFunction(context.Background(), "test")
  if err != nil {
    t.Error(err)
  }

  if result != http.StatusText(http.StatusOK) {
    t.Errorf("not equal responses, expect %s, go %s", http.StatusText(http.StatusOK), result)
  }

  if err := mockedClient.AllExpectationsDone(); err != nil {
    t.Error(err)
  }
}

Documentation

Index

Constants

View Source
const (
	ERR_Unexpected_Method  = "call %q expected method %q, got %q"
	ERR_Unexpected_Url     = "call %q expected url %q, got %q"
	ERR_Unexpected_Data    = "call %q expected data %v, got %v"
	ERR_Unexpected_Headers = "call %q expected headers %v, got %v"
)

Variables

This section is empty.

Functions

func GetIP

func GetIP(req *http.Request) string

Get IP address from request using X-Real-IP or X-Forwarded-For headers

func SetTimeout

func SetTimeout(val time.Duration)

Set request timeout

func Timeout

func Timeout() time.Duration

Get request timeout

Types

type Client

type Client interface {
	Post(ctx context.Context, url string, body []byte, headers map[string]string) (*http.Response, error)
	Put(ctx context.Context, url string, body []byte, headers map[string]string) (*http.Response, error)
	Patch(ctx context.Context, url string, body []byte, headers map[string]string) (*http.Response, error)
	Delete(ctx context.Context, url string, body []byte, headers map[string]string) (*http.Response, error)
	Get(ctx context.Context, url string, headers map[string]string) (*http.Response, error)

	Do(ctx context.Context, method Method, url string, body []byte, headers map[string]string) (*http.Response, error)
}

func New

func New() Client

Create new Client instance

type Expecter added in v1.1.13

type Expecter interface {
	ExpectPost(url string, body []byte, err error, response *http.Response, headers map[string]string)
	ExpectPut(url string, body []byte, err error, response *http.Response, headers map[string]string)
	ExpectPatch(url string, body []byte, err error, response *http.Response, headers map[string]string)
	ExpectDelete(url string, body []byte, err error, response *http.Response, headers map[string]string)
	ExpectGet(url string, err error, response *http.Response, headers map[string]string)
	ExpectDo(url string, method Method, body []byte, err error, response *http.Response, headers map[string]string)

	AllExpectationsDone() error
	Reset()
}

type Method added in v1.1.13

type Method string
const (
	MethodGet    Method = http.MethodGet
	MethodPost   Method = http.MethodPost
	MethodPut    Method = http.MethodPut
	MethodPatch  Method = http.MethodPatch
	MethodDelete Method = http.MethodDelete
)

type MockedClient added in v1.1.13

type MockedClient struct {
	// contains filtered or unexported fields
}

func (*MockedClient) AllExpectationsDone added in v1.1.13

func (m *MockedClient) AllExpectationsDone() error

Check if all expected calls were done

func (*MockedClient) Delete added in v1.1.13

func (m *MockedClient) Delete(ctx context.Context, url string, body []byte, headers map[string]string) (*http.Response, error)

Check if call of Post method was expected and returning expected response and error

func (*MockedClient) Do added in v1.1.13

func (m *MockedClient) Do(ctx context.Context, method Method, url string, body []byte, headers map[string]string) (*http.Response, error)

func (*MockedClient) ExpectDelete added in v1.1.13

func (m *MockedClient) ExpectDelete(url string, body []byte, err error, response *http.Response, headers map[string]string)

Store expected call of Post method with expected data and error

func (*MockedClient) ExpectDo added in v1.1.13

func (m *MockedClient) ExpectDo(url string, method Method, body []byte, err error, response *http.Response, headers map[string]string)

Store expected call of Post method with expected data and error

func (*MockedClient) ExpectGet added in v1.1.13

func (m *MockedClient) ExpectGet(url string, err error, response *http.Response, headers map[string]string)

Store expected call of Get method with expected data and error

func (*MockedClient) ExpectPatch added in v1.1.13

func (m *MockedClient) ExpectPatch(url string, body []byte, err error, response *http.Response, headers map[string]string)

Store expected call of Post method with expected data and error

func (*MockedClient) ExpectPost added in v1.1.13

func (m *MockedClient) ExpectPost(url string, body []byte, err error, response *http.Response, headers map[string]string)

Store expected call of Post method with expected data and error

func (*MockedClient) ExpectPut added in v1.1.13

func (m *MockedClient) ExpectPut(url string, body []byte, err error, response *http.Response, headers map[string]string)

Store expected call of Post method with expected data and error

func (*MockedClient) Get added in v1.1.13

func (m *MockedClient) Get(ctx context.Context, url string, headers map[string]string) (*http.Response, error)

Check if call of Get method was expected and returning expected response and error

func (*MockedClient) Patch added in v1.1.13

func (m *MockedClient) Patch(ctx context.Context, url string, body []byte, headers map[string]string) (*http.Response, error)

Check if call of Post method was expected and returning expected response and error

func (*MockedClient) Post added in v1.1.13

func (m *MockedClient) Post(ctx context.Context, url string, body []byte, headers map[string]string) (*http.Response, error)

Check if call of Post method was expected and returning expected response and error

func (*MockedClient) Put added in v1.1.13

func (m *MockedClient) Put(ctx context.Context, url string, body []byte, headers map[string]string) (*http.Response, error)

Check if call of Post method was expected and returning expected response and error

func (*MockedClient) Reset added in v1.1.13

func (m *MockedClient) Reset()

Reset all expected calls

type Mocker added in v1.1.13

type Mocker interface {
	Expecter
	Client
}

func NewMock added in v1.1.13

func NewMock() Mocker

Create new Client instance

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL