From d278d50bdb1f80da678653122f4d4746466b4be5 Mon Sep 17 00:00:00 2001 From: Tarmo Randma Date: Fri, 22 Oct 2021 12:01:11 +0300 Subject: [PATCH 1/4] Parameters are sent in request body instead of query parameters --- internal/common/util.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/internal/common/util.go b/internal/common/util.go index b25c5e5..7119394 100644 --- a/internal/common/util.go +++ b/internal/common/util.go @@ -4,13 +4,14 @@ import ( "context" "encoding/json" "fmt" - "github.com/erply/api-go-wrapper/pkg/api/common" - "github.com/erply/api-go-wrapper/pkg/api/log" "io" "io/ioutil" "net/http" "net/url" "strings" + + "github.com/erply/api-go-wrapper/pkg/api/common" + "github.com/erply/api-go-wrapper/pkg/api/log" ) type BulkInput struct { @@ -56,22 +57,24 @@ func setParams(params url.Values, filters map[string]string) { func (cli *Client) SendRequest(ctx context.Context, apiMethod string, filters map[string]string) (*http.Response, error) { log.Log.Log(log.Debug, "will call %s with filters %+v", apiMethod, filters) - req, err := getHTTPRequest(cli, nil) - if err != nil { - return nil, common.NewFromError("failed to build http request", err, 0) - } - req = req.WithContext(ctx) params := cli.headersFunc(apiMethod) log.Log.Log(log.Debug, "extracted headers %+v", params) - params, err = cli.addSessionParams(params) + params, err := cli.addSessionParams(params) if err != nil { return nil, err } setParams(params, filters) - req.URL.RawQuery = params.Encode() + req, err := getHTTPRequest(cli, strings.NewReader(params.Encode())) + if err != nil { + return nil, common.NewFromError("failed to build http request", err, 0) + } + + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req = req.WithContext(ctx) + resp, err := doRequest(req, cli) if err != nil { return nil, common.NewFromError(fmt.Sprintf("%v request failed", apiMethod), err, 0) From 820c02f1f7ab960f6cc56a3516f4237d43865de1 Mon Sep 17 00:00:00 2001 From: Tarmo Randma Date: Fri, 22 Oct 2021 12:24:27 +0300 Subject: [PATCH 2/4] Fixed unit tests --- pkg/api/addresses/addressRequests_test.go | 23 +++-- pkg/api/customers/customerRequests_test.go | 36 ++++---- pkg/api/customers/supplierRequests_test.go | 34 ++----- pkg/api/prices/requests_test.go | 101 ++++++++++++--------- pkg/api/sales/vatrateRequests_test.go | 39 ++++---- 5 files changed, 120 insertions(+), 113 deletions(-) diff --git a/pkg/api/addresses/addressRequests_test.go b/pkg/api/addresses/addressRequests_test.go index c89d155..3cac5f3 100644 --- a/pkg/api/addresses/addressRequests_test.go +++ b/pkg/api/addresses/addressRequests_test.go @@ -3,12 +3,13 @@ package addresses import ( "context" "encoding/json" - "github.com/erply/api-go-wrapper/internal/common" - sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common" - "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" + + "github.com/erply/api-go-wrapper/internal/common" + sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common" + "github.com/stretchr/testify/assert" ) func TestGetAddresses(t *testing.T) { @@ -279,12 +280,12 @@ func TestGetAddressesBulkCustomUnmarshal(t *testing.T) { { AddressID: 123, Address: "Some Address 123", - TypeID: "3", + TypeID: 3, }, { AddressID: 124, Address: "Some Address 124", - TypeID: "4", + TypeID: 4, }, }, suppliersBulk.BulkItems[0].Addresses) @@ -294,7 +295,7 @@ func TestGetAddressesBulkCustomUnmarshal(t *testing.T) { { AddressID: 125, Address: "Some Address 125", - TypeID: "5", + TypeID: 5, }, }, suppliersBulk.BulkItems[1].Addresses) assert.Equal(t, expectedStatus, suppliersBulk.BulkItems[1].Status) @@ -440,10 +441,12 @@ func TestSaveAddressesBulkResponseFailure(t *testing.T) { func TestDeleteAddresses(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) - assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) - assert.Equal(t, "deleteAddress", r.URL.Query().Get("request")) - assert.Equal(t, "2223", r.URL.Query().Get("addressID")) + common.AssertFormValues(t, r, map[string]interface{}{ + "request": "deleteAddress", + "sessionKey": "somesess", + "clientCode": "someclient", + "addressID": "2223", + }) resp := DeleteAddressResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, diff --git a/pkg/api/customers/customerRequests_test.go b/pkg/api/customers/customerRequests_test.go index 2f2d8c2..9a35b95 100644 --- a/pkg/api/customers/customerRequests_test.go +++ b/pkg/api/customers/customerRequests_test.go @@ -3,12 +3,13 @@ package customers import ( "context" "encoding/json" - "github.com/erply/api-go-wrapper/internal/common" - sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common" - "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" + + "github.com/erply/api-go-wrapper/internal/common" + sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common" + "github.com/stretchr/testify/assert" ) func TestGetCustomersBulk(t *testing.T) { @@ -133,12 +134,14 @@ func TestGetCustomersBulkResponseFailure(t *testing.T) { func TestAddCustomerRewardPoints(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) - assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) - assert.Equal(t, "addCustomerRewardPoints", r.URL.Query().Get("request")) - assert.Equal(t, "1232131", r.URL.Query().Get("customerID")) - assert.Equal(t, "34456", r.URL.Query().Get("invoiceID")) - assert.Equal(t, "11", r.URL.Query().Get("points")) + common.AssertFormValues(t, r, map[string]interface{}{ + "clientCode": "someclient", + "sessionKey": "somesess", + "request": "addCustomerRewardPoints", + "customerID": "1232131", + "invoiceID": "34456", + "points": "11", + }) resp := AddCustomerRewardPointsResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -439,18 +442,13 @@ func TestDeleteCustomer(t *testing.T) { _, err = w.Write(jsonRaw) assert.NoError(t, err) - reqItems := make(map[string]interface{}) - for key, vals := range r.URL.Query() { - reqItems[key] = vals[0] - } - - assert.Equal(t, map[string]interface{}{ - "setContentType": "1", - "request": "deleteCustomer", + common.AssertFormValues(t, r, map[string]interface{}{ + "clientCode": "someclient", "sessionKey": "somesess", + "request": "deleteCustomer", "customerID": "100000046", - "clientCode": "someclient", - }, reqItems) + "setContentType": "1", + }) })) defer srv.Close() diff --git a/pkg/api/customers/supplierRequests_test.go b/pkg/api/customers/supplierRequests_test.go index 904e704..35a4a23 100644 --- a/pkg/api/customers/supplierRequests_test.go +++ b/pkg/api/customers/supplierRequests_test.go @@ -3,12 +3,13 @@ package customers import ( "context" "encoding/json" - "github.com/erply/api-go-wrapper/internal/common" - common2 "github.com/erply/api-go-wrapper/pkg/api/common" - "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" + + "github.com/erply/api-go-wrapper/internal/common" + common2 "github.com/erply/api-go-wrapper/pkg/api/common" + "github.com/stretchr/testify/assert" ) func TestGetSuppliersBulk(t *testing.T) { @@ -343,18 +344,13 @@ func TestDeleteSupplier(t *testing.T) { _, err = w.Write(jsonRaw) assert.NoError(t, err) - reqItems := make(map[string]interface{}) - for key, vals := range r.URL.Query() { - reqItems[key] = vals[0] - } - - assert.Equal(t, map[string]interface{}{ + common.AssertFormValues(t, r, map[string]interface{}{ "setContentType": "1", "request": "deleteSupplier", "sessionKey": "somesess", "supplierID": "100000046", "clientCode": "someclient", - }, reqItems) + }) })) defer srv.Close() @@ -473,18 +469,13 @@ func TestSaveSupplierGroup(t *testing.T) { _, err = w.Write(jsonRaw) assert.NoError(t, err) - reqItems := make(map[string]interface{}) - for key, vals := range r.URL.Query() { - reqItems[key] = vals[0] - } - - assert.Equal(t, map[string]interface{}{ + common.AssertFormValues(t, r, map[string]interface{}{ "setContentType": "1", "request": "saveSupplierGroup", "sessionKey": "somesess", "name": "100000046", "clientCode": "someclient", - }, reqItems) + }) })) defer srv.Close() @@ -520,18 +511,13 @@ func TestSaveCompanyType(t *testing.T) { _, err = w.Write(jsonRaw) assert.NoError(t, err) - reqItems := make(map[string]interface{}) - for key, vals := range r.URL.Query() { - reqItems[key] = vals[0] - } - - assert.Equal(t, map[string]interface{}{ + common.AssertFormValues(t, r, map[string]interface{}{ "setContentType": "1", "request": "saveCompanyType", "sessionKey": "somesess", "name": "100000046", "clientCode": "someclient", - }, reqItems) + }) })) defer srv.Close() diff --git a/pkg/api/prices/requests_test.go b/pkg/api/prices/requests_test.go index afa6598..0f37b97 100644 --- a/pkg/api/prices/requests_test.go +++ b/pkg/api/prices/requests_test.go @@ -3,12 +3,13 @@ package prices import ( "context" "encoding/json" - "github.com/erply/api-go-wrapper/internal/common" - sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common" - "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" + + "github.com/erply/api-go-wrapper/internal/common" + sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common" + "github.com/stretchr/testify/assert" ) func TestGetSupplierPriceListsBulk(t *testing.T) { @@ -139,11 +140,13 @@ func TestAddProductToSupplierPriceList(t *testing.T) { func TestEditProductToSupplierPriceList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) - assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) - assert.Equal(t, "editProductInSupplierPriceList", r.URL.Query().Get("request")) - assert.Equal(t, "1234", r.URL.Query().Get("supplierPriceListProductID")) - assert.Equal(t, "20.23", r.URL.Query().Get("price")) + common.AssertFormValues(t, r, map[string]interface{}{ + "request": "editProductInSupplierPriceList", + "sessionKey": "somesess", + "clientCode": "someclient", + "supplierPriceListProductID": "1234", + "price": "20.23", + }) resp := ChangeProductToSupplierPriceListResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -711,11 +714,13 @@ func TestGetProductPriceListsResponseFailure(t *testing.T) { func TestDeleteProductsFromSupplierPriceList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) - assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) - assert.Equal(t, "deleteProductsFromSupplierPriceList", r.URL.Query().Get("request")) - assert.Equal(t, "2223", r.URL.Query().Get("supplierPriceListID")) - assert.Equal(t, "3444,3445", r.URL.Query().Get("supplierPriceListProductIDs")) + common.AssertFormValues(t, r, map[string]interface{}{ + "request": "deleteProductsFromSupplierPriceList", + "sessionKey": "somesess", + "clientCode": "someclient", + "supplierPriceListID": "2223", + "supplierPriceListProductIDs": "3444,3445", + }) resp := DeleteProductsFromSupplierPriceListResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -847,14 +852,16 @@ func TestDeleteProductsFromSupplierPriceListBulk(t *testing.T) { func TestSaveSupplierPriceList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) - assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) - assert.Equal(t, "saveSupplierPriceList", r.URL.Query().Get("request")) - assert.Equal(t, "Some Price Name 1", r.URL.Query().Get("name")) - assert.Equal(t, "34456", r.URL.Query().Get("supplierID")) - assert.Equal(t, "1", r.URL.Query().Get("productID")) - assert.Equal(t, "100", r.URL.Query().Get("price")) - assert.Equal(t, "10", r.URL.Query().Get("amount")) + common.AssertFormValues(t, r, map[string]interface{}{ + "request": "saveSupplierPriceList", + "sessionKey": "somesess", + "clientCode": "someclient", + "name": "Some Price Name 1", + "supplierID": "34456", + "productID": "1", + "price": "100", + "amount": "10", + }) resp := SaveSupplierPriceListResultResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -1000,12 +1007,14 @@ func TestSaveSupplierPriceListBulk(t *testing.T) { func TestSavePriceList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) - assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) - assert.Equal(t, "savePriceList", r.URL.Query().Get("request")) - assert.Equal(t, "Some Price Name 1", r.URL.Query().Get("name")) - assert.Equal(t, "34456", r.URL.Query().Get("pricelistID")) - assert.Equal(t, "BASE_PRICE_LIST", r.URL.Query().Get("type")) + common.AssertFormValues(t, r, map[string]interface{}{ + "request": "savePriceList", + "sessionKey": "somesess", + "clientCode": "someclient", + "name": "Some Price Name 1", + "pricelistID": "34456", + "type": "BASE_PRICE_LIST", + }) resp := SavePriceListResultResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -1282,12 +1291,14 @@ func TestChangeProductInPriceListBulk(t *testing.T) { func TestAddProductToPriceList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) - assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) - assert.Equal(t, "addProductToPriceList", r.URL.Query().Get("request")) - assert.Equal(t, "3333", r.URL.Query().Get("priceListID")) - assert.Equal(t, "342314", r.URL.Query().Get("productID")) - assert.Equal(t, "22.22", r.URL.Query().Get("price")) + common.AssertFormValues(t, r, map[string]interface{}{ + "request": "addProductToPriceList", + "sessionKey": "somesess", + "clientCode": "someclient", + "priceListID": "3333", + "productID": "342314", + "price": "22.22", + }) resp := ChangeProductToPriceListResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -1324,11 +1335,13 @@ func TestAddProductToPriceList(t *testing.T) { func TestEditProductInPriceList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) - assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) - assert.Equal(t, "editProductInPriceList", r.URL.Query().Get("request")) - assert.Equal(t, "1234", r.URL.Query().Get("priceListProductID")) - assert.Equal(t, "20.23", r.URL.Query().Get("price")) + common.AssertFormValues(t, r, map[string]interface{}{ + "request": "editProductInPriceList", + "sessionKey": "somesess", + "clientCode": "someclient", + "priceListProductID": "1234", + "price": "20.23", + }) resp := ChangeProductToPriceListResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -1368,11 +1381,13 @@ func TestEditProductInPriceList(t *testing.T) { func TestDeleteProductsFromPriceList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) - assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) - assert.Equal(t, "deleteProductInPriceList", r.URL.Query().Get("request")) - assert.Equal(t, "2223", r.URL.Query().Get("priceListID")) - assert.Equal(t, "3444,3445", r.URL.Query().Get("priceListProductIDs")) + common.AssertFormValues(t, r, map[string]interface{}{ + "request": "deleteProductInPriceList", + "sessionKey": "somesess", + "clientCode": "someclient", + "priceListID": "2223", + "priceListProductIDs": "3444,3445", + }) resp := DeleteProductsFromPriceListResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, diff --git a/pkg/api/sales/vatrateRequests_test.go b/pkg/api/sales/vatrateRequests_test.go index fb21e51..ba65ed8 100644 --- a/pkg/api/sales/vatrateRequests_test.go +++ b/pkg/api/sales/vatrateRequests_test.go @@ -3,23 +3,26 @@ package sales import ( "context" "encoding/json" - "github.com/erply/api-go-wrapper/internal/common" - sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common" - "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" + + "github.com/erply/api-go-wrapper/internal/common" + sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common" + "github.com/stretchr/testify/assert" ) func TestSaveVatRate(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) - assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) - assert.Equal(t, "saveVatRate", r.URL.Query().Get("request")) - assert.Equal(t, "ID123", r.URL.Query().Get("vatRateID")) - assert.Equal(t, "VatName", r.URL.Query().Get("name")) - assert.Equal(t, "0.19", r.URL.Query().Get("rate")) - assert.Equal(t, "vatCode123", r.URL.Query().Get("code")) + common.AssertFormValues(t, r, map[string]interface{}{ + "request": "saveVatRate", + "sessionKey": "somesess", + "clientCode": "someclient", + "vatRateID": "ID123", + "name": "VatName", + "rate": "0.19", + "code": "vatCode123", + }) resp := SaveVatRateResultResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -152,13 +155,15 @@ func TestSaveVatRateBulk(t *testing.T) { func TestSaveVatRateComponent(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) - assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) - assert.Equal(t, "saveVatRateComponent", r.URL.Query().Get("request")) - assert.Equal(t, "ID123", r.URL.Query().Get("vatRateComponentID")) - assert.Equal(t, "#2333", r.URL.Query().Get("vatRateID")) - assert.Equal(t, "Some name", r.URL.Query().Get("name")) - assert.Equal(t, "8.76", r.URL.Query().Get("rate")) + common.AssertFormValues(t, r, map[string]interface{}{ + "request": "saveVatRateComponent", + "sessionKey": "somesess", + "clientCode": "someclient", + "vatRateComponentID": "ID123", + "vatRateID": "#2333", + "name": "Some name", + "rate": "8.76", + }) resp := SaveVatRateComponentResultResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, From bbd7e383729b0ac1cf0e9a0d1a6a5f742b126bf0 Mon Sep 17 00:00:00 2001 From: Tarmo Randma Date: Fri, 22 Oct 2021 14:00:29 +0300 Subject: [PATCH 3/4] Revert "Fixed unit tests" This reverts commit 820c02f1 --- pkg/api/addresses/addressRequests_test.go | 23 ++--- pkg/api/customers/customerRequests_test.go | 36 ++++---- pkg/api/customers/supplierRequests_test.go | 34 +++++-- pkg/api/prices/requests_test.go | 101 +++++++++------------ pkg/api/sales/vatrateRequests_test.go | 39 ++++---- 5 files changed, 113 insertions(+), 120 deletions(-) diff --git a/pkg/api/addresses/addressRequests_test.go b/pkg/api/addresses/addressRequests_test.go index 3cac5f3..c89d155 100644 --- a/pkg/api/addresses/addressRequests_test.go +++ b/pkg/api/addresses/addressRequests_test.go @@ -3,13 +3,12 @@ package addresses import ( "context" "encoding/json" - "net/http" - "net/http/httptest" - "testing" - "github.com/erply/api-go-wrapper/internal/common" sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common" "github.com/stretchr/testify/assert" + "net/http" + "net/http/httptest" + "testing" ) func TestGetAddresses(t *testing.T) { @@ -280,12 +279,12 @@ func TestGetAddressesBulkCustomUnmarshal(t *testing.T) { { AddressID: 123, Address: "Some Address 123", - TypeID: 3, + TypeID: "3", }, { AddressID: 124, Address: "Some Address 124", - TypeID: 4, + TypeID: "4", }, }, suppliersBulk.BulkItems[0].Addresses) @@ -295,7 +294,7 @@ func TestGetAddressesBulkCustomUnmarshal(t *testing.T) { { AddressID: 125, Address: "Some Address 125", - TypeID: 5, + TypeID: "5", }, }, suppliersBulk.BulkItems[1].Addresses) assert.Equal(t, expectedStatus, suppliersBulk.BulkItems[1].Status) @@ -441,12 +440,10 @@ func TestSaveAddressesBulkResponseFailure(t *testing.T) { func TestDeleteAddresses(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - common.AssertFormValues(t, r, map[string]interface{}{ - "request": "deleteAddress", - "sessionKey": "somesess", - "clientCode": "someclient", - "addressID": "2223", - }) + assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) + assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) + assert.Equal(t, "deleteAddress", r.URL.Query().Get("request")) + assert.Equal(t, "2223", r.URL.Query().Get("addressID")) resp := DeleteAddressResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, diff --git a/pkg/api/customers/customerRequests_test.go b/pkg/api/customers/customerRequests_test.go index 9a35b95..2f2d8c2 100644 --- a/pkg/api/customers/customerRequests_test.go +++ b/pkg/api/customers/customerRequests_test.go @@ -3,13 +3,12 @@ package customers import ( "context" "encoding/json" - "net/http" - "net/http/httptest" - "testing" - "github.com/erply/api-go-wrapper/internal/common" sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common" "github.com/stretchr/testify/assert" + "net/http" + "net/http/httptest" + "testing" ) func TestGetCustomersBulk(t *testing.T) { @@ -134,14 +133,12 @@ func TestGetCustomersBulkResponseFailure(t *testing.T) { func TestAddCustomerRewardPoints(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - common.AssertFormValues(t, r, map[string]interface{}{ - "clientCode": "someclient", - "sessionKey": "somesess", - "request": "addCustomerRewardPoints", - "customerID": "1232131", - "invoiceID": "34456", - "points": "11", - }) + assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) + assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) + assert.Equal(t, "addCustomerRewardPoints", r.URL.Query().Get("request")) + assert.Equal(t, "1232131", r.URL.Query().Get("customerID")) + assert.Equal(t, "34456", r.URL.Query().Get("invoiceID")) + assert.Equal(t, "11", r.URL.Query().Get("points")) resp := AddCustomerRewardPointsResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -442,13 +439,18 @@ func TestDeleteCustomer(t *testing.T) { _, err = w.Write(jsonRaw) assert.NoError(t, err) - common.AssertFormValues(t, r, map[string]interface{}{ - "clientCode": "someclient", - "sessionKey": "somesess", + reqItems := make(map[string]interface{}) + for key, vals := range r.URL.Query() { + reqItems[key] = vals[0] + } + + assert.Equal(t, map[string]interface{}{ + "setContentType": "1", "request": "deleteCustomer", + "sessionKey": "somesess", "customerID": "100000046", - "setContentType": "1", - }) + "clientCode": "someclient", + }, reqItems) })) defer srv.Close() diff --git a/pkg/api/customers/supplierRequests_test.go b/pkg/api/customers/supplierRequests_test.go index 35a4a23..904e704 100644 --- a/pkg/api/customers/supplierRequests_test.go +++ b/pkg/api/customers/supplierRequests_test.go @@ -3,13 +3,12 @@ package customers import ( "context" "encoding/json" - "net/http" - "net/http/httptest" - "testing" - "github.com/erply/api-go-wrapper/internal/common" common2 "github.com/erply/api-go-wrapper/pkg/api/common" "github.com/stretchr/testify/assert" + "net/http" + "net/http/httptest" + "testing" ) func TestGetSuppliersBulk(t *testing.T) { @@ -344,13 +343,18 @@ func TestDeleteSupplier(t *testing.T) { _, err = w.Write(jsonRaw) assert.NoError(t, err) - common.AssertFormValues(t, r, map[string]interface{}{ + reqItems := make(map[string]interface{}) + for key, vals := range r.URL.Query() { + reqItems[key] = vals[0] + } + + assert.Equal(t, map[string]interface{}{ "setContentType": "1", "request": "deleteSupplier", "sessionKey": "somesess", "supplierID": "100000046", "clientCode": "someclient", - }) + }, reqItems) })) defer srv.Close() @@ -469,13 +473,18 @@ func TestSaveSupplierGroup(t *testing.T) { _, err = w.Write(jsonRaw) assert.NoError(t, err) - common.AssertFormValues(t, r, map[string]interface{}{ + reqItems := make(map[string]interface{}) + for key, vals := range r.URL.Query() { + reqItems[key] = vals[0] + } + + assert.Equal(t, map[string]interface{}{ "setContentType": "1", "request": "saveSupplierGroup", "sessionKey": "somesess", "name": "100000046", "clientCode": "someclient", - }) + }, reqItems) })) defer srv.Close() @@ -511,13 +520,18 @@ func TestSaveCompanyType(t *testing.T) { _, err = w.Write(jsonRaw) assert.NoError(t, err) - common.AssertFormValues(t, r, map[string]interface{}{ + reqItems := make(map[string]interface{}) + for key, vals := range r.URL.Query() { + reqItems[key] = vals[0] + } + + assert.Equal(t, map[string]interface{}{ "setContentType": "1", "request": "saveCompanyType", "sessionKey": "somesess", "name": "100000046", "clientCode": "someclient", - }) + }, reqItems) })) defer srv.Close() diff --git a/pkg/api/prices/requests_test.go b/pkg/api/prices/requests_test.go index 0f37b97..afa6598 100644 --- a/pkg/api/prices/requests_test.go +++ b/pkg/api/prices/requests_test.go @@ -3,13 +3,12 @@ package prices import ( "context" "encoding/json" - "net/http" - "net/http/httptest" - "testing" - "github.com/erply/api-go-wrapper/internal/common" sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common" "github.com/stretchr/testify/assert" + "net/http" + "net/http/httptest" + "testing" ) func TestGetSupplierPriceListsBulk(t *testing.T) { @@ -140,13 +139,11 @@ func TestAddProductToSupplierPriceList(t *testing.T) { func TestEditProductToSupplierPriceList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - common.AssertFormValues(t, r, map[string]interface{}{ - "request": "editProductInSupplierPriceList", - "sessionKey": "somesess", - "clientCode": "someclient", - "supplierPriceListProductID": "1234", - "price": "20.23", - }) + assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) + assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) + assert.Equal(t, "editProductInSupplierPriceList", r.URL.Query().Get("request")) + assert.Equal(t, "1234", r.URL.Query().Get("supplierPriceListProductID")) + assert.Equal(t, "20.23", r.URL.Query().Get("price")) resp := ChangeProductToSupplierPriceListResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -714,13 +711,11 @@ func TestGetProductPriceListsResponseFailure(t *testing.T) { func TestDeleteProductsFromSupplierPriceList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - common.AssertFormValues(t, r, map[string]interface{}{ - "request": "deleteProductsFromSupplierPriceList", - "sessionKey": "somesess", - "clientCode": "someclient", - "supplierPriceListID": "2223", - "supplierPriceListProductIDs": "3444,3445", - }) + assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) + assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) + assert.Equal(t, "deleteProductsFromSupplierPriceList", r.URL.Query().Get("request")) + assert.Equal(t, "2223", r.URL.Query().Get("supplierPriceListID")) + assert.Equal(t, "3444,3445", r.URL.Query().Get("supplierPriceListProductIDs")) resp := DeleteProductsFromSupplierPriceListResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -852,16 +847,14 @@ func TestDeleteProductsFromSupplierPriceListBulk(t *testing.T) { func TestSaveSupplierPriceList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - common.AssertFormValues(t, r, map[string]interface{}{ - "request": "saveSupplierPriceList", - "sessionKey": "somesess", - "clientCode": "someclient", - "name": "Some Price Name 1", - "supplierID": "34456", - "productID": "1", - "price": "100", - "amount": "10", - }) + assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) + assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) + assert.Equal(t, "saveSupplierPriceList", r.URL.Query().Get("request")) + assert.Equal(t, "Some Price Name 1", r.URL.Query().Get("name")) + assert.Equal(t, "34456", r.URL.Query().Get("supplierID")) + assert.Equal(t, "1", r.URL.Query().Get("productID")) + assert.Equal(t, "100", r.URL.Query().Get("price")) + assert.Equal(t, "10", r.URL.Query().Get("amount")) resp := SaveSupplierPriceListResultResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -1007,14 +1000,12 @@ func TestSaveSupplierPriceListBulk(t *testing.T) { func TestSavePriceList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - common.AssertFormValues(t, r, map[string]interface{}{ - "request": "savePriceList", - "sessionKey": "somesess", - "clientCode": "someclient", - "name": "Some Price Name 1", - "pricelistID": "34456", - "type": "BASE_PRICE_LIST", - }) + assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) + assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) + assert.Equal(t, "savePriceList", r.URL.Query().Get("request")) + assert.Equal(t, "Some Price Name 1", r.URL.Query().Get("name")) + assert.Equal(t, "34456", r.URL.Query().Get("pricelistID")) + assert.Equal(t, "BASE_PRICE_LIST", r.URL.Query().Get("type")) resp := SavePriceListResultResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -1291,14 +1282,12 @@ func TestChangeProductInPriceListBulk(t *testing.T) { func TestAddProductToPriceList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - common.AssertFormValues(t, r, map[string]interface{}{ - "request": "addProductToPriceList", - "sessionKey": "somesess", - "clientCode": "someclient", - "priceListID": "3333", - "productID": "342314", - "price": "22.22", - }) + assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) + assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) + assert.Equal(t, "addProductToPriceList", r.URL.Query().Get("request")) + assert.Equal(t, "3333", r.URL.Query().Get("priceListID")) + assert.Equal(t, "342314", r.URL.Query().Get("productID")) + assert.Equal(t, "22.22", r.URL.Query().Get("price")) resp := ChangeProductToPriceListResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -1335,13 +1324,11 @@ func TestAddProductToPriceList(t *testing.T) { func TestEditProductInPriceList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - common.AssertFormValues(t, r, map[string]interface{}{ - "request": "editProductInPriceList", - "sessionKey": "somesess", - "clientCode": "someclient", - "priceListProductID": "1234", - "price": "20.23", - }) + assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) + assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) + assert.Equal(t, "editProductInPriceList", r.URL.Query().Get("request")) + assert.Equal(t, "1234", r.URL.Query().Get("priceListProductID")) + assert.Equal(t, "20.23", r.URL.Query().Get("price")) resp := ChangeProductToPriceListResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -1381,13 +1368,11 @@ func TestEditProductInPriceList(t *testing.T) { func TestDeleteProductsFromPriceList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - common.AssertFormValues(t, r, map[string]interface{}{ - "request": "deleteProductInPriceList", - "sessionKey": "somesess", - "clientCode": "someclient", - "priceListID": "2223", - "priceListProductIDs": "3444,3445", - }) + assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) + assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) + assert.Equal(t, "deleteProductInPriceList", r.URL.Query().Get("request")) + assert.Equal(t, "2223", r.URL.Query().Get("priceListID")) + assert.Equal(t, "3444,3445", r.URL.Query().Get("priceListProductIDs")) resp := DeleteProductsFromPriceListResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, diff --git a/pkg/api/sales/vatrateRequests_test.go b/pkg/api/sales/vatrateRequests_test.go index ba65ed8..fb21e51 100644 --- a/pkg/api/sales/vatrateRequests_test.go +++ b/pkg/api/sales/vatrateRequests_test.go @@ -3,26 +3,23 @@ package sales import ( "context" "encoding/json" - "net/http" - "net/http/httptest" - "testing" - "github.com/erply/api-go-wrapper/internal/common" sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common" "github.com/stretchr/testify/assert" + "net/http" + "net/http/httptest" + "testing" ) func TestSaveVatRate(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - common.AssertFormValues(t, r, map[string]interface{}{ - "request": "saveVatRate", - "sessionKey": "somesess", - "clientCode": "someclient", - "vatRateID": "ID123", - "name": "VatName", - "rate": "0.19", - "code": "vatCode123", - }) + assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) + assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) + assert.Equal(t, "saveVatRate", r.URL.Query().Get("request")) + assert.Equal(t, "ID123", r.URL.Query().Get("vatRateID")) + assert.Equal(t, "VatName", r.URL.Query().Get("name")) + assert.Equal(t, "0.19", r.URL.Query().Get("rate")) + assert.Equal(t, "vatCode123", r.URL.Query().Get("code")) resp := SaveVatRateResultResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, @@ -155,15 +152,13 @@ func TestSaveVatRateBulk(t *testing.T) { func TestSaveVatRateComponent(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - common.AssertFormValues(t, r, map[string]interface{}{ - "request": "saveVatRateComponent", - "sessionKey": "somesess", - "clientCode": "someclient", - "vatRateComponentID": "ID123", - "vatRateID": "#2333", - "name": "Some name", - "rate": "8.76", - }) + assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) + assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) + assert.Equal(t, "saveVatRateComponent", r.URL.Query().Get("request")) + assert.Equal(t, "ID123", r.URL.Query().Get("vatRateComponentID")) + assert.Equal(t, "#2333", r.URL.Query().Get("vatRateID")) + assert.Equal(t, "Some name", r.URL.Query().Get("name")) + assert.Equal(t, "8.76", r.URL.Query().Get("rate")) resp := SaveVatRateComponentResultResponse{ Status: sharedCommon.Status{ResponseStatus: "ok"}, From 1a9c6c48e22635e2cb0af3c4bbdb08c584b15d5e Mon Sep 17 00:00:00 2001 From: Tarmo Randma Date: Fri, 22 Oct 2021 14:27:15 +0300 Subject: [PATCH 4/4] Request body used only based on flag --- internal/common/client.go | 20 ++++-- internal/common/util.go | 18 +++-- internal/common/util_test.go | 82 ++++++++++++++++++++++- pkg/api/addresses/addressRequests_test.go | 13 ++-- pkg/api/client.go | 18 +++-- 5 files changed, 128 insertions(+), 23 deletions(-) diff --git a/internal/common/client.go b/internal/common/client.go index 01c3beb..92cf11f 100644 --- a/internal/common/client.go +++ b/internal/common/client.go @@ -122,12 +122,20 @@ func (dsp *DefaultSessionProvider) Invalidate() { } type Client struct { - Url string - httpClient *http.Client - clientCode string - partnerKey string - headersFunc AuthFunc - sessionProvider SessionProvider + Url string + httpClient *http.Client + clientCode string + partnerKey string + headersFunc AuthFunc + sessionProvider SessionProvider + sendParametersInRequestBody bool +} + +//SendParametersInRequestBody indicates to the client that the request should add the data payload in the +//request body instead of using the query parameters. Using the request body eliminates the query size +//limitations imposed by the maximum URL length +func (cli *Client) SendParametersInRequestBody() { + cli.sendParametersInRequestBody = true } func (cli *Client) Close() { diff --git a/internal/common/util.go b/internal/common/util.go index 7119394..ca00050 100644 --- a/internal/common/util.go +++ b/internal/common/util.go @@ -67,12 +67,20 @@ func (cli *Client) SendRequest(ctx context.Context, apiMethod string, filters ma setParams(params, filters) - req, err := getHTTPRequest(cli, strings.NewReader(params.Encode())) - if err != nil { - return nil, common.NewFromError("failed to build http request", err, 0) + var req *http.Request + if cli.sendParametersInRequestBody { + req, err = getHTTPRequest(cli, strings.NewReader(params.Encode())) + if err != nil { + return nil, common.NewFromError("failed to build http request", err, 0) + } + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + } else { + req, err = getHTTPRequest(cli, nil) + if err != nil { + return nil, common.NewFromError("failed to build http request", err, 0) + } + req.URL.RawQuery = params.Encode() } - - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req = req.WithContext(ctx) resp, err := doRequest(req, cli) diff --git a/internal/common/util_test.go b/internal/common/util_test.go index bc5cbba..fcc14cf 100644 --- a/internal/common/util_test.go +++ b/internal/common/util_test.go @@ -2,13 +2,93 @@ package common import ( "context" - "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" "time" + + "github.com/stretchr/testify/assert" ) +func TestSendRequestInBody(t *testing.T) { + calledTimes := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calledTimes++ + assert.Equal(t, "", r.URL.Query().Get("clientCode")) + assert.Equal(t, "", r.URL.Query().Get("sessionKey")) + assert.Equal(t, "", r.URL.Query().Get("someKey")) + AssertFormValues(t, r, map[string]interface{}{ + "clientCode": "someclient", + "sessionKey": "somesess", + "someKey": "someValue", + }) + })) + defer srv.Close() + + cli := NewClientWithURL( + "somesess", + "someclient", + "", + srv.URL, + &http.Client{ + Timeout: 5 * time.Second, + }, + nil, + ) + cli.SendParametersInRequestBody() + + resp, err := cli.SendRequest( + context.Background(), + "getSuppliers", + map[string]string{"someKey": "someValue"}, + ) + assert.NoError(t, err) + if err != nil { + return + } + assert.Equal(t, 200, resp.StatusCode) + assert.Equal(t, 1, calledTimes) +} + +func TestSendRequestInQuery(t *testing.T) { + calledTimes := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calledTimes++ + assert.Equal(t, "someclient", r.URL.Query().Get("clientCode")) + assert.Equal(t, "somesess", r.URL.Query().Get("sessionKey")) + assert.Equal(t, "someValue", r.URL.Query().Get("someKey")) + AssertFormValues(t, r, map[string]interface{}{ + "clientCode": "someclient", + "sessionKey": "somesess", + "someKey": "someValue", + }) + })) + defer srv.Close() + + cli := NewClientWithURL( + "somesess", + "someclient", + "", + srv.URL, + &http.Client{ + Timeout: 5 * time.Second, + }, + nil, + ) + + resp, err := cli.SendRequest( + context.Background(), + "getSuppliers", + map[string]string{"someKey": "someValue"}, + ) + assert.NoError(t, err) + if err != nil { + return + } + assert.Equal(t, 200, resp.StatusCode) + assert.Equal(t, 1, calledTimes) +} + func TestSendRequestBulk(t *testing.T) { calledTimes := 0 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/pkg/api/addresses/addressRequests_test.go b/pkg/api/addresses/addressRequests_test.go index c89d155..0f31551 100644 --- a/pkg/api/addresses/addressRequests_test.go +++ b/pkg/api/addresses/addressRequests_test.go @@ -3,12 +3,13 @@ package addresses import ( "context" "encoding/json" - "github.com/erply/api-go-wrapper/internal/common" - sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common" - "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" + + "github.com/erply/api-go-wrapper/internal/common" + sharedCommon "github.com/erply/api-go-wrapper/pkg/api/common" + "github.com/stretchr/testify/assert" ) func TestGetAddresses(t *testing.T) { @@ -279,12 +280,12 @@ func TestGetAddressesBulkCustomUnmarshal(t *testing.T) { { AddressID: 123, Address: "Some Address 123", - TypeID: "3", + TypeID: 3, }, { AddressID: 124, Address: "Some Address 124", - TypeID: "4", + TypeID: 4, }, }, suppliersBulk.BulkItems[0].Addresses) @@ -294,7 +295,7 @@ func TestGetAddressesBulkCustomUnmarshal(t *testing.T) { { AddressID: 125, Address: "Some Address 125", - TypeID: "5", + TypeID: 5, }, }, suppliersBulk.BulkItems[1].Addresses) assert.Equal(t, expectedStatus, suppliersBulk.BulkItems[1].Status) diff --git a/pkg/api/client.go b/pkg/api/client.go index 75cec73..cb60c4d 100644 --- a/pkg/api/client.go +++ b/pkg/api/client.go @@ -4,6 +4,12 @@ import ( "encoding/json" "errors" "fmt" + "net/http" + "net/url" + "strconv" + "sync" + "time" + "github.com/erply/api-go-wrapper/internal/common" "github.com/erply/api-go-wrapper/pkg/api/addresses" "github.com/erply/api-go-wrapper/pkg/api/auth" @@ -18,11 +24,6 @@ import ( "github.com/erply/api-go-wrapper/pkg/api/sales" "github.com/erply/api-go-wrapper/pkg/api/servicediscovery" "github.com/erply/api-go-wrapper/pkg/api/warehouse" - "net/http" - "net/url" - "strconv" - "sync" - "time" ) type Client struct { @@ -59,6 +60,13 @@ func (cl *Client) GetSession() (sessionKey string, err error) { return cl.commonClient.GetSession() } +//SendParametersInRequestBody indicates to the client that the request should add the data payload in the +//request body instead of using the query parameters. Using the request body eliminates the query size +//limitations imposed by the maximum URL length +func (cl *Client) SendParametersInRequestBody() { + cl.commonClient.SendParametersInRequestBody() +} + //NewUnvalidatedClient returns a new Client without validating any of the incoming parameters giving the //developer more flexibility func NewUnvalidatedClient(sk, cc, partnerKey string, httpCli *http.Client) *Client {