github.com/Kong/go-pdk@v0.11.0/client/client_test.go (about)

     1  package client
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/Kong/go-pdk/bridge"
     7  	"github.com/Kong/go-pdk/bridge/bridgetest"
     8  	"github.com/Kong/go-pdk/entities"
     9  	"github.com/Kong/go-pdk/server/kong_plugin_protocol"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func mockClient(t *testing.T, s []bridgetest.MockStep) Client {
    14  	return Client{bridge.New(bridgetest.Mock(t, s))}
    15  }
    16  
    17  func TestGetIp(t *testing.T) {
    18  	c := mockClient(t, []bridgetest.MockStep{
    19  		{"kong.client.get_ip", nil, bridge.WrapString("10.10.10.1")},
    20  	})
    21  
    22  	resp, err := c.GetIp()
    23  	assert.NoError(t, err)
    24  	assert.Equal(t, resp, "10.10.10.1")
    25  }
    26  
    27  func TestGetForwardedIp(t *testing.T) {
    28  	c := mockClient(t, []bridgetest.MockStep{
    29  		{"kong.client.get_forwarded_ip", nil, bridge.WrapString("10.10.10.1")},
    30  	})
    31  
    32  	resp, err := c.GetForwardedIp()
    33  	assert.NoError(t, err)
    34  	assert.Equal(t, resp, "10.10.10.1")
    35  }
    36  
    37  func TestGetPort(t *testing.T) {
    38  	c := mockClient(t, []bridgetest.MockStep{
    39  		{"kong.client.get_port", nil, &kong_plugin_protocol.Int{V: 443}},
    40  	})
    41  	resp, err := c.GetPort()
    42  	assert.NoError(t, err)
    43  	assert.Equal(t, 443, resp)
    44  }
    45  
    46  func TestGetForwardedPort(t *testing.T) {
    47  	c := mockClient(t, []bridgetest.MockStep{
    48  		{"kong.client.get_forwarded_port", nil, &kong_plugin_protocol.Int{V: 80}},
    49  	})
    50  	resp, err := c.GetForwardedPort()
    51  	assert.NoError(t, err)
    52  	assert.Equal(t, 80, resp)
    53  }
    54  
    55  func TestGetCredential(t *testing.T) {
    56  	c := mockClient(t, []bridgetest.MockStep{
    57  		{"kong.client.get_credential", nil,
    58  			&kong_plugin_protocol.AuthenticatedCredential{Id: "000:00", ConsumerId: "000:01"},
    59  		},
    60  	})
    61  
    62  	resp, err := c.GetCredential()
    63  	assert.NoError(t, err)
    64  	assert.Equal(t, AuthenticatedCredential{Id: "000:00", ConsumerId: "000:01"}, resp)
    65  }
    66  
    67  func TestLoadConsumer(t *testing.T) {
    68  	c := mockClient(t, []bridgetest.MockStep{
    69  		{"kong.client.load_consumer",
    70  			&kong_plugin_protocol.ConsumerSpec{Id: "001", ByUsername: false},
    71  			&kong_plugin_protocol.Consumer{Id: "001", Username: "Jon Doe"},
    72  		},
    73  	})
    74  	resp, err := c.LoadConsumer("001", false)
    75  	assert.NoError(t, err)
    76  	assert.Equal(t, entities.Consumer{Id: "001", Username: "Jon Doe"}, resp)
    77  }
    78  
    79  func TestAuthenticate(t *testing.T) {
    80  	var consumer *entities.Consumer = &entities.Consumer{Id: "001", Username: "Jon Doe"}
    81  	var credential *AuthenticatedCredential = &AuthenticatedCredential{Id: "000:00", ConsumerId: "001"}
    82  
    83  	c := mockClient(t, []bridgetest.MockStep{
    84  		{Method: "kong.client.authenticate",
    85  			Args: &kong_plugin_protocol.AuthenticateArgs{
    86  				Consumer: &kong_plugin_protocol.Consumer{
    87  					Id:        consumer.Id,
    88  					CreatedAt: int64(consumer.CreatedAt),
    89  					Username:  consumer.Username,
    90  					CustomId:  consumer.CustomId,
    91  					Tags:      consumer.Tags,
    92  				},
    93  				Credential: &kong_plugin_protocol.AuthenticatedCredential{
    94  					Id:         credential.Id,
    95  					ConsumerId: credential.ConsumerId,
    96  				},
    97  			},
    98  			Ret: nil,
    99  		},
   100  	})
   101  
   102  	err := c.Authenticate(consumer, credential)
   103  
   104  	assert.NoError(t, err)
   105  }
   106  
   107  func TestAuthenticateNil(t *testing.T) {
   108  	var consumer *entities.Consumer = nil
   109  	var credential *AuthenticatedCredential = nil
   110  
   111  	c := mockClient(t, nil)
   112  
   113  	err := c.Authenticate(consumer, credential)
   114  
   115  	assert.EqualError(t, err, "either credential or consumer must be provided")
   116  }
   117  
   118  /*
   119  func TestGetConsumer(t *testing.T) {
   120  	assert.Equal(t, bridge.StepData{Method: "kong.client.get_consumer"}, getBack(func() { client.GetConsumer() }))
   121  }
   122  
   123  func TestGetProtocol(t *testing.T) {
   124  	assert.Equal(t, bridge.StepData{Method: "kong.client.get_protocol", Args: []interface{}{true}}, getBack(func() { client.GetProtocol(true) }))
   125  	assert.Equal(t, bridge.StepData{Method: "kong.client.get_protocol", Args: []interface{}{false}}, getBack(func() { client.GetProtocol(false) }))
   126  }
   127  */