Skip to content

Commit 360d0d8

Browse files
AchoArnoldCopilot
andcommitted
feat(api): add FCM_ENDPOINT transport override for integration tests
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 65ced9d commit 360d0d8

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

api/pkg/di/container.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/tls"
66
"fmt"
77
"net/http"
8+
"net/url"
89
"os"
910
"strconv"
1011
"strings"
@@ -398,7 +399,26 @@ ALTER TABLE discords ADD CONSTRAINT IF NOT EXISTS uni_discords_server_id CHECK (
398399
// FirebaseApp creates a new instance of firebase.App
399400
func (container *Container) FirebaseApp() (app *firebase.App) {
400401
container.logger.Debug(fmt.Sprintf("creating %T", app))
401-
app, err := firebase.NewApp(context.Background(), nil, option.WithAuthCredentialsJSON(option.ServiceAccount, container.FirebaseCredentials()))
402+
403+
var opts []option.ClientOption
404+
405+
if fcmEndpoint := os.Getenv("FCM_ENDPOINT"); fcmEndpoint != "" {
406+
container.logger.Info(fmt.Sprintf("using FCM endpoint override: %s", fcmEndpoint))
407+
targetURL, err := url.Parse(fcmEndpoint)
408+
if err != nil {
409+
container.logger.Fatal(stacktrace.Propagate(err, "cannot parse FCM_ENDPOINT"))
410+
}
411+
opts = append(opts, option.WithHTTPClient(&http.Client{
412+
Transport: &fcmRedirectTransport{
413+
target: targetURL,
414+
base: http.DefaultTransport,
415+
},
416+
}))
417+
} else {
418+
opts = append(opts, option.WithAuthCredentialsJSON(option.ServiceAccount, container.FirebaseCredentials()))
419+
}
420+
421+
app, err := firebase.NewApp(context.Background(), nil, opts...)
402422
if err != nil {
403423
msg := "cannot initialize firebase application"
404424
container.logger.Fatal(stacktrace.Propagate(err, msg))

api/pkg/di/fcm_transport.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package di
2+
3+
import (
4+
"net/http"
5+
"net/url"
6+
)
7+
8+
// fcmRedirectTransport rewrites Firebase SDK HTTP requests to a custom endpoint.
9+
// Used in integration tests to redirect FCM traffic to the emulator.
10+
type fcmRedirectTransport struct {
11+
target *url.URL
12+
base http.RoundTripper
13+
}
14+
15+
func (t *fcmRedirectTransport) RoundTrip(req *http.Request) (*http.Response, error) {
16+
req.URL.Scheme = t.target.Scheme
17+
req.URL.Host = t.target.Host
18+
return t.base.RoundTrip(req)
19+
}

0 commit comments

Comments
 (0)