func TestClient_GetActor(t *testing.T) {
	actorIRI := testutil.MustParseURL("https://example.com/services/service1")
	actorBytes, e := json.Marshal(aptestutil.NewMockService(actorIRI))
	require.NoError(t, e)
	t.Run("Success", func(t *testing.T) {
		httpClient := &mocks.HTTPTransport{}
		rw := httptest.NewRecorder()
		_, err := rw.Write(actorBytes)
		require.NoError(t, err)
		result := rw.Result()
		httpClient.GetReturns(result, nil)
		c := New(Config{}, httpClient)
		require.NotNil(t, t, c)
		actor, e := c.GetActor(actorIRI)
		require.NoError(t, e)
		require.NotNil(t, actor)
		require.Equal(t, actorIRI.String(), actor.ID().String())
		require.NoError(t, result.Body.Close())
	})
	t.Run("Error status code", func(t *testing.T) {
		httpClient := &mocks.HTTPTransport{}
		rw := httptest.NewRecorder()
		rw.Code = http.StatusInternalServerError
		result := rw.Result()
		httpClient.GetReturns(result, nil)
		c := New(Config{}, httpClient)
		require.NotNil(t, t, c)
		actor, e := c.GetActor(actorIRI)
		require.Error(t, e)
		require.Nil(t, actor)
		require.Contains(t, e.Error(), "status code 500")
		require.NoError(t, result.Body.Close())
	})
	t.Run("HTTP client error", func(t *testing.T) {
		errExpected := fmt.Errorf("injected HTTP client error")
		httpClient := &mocks.HTTPTransport{}
		httpClient.GetReturns(nil, errExpected)
		c := New(Config{}, httpClient)
		require.NotNil(t, t, c)
		actor, e := c.GetActor(actorIRI)
		require.Error(t, e)
		require.Contains(t, e.Error(), errExpected.Error())
		require.Nil(t, actor)
	})
	t.Run("Unmarshal client error", func(t *testing.T) {
		rw := httptest.NewRecorder()
		_, err := rw.Write([]byte("{"))
		require.NoError(t, err)
		httpClient := &mocks.HTTPTransport{}
		result := rw.Result()
		httpClient.GetReturns(result, nil)
		c := New(Config{}, httpClient)
		require.NotNil(t, t, c)
		actor, err := c.GetActor(actorIRI)
		require.Error(t, err)
		require.Contains(t, err.Error(), "unexpected end of JSON input")
		require.Nil(t, actor)
		require.NoError(t, result.Body.Close())
	})
	t.Run("Cache expiry", func(t *testing.T) {
		errExpected := errors.New("not found")
		t.Run("Cached", func(t *testing.T) {
			rw := httptest.NewRecorder()
			_, err := rw.Write(actorBytes)
			require.NoError(t, err)
			result := rw.Result()
			httpClient := &mocks.HTTPTransport{}
			httpClient.GetReturnsOnCall(0, result, nil)
			httpClient.GetReturnsOnCall(1, nil, errExpected)
			c := New(Config{
				CacheExpiration: time.Second,
			}, httpClient)
			require.NotNil(t, t, c)
			actor, e := c.GetActor(actorIRI)
			require.NoError(t, e)
			require.NotNil(t, actor)
			require.Equal(t, actorIRI.String(), actor.ID().String())
			actor, e = c.GetActor(actorIRI)
			require.NoError(t, e)
			require.NotNil(t, actor)
			require.Equal(t, actorIRI.String(), actor.ID().String())
			require.NoError(t, result.Body.Close())
		})
		t.Run("Item expired", func(t *testing.T) {
			rw := httptest.NewRecorder()
			_, err := rw.Write(actorBytes)
			require.NoError(t, err)
			result := rw.Result()
			httpClient := &mocks.HTTPTransport{}
			httpClient.GetReturnsOnCall(0, result, nil)
			httpClient.GetReturnsOnCall(1, nil, errExpected)
			c := New(Config{
				CacheExpiration: time.Nanosecond,
			}, httpClient)
			require.NotNil(t, t, c)
			actor, e := c.GetActor(actorIRI)
			require.NoError(t, e)
			require.NotNil(t, actor)
			require.Equal(t, actorIRI.String(), actor.ID().String())
			actor, e = c.GetActor(actorIRI)
			require.Error(t, e)
			require.Nil(t, actor)
			require.True(t, errors.Is(e, errExpected))
			require.NoError(t, result.Body.Close())
		})
	})
}
go:generate counterfeiter -o ../mocks/httptransport.gen.go --fake-name HTTPTransport . httpTransport
package client
import (
	"encoding/json"
	"errors"
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"
	"time"
	"github.com/stretchr/testify/require"
	"github.com/trustbloc/edge-core/pkg/log"
	"github.com/trustbloc/orb/pkg/activitypub/mocks"
	"github.com/trustbloc/orb/pkg/activitypub/vocab"
	"github.com/trustbloc/orb/pkg/internal/aptestutil"
	"github.com/trustbloc/orb/pkg/internal/testutil"
)