Skip to content

Commit

Permalink
fix(bulk): regression on bulk endpoint using content type application…
Browse files Browse the repository at this point in the history
…/json (#597)
  • Loading branch information
gfyrag authored Dec 9, 2024
1 parent 04a2d46 commit 2e3bb90
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/api/v2/controllers_bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"github.com/formancehq/ledger/internal/api/bulking"
"net/http"
"strings"

"github.com/formancehq/go-libs/v2/api"
"github.com/formancehq/ledger/internal/api/common"
Expand All @@ -16,6 +17,10 @@ func bulkHandler(bulkerFactory bulking.BulkerFactory, bulkHandlerFactories map[s
if contentType == "" {
contentType = "application/json"
}
if strings.Index(contentType, ";") > 0 {
contentType = strings.Split(contentType, ";")[0]
}

bulkHandlerFactory, ok := bulkHandlerFactories[contentType]
if !ok {
api.BadRequest(w, common.ErrValidation, errors.New("unsupported content type: "+contentType))
Expand Down
62 changes: 62 additions & 0 deletions internal/api/v2/controllers_bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestBulk(t *testing.T) {
expectations func(mockLedger *LedgerController)
expectError bool
expectResults []bulking.APIResult
headers http.Header
}

testCases := []bulkTestCase{
Expand Down Expand Up @@ -417,6 +418,65 @@ func TestBulk(t *testing.T) {
ResponseType: bulking.ActionAddMetadata,
}},
},
{
name: "with custom content type",
headers: map[string][]string{
"Content-Type": {"application/json; charset=utf-8"},
},
body: fmt.Sprintf(`[{
"action": "CREATE_TRANSACTION",
"data": {
"postings": [{
"source": "world",
"destination": "bank",
"amount": 100,
"asset": "USD/2"
}],
"timestamp": "%s"
}
}]`, now.Format(time.RFC3339Nano)),
expectations: func(mockLedger *LedgerController) {
postings := []ledger.Posting{{
Source: "world",
Destination: "bank",
Amount: big.NewInt(100),
Asset: "USD/2",
}}
mockLedger.EXPECT().
CreateTransaction(gomock.Any(), ledgercontroller.Parameters[ledgercontroller.RunScript]{
Input: ledgercontroller.TxToScriptData(ledger.TransactionData{
Postings: postings,
Timestamp: now,
}, false),
}).
Return(&ledger.Log{}, &ledger.CreatedTransaction{
Transaction: ledger.Transaction{
TransactionData: ledger.TransactionData{
Postings: postings,
Metadata: metadata.Metadata{},
Timestamp: now,
},
},
}, nil)
},
expectResults: []bulking.APIResult{{
Data: map[string]any{
"postings": []any{
map[string]any{
"source": "world",
"destination": "bank",
"amount": float64(100),
"asset": "USD/2",
},
},
"timestamp": now.Format(time.RFC3339Nano),
"metadata": map[string]any{},
"reverted": false,
"id": float64(0),
},
ResponseType: bulking.ActionCreateTransaction,
}},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
Expand All @@ -428,6 +488,8 @@ func TestBulk(t *testing.T) {
router := NewRouter(systemController, auth.NewNoAuth(), os.Getenv("DEBUG") == "true")

req := httptest.NewRequest(http.MethodPost, "/xxx/_bulk", bytes.NewBufferString(testCase.body))
req.Header = testCase.headers

rec := httptest.NewRecorder()
if testCase.queryParams != nil {
req.URL.RawQuery = testCase.queryParams.Encode()
Expand Down

0 comments on commit 2e3bb90

Please sign in to comment.