Skip to content

Commit 4c8ebba

Browse files
Fix: Inconsistent error handling for response body read failure.
1 parent 176510b commit 4c8ebba

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

portal/backend/internal/proxy/service.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"crypto/rand"
88
"encoding/hex"
99
"errors"
10+
"fmt"
1011
"io"
1112
"net"
1213
"net/http"
@@ -195,7 +196,7 @@ func (s *Service) ForwardRawWithClientID(r *http.Request, upstreamMethod, upstre
195196

196197
respBody, err := io.ReadAll(resp.Body)
197198
if err != nil {
198-
return nil, err
199+
return nil, fmt.Errorf("read upstream response body: %w", ErrUpstreamUnavailable)
199200
}
200201

201202
return &UpstreamResponse{

portal/backend/tests/unit/proxy_service_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package unit
22

33
import (
4+
"errors"
5+
"net/http"
6+
"net/http/httptest"
47
"testing"
58
"time"
69

@@ -64,3 +67,27 @@ func TestIsAllowedPassthroughMethod(t *testing.T) {
6467
t.Fatal("expected DELETE to be disallowed")
6568
}
6669
}
70+
71+
func TestForwardRawMapsBodyReadFailureToUpstreamUnavailable(t *testing.T) {
72+
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
73+
w.Header().Set("Content-Length", "10")
74+
_, _ = w.Write([]byte("abc"))
75+
}))
76+
defer upstream.Close()
77+
78+
svc, err := proxy.NewService(config.ProxyConfig{
79+
OpenFGCAPIURL: upstream.URL,
80+
OpenFGCAPITimeout: 2 * time.Second,
81+
MaxRequestBytes: 1024,
82+
AllowedPassthrough: []string{"GET"},
83+
})
84+
if err != nil {
85+
t.Fatalf("failed to construct service: %v", err)
86+
}
87+
88+
req := httptest.NewRequest(http.MethodGet, "http://bff.local/api/consents", nil)
89+
_, err = svc.ForwardRaw(req, http.MethodGet, "/api/v1/consents", nil, nil)
90+
if !errors.Is(err, proxy.ErrUpstreamUnavailable) {
91+
t.Fatalf("expected ErrUpstreamUnavailable, got: %v", err)
92+
}
93+
}

0 commit comments

Comments
 (0)