Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion internal/api/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"bytes"
"crypto/subtle"
"encoding/json"
"errors"
Expand Down Expand Up @@ -358,7 +359,16 @@ func constantTimeEqual(expected, actual string) bool {
}

func decodeJSON(e *core.RequestEvent, dst any) error {
decoder := json.NewDecoder(e.Request.Body)
// PocketBase wraps request bodies in a rereadable reader so middleware can
// inspect them more than once. Decode from an immutable snapshot here;
// otherwise a second Decode used to prove EOF can observe the rewound body
// as a second JSON value on real network requests.
body, err := io.ReadAll(e.Request.Body)
if err != nil {
return err
}

decoder := json.NewDecoder(bytes.NewReader(body))
decoder.DisallowUnknownFields()
if err := decoder.Decode(dst); err != nil {
return err
Expand Down
51 changes: 51 additions & 0 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package api

import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
Expand All @@ -10,6 +12,7 @@ import (
"github.com/Phloraxx/payment-api/internal/payments"
"github.com/Phloraxx/payment-api/internal/sms"
_ "github.com/Phloraxx/payment-api/migrations"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tests"
)
Expand Down Expand Up @@ -88,6 +91,54 @@ func TestPaymentAPIAuthenticationAndAmountValidation(t *testing.T) {
}
}

func TestPaymentCreateThroughRealHTTPServer(t *testing.T) {
app := apiTestFactory(t, nil)
defer app.Cleanup()

router, err := apis.NewRouter(app)
if err != nil {
t.Fatal(err)
}
serveEvent := &core.ServeEvent{App: app, Router: router}
if err := app.OnServe().Trigger(serveEvent, func(e *core.ServeEvent) error { return nil }); err != nil {
t.Fatal(err)
}
mux, err := serveEvent.Router.BuildMux()
if err != nil {
t.Fatal(err)
}

server := httptest.NewServer(mux)
defer server.Close()

req, err := http.NewRequest(http.MethodPost, server.URL+"/api/payments", strings.NewReader(`{"amount":100,"externalId":"network-http"}`))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Authorization", "Bearer api-secret")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", "network-http-idem")

res, err := server.Client().Do(req)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if res.StatusCode != http.StatusCreated {
t.Fatalf("status = %d, body = %s", res.StatusCode, body)
}
content := string(body)
for _, want := range []string{`"requestedAmount":100`, `"externalId":"network-http"`, `upi://pay?`} {
if !strings.Contains(content, want) {
t.Fatalf("response missing %q: %s", want, content)
}
}
}

func TestPublicPaymentStatusRedactsSensitiveEvidence(t *testing.T) {
const paymentID = "paytest00000001"
scenario := tests.ApiScenario{
Expand Down