mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
* Use formance-sdk-go/v3 to in connector uninstall e2e test * Compile API docs from openapi spec * Generate go SDK locally for use in integration tests * Get rid of unnecessary redirect in e2e tests now that SDK is generated * Omit generated SDK code from codecoverage checks * Add flake and envrc for nix * Run Dirty and Tests CI steps with nix * Run pre-commit using nix * Remove duplicated steps from Earthfile * Fixes for linter * Add go generate to Justfile pre-commit * Still run Deploy on ubuntu-latest * Address coderabbit feedback
104 lines
3.0 KiB
Go
104 lines
3.0 KiB
Go
package httpwrapper_test
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/formancehq/payments/internal/connectors/httpwrapper"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
func TestClient(t *testing.T) {
|
|
RegisterFailHandler(Fail)
|
|
RunSpecs(t, "Client Suite")
|
|
}
|
|
|
|
type successRes struct {
|
|
ID string `json:"id"`
|
|
}
|
|
|
|
type errorRes struct {
|
|
Code string `json:"code"`
|
|
}
|
|
|
|
var _ = Describe("ClientWrapper", func() {
|
|
var (
|
|
config *httpwrapper.Config
|
|
client httpwrapper.Client
|
|
server *httptest.Server
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
config = &httpwrapper.Config{Timeout: 30 * time.Millisecond}
|
|
client = httpwrapper.NewClient(config)
|
|
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
params, err := url.ParseQuery(r.URL.RawQuery)
|
|
Expect(err).To(BeNil())
|
|
|
|
code := params.Get("code")
|
|
statusCode, err := strconv.Atoi(code)
|
|
Expect(err).To(BeNil())
|
|
if statusCode == http.StatusOK {
|
|
_, err := w.Write([]byte(`{"id":"someid"}`))
|
|
Expect(err).To(BeNil())
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(statusCode)
|
|
_, err = w.Write([]byte(`{"code":"err123"}`))
|
|
Expect(err).To(BeNil())
|
|
}))
|
|
})
|
|
AfterEach(func() {
|
|
server.Close()
|
|
})
|
|
|
|
Context("making a request with default client settings", func() {
|
|
It("unmarshals successful responses when acceptable status code seen", func(ctx SpecContext) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL+"?code=200", http.NoBody)
|
|
Expect(err).To(BeNil())
|
|
|
|
res := &successRes{}
|
|
code, doErr := client.Do(context.Background(), req, res, nil)
|
|
Expect(code).To(Equal(http.StatusOK))
|
|
Expect(doErr).To(BeNil())
|
|
Expect(res.ID).To(Equal("someid"))
|
|
})
|
|
It("unmarshals error responses when bad status code seen", func(ctx SpecContext) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL+"?code=500", http.NoBody)
|
|
Expect(err).To(BeNil())
|
|
|
|
res := &errorRes{}
|
|
code, doErr := client.Do(context.Background(), req, &successRes{}, res)
|
|
Expect(code).To(Equal(http.StatusInternalServerError))
|
|
Expect(doErr).To(MatchError(httpwrapper.ErrStatusCodeServerError))
|
|
Expect(res.Code).To(Equal("err123"))
|
|
})
|
|
It("unmarshals error responses when http client error seen", func(ctx SpecContext) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL+"?code=400", http.NoBody)
|
|
Expect(err).To(BeNil())
|
|
|
|
res := &errorRes{}
|
|
code, doErr := client.Do(context.Background(), req, &successRes{}, res)
|
|
Expect(code).To(Equal(http.StatusBadRequest))
|
|
Expect(doErr).To(MatchError(httpwrapper.ErrStatusCodeClientError))
|
|
Expect(res.Code).To(Equal("err123"))
|
|
})
|
|
It("responds with error when HTTP request fails", func(ctx SpecContext) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "notaurl", http.NoBody)
|
|
Expect(err).To(BeNil())
|
|
|
|
res := &errorRes{}
|
|
code, doErr := client.Do(context.Background(), req, &successRes{}, res)
|
|
Expect(code).To(Equal(0))
|
|
Expect(doErr).To(MatchError(ContainSubstring("failed to make request")))
|
|
})
|
|
})
|
|
})
|