126 lines
3.2 KiB
Go
126 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/beckn-one/beckn-onix/pkg/model"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TODO: Will Split this into success and fail (two test cases)
|
|
func TestProviderNew(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
config map[string]string
|
|
expectedError bool
|
|
expectedStatus int
|
|
prepareRequest func(req *http.Request)
|
|
}{
|
|
{
|
|
name: "No Config",
|
|
config: map[string]string{},
|
|
expectedError: true,
|
|
expectedStatus: http.StatusOK,
|
|
prepareRequest: func(req *http.Request) {
|
|
// Add minimal required headers.
|
|
req.Header.Set("context", "test-context")
|
|
req.Header.Set("transaction_id", "test-transaction")
|
|
},
|
|
},
|
|
{
|
|
name: "Success with BPP role",
|
|
config: map[string]string{
|
|
"role": "bpp",
|
|
},
|
|
expectedError: false,
|
|
expectedStatus: http.StatusOK,
|
|
prepareRequest: func(req *http.Request) {
|
|
// Add headers matching the check keys.
|
|
req.Header.Set("context", "test-context")
|
|
req.Header.Set("transaction_id", "test-transaction")
|
|
req.Header.Set("bpp_id", "bpp-456")
|
|
},
|
|
},
|
|
{
|
|
name: "Missing role configuration",
|
|
config: map[string]string{
|
|
// No role specified
|
|
},
|
|
expectedError: true,
|
|
prepareRequest: func(req *http.Request) {
|
|
req.Header.Set("context", "test-context")
|
|
req.Header.Set("transaction_id", "test-transaction")
|
|
},
|
|
},
|
|
{
|
|
name: "Invalid role configuration",
|
|
config: map[string]string{
|
|
"role": "invalid-role",
|
|
},
|
|
expectedError: true,
|
|
prepareRequest: func(req *http.Request) {
|
|
req.Header.Set("context", "test-context")
|
|
req.Header.Set("transaction_id", "test-transaction")
|
|
},
|
|
},
|
|
{
|
|
name: "passing the contextKeys",
|
|
config: map[string]string{
|
|
"role": "bpp",
|
|
"contextKeys": "transaction_id,message_id",
|
|
},
|
|
expectedError: false,
|
|
expectedStatus: http.StatusOK,
|
|
prepareRequest: func(req *http.Request) {
|
|
req.Header.Set("context", "test-context")
|
|
req.Header.Set("transaction_id", "test-transaction")
|
|
req.Header.Set("bpp_id", "bpp1")
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
requestBody := `{
|
|
"context": {
|
|
"transaction_id": "abc"
|
|
}
|
|
}`
|
|
|
|
p := provider{}
|
|
ctx := context.Background()
|
|
ctx = context.WithValue(ctx, model.ContextKeyParentID, "bap:bap-1:instanceID")
|
|
middleware, err := p.New(ctx, tc.config)
|
|
if tc.expectedError {
|
|
assert.Error(t, err)
|
|
return
|
|
}
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, middleware)
|
|
|
|
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
|
|
req := httptest.NewRequest("POST", "/", strings.NewReader(requestBody))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
if tc.prepareRequest != nil {
|
|
tc.prepareRequest(req)
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
middlewaredHandler := middleware(testHandler)
|
|
middlewaredHandler.ServeHTTP(w, req)
|
|
assert.Equal(t, tc.expectedStatus, w.Code, "Unexpected response status")
|
|
responseBody := w.Body.String()
|
|
t.Logf("Response Body: %s", responseBody)
|
|
|
|
})
|
|
}
|
|
}
|