github.com/Ingenico-ePayments/connect-sdk-go@v0.0.0-20240318153750-1f8cd329b9c9/communicator/Communicator_test.go (about)

     1  package communicator
     2  
     3  import (
     4  	"io"
     5  	"net/url"
     6  	"testing"
     7  )
     8  
     9  var baseURL = url.URL{
    10  	Scheme: "https",
    11  	Host:   "eu.sandbox.api-ingenico.com",
    12  }
    13  
    14  var marshaller = &communicatorTestMarshaller{}
    15  
    16  type communicatorTestMarshaller struct {
    17  }
    18  
    19  // Marshal encodes the given value using json.Marshal
    20  func (m *communicatorTestMarshaller) Marshal(v interface{}) (string, error) {
    21  	panic("Not implemented")
    22  }
    23  
    24  func (m *communicatorTestMarshaller) UnmarshalFromReader(reader io.Reader, v interface{}) error {
    25  	panic("Not implemented")
    26  }
    27  
    28  // Unmarshal decodes the given data into the given value using json.Unmarshal
    29  func (m *communicatorTestMarshaller) Unmarshal(data string, v interface{}) error {
    30  	panic("Not implemented")
    31  }
    32  
    33  var mockSession = &Session{
    34  	apiEndpoint: &baseURL,
    35  }
    36  
    37  func TestToURIWithoutRequestParams(t *testing.T) {
    38  	communicator, err := NewCommunicator(mockSession, marshaller)
    39  	if err != nil {
    40  		t.Fatalf("TestToURIWithoutRequestParams: %v", err)
    41  	}
    42  
    43  	expectedURL, err := url.Parse("https://eu.sandbox.api-ingenico.com/v1/merchant/20000/convertamount")
    44  	if err != nil {
    45  		t.Fatalf("TestToURIWithoutRequestParams: %v", err)
    46  	}
    47  
    48  	url1, err := communicator.toAbsoluteURI("v1/merchant/20000/convertamount", RequestParams{})
    49  	if err != nil {
    50  		t.Fatalf("TestToURIWithoutRequestParams: %v", err)
    51  	}
    52  	if url1 != *expectedURL {
    53  		t.Fatalf("TestToURIWithoutRequestParams: url mismatch '%v' '%v'", url1, *expectedURL)
    54  	}
    55  
    56  	url2, err := communicator.toAbsoluteURI("/v1/merchant/20000/convertamount", RequestParams{})
    57  	if err != nil {
    58  		t.Fatalf("TestToURIWithoutRequestParams: %v", err)
    59  	}
    60  	if url2 != *expectedURL {
    61  		t.Fatalf("TestToURIWithoutRequestParams: url mismatch '%v' '%v'", url2, *expectedURL)
    62  	}
    63  }
    64  
    65  func TestToURIWithRequestParams(t *testing.T) {
    66  	params := append(RequestParams{},
    67  		RequestParam{"amount", "123"},
    68  		RequestParam{"source", "USD"},
    69  		RequestParam{"target", "EUR"},
    70  		RequestParam{"dummy", "é&%="})
    71  
    72  	communicator, err := NewCommunicator(mockSession, marshaller)
    73  	if err != nil {
    74  		t.Fatalf("TestToURIWithoutRequestParams: %v", err)
    75  	}
    76  
    77  	expectedURL, err := url.Parse("https://eu.sandbox.api-ingenico.com/v1/merchant/20000/convertamount?amount=123&source=USD&target=EUR&dummy=%C3%A9%26%25%3D")
    78  	if err != nil {
    79  		t.Fatalf("TestToURIWithoutRequestParams: %v", err)
    80  	}
    81  
    82  	url1, err := communicator.toAbsoluteURI("v1/merchant/20000/convertamount", params)
    83  	if err != nil {
    84  		t.Fatalf("TestToURIWithoutRequestParams: %v", err)
    85  	}
    86  	if url1 != *expectedURL {
    87  		t.Fatalf("TestToURIWithoutRequestParams: url mismatch '%v' '%v'", url1, *expectedURL)
    88  	}
    89  
    90  	url2, err := communicator.toAbsoluteURI("/v1/merchant/20000/convertamount", params)
    91  	if err != nil {
    92  		t.Fatalf("TestToURIWithoutRequestParams: %v", err)
    93  	}
    94  	if url2 != *expectedURL {
    95  		t.Fatalf("TestToURIWithoutRequestParams: url mismatch '%v' '%v'", url2, *expectedURL)
    96  	}
    97  }