Files
payments/internal/api/v2/handler_test.go
CrimsonandGitHub 4ae387a5d1 test: setup go SDK generation for use in e2e tests and start running CI steps with nix (#377)
* 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
2025-03-31 10:30:34 +02:00

73 lines
2.0 KiB
Go

package v2
import (
"bytes"
"context"
"encoding/json"
"io"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-chi/chi/v5"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestV2Handlers(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "API v2 Suite")
}
func assertExpectedResponse(res *http.Response, expectedStatusCode int, expectedBodyString string) {
defer res.Body.Close()
Expect(res.StatusCode).To(Equal(expectedStatusCode))
data, err := io.ReadAll(res.Body)
Expect(err).To(BeNil())
Expect(string(data)).To(ContainSubstring(expectedBodyString))
}
func prepareJSONRequest(method string, a any) *http.Request {
b, err := json.Marshal(a)
Expect(err).To(BeNil())
body := bytes.NewReader(b)
return httptest.NewRequest(method, "/", body)
}
func prepareJSONRequestWithQuery(method string, key string, val string, a any) *http.Request {
b, err := json.Marshal(a)
Expect(err).To(BeNil())
body := bytes.NewReader(b)
return prepareQueryRequestWithBody(method, body, key, val)
}
func prepareQueryRequest(method string, args ...string) *http.Request {
return prepareQueryRequestWithBody(method, nil, args...)
}
func prepareQueryRequestWithBody(method string, body io.Reader, args ...string) *http.Request {
req := httptest.NewRequest(method, "/", body)
rctx := chi.NewRouteContext()
appendToRouteContext(rctx, args...)
return req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
}
func prepareQueryRequestWithPath(path string, args ...string) *http.Request {
req := httptest.NewRequest(http.MethodGet, path, nil)
rctx := chi.NewRouteContext()
appendToRouteContext(rctx, args...)
return req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
}
func appendToRouteContext(rctx *chi.Context, args ...string) {
if len(args)%2 != 0 {
log.Fatalf("arguments must be provided in key value pairs: %s", args)
}
for i := 0; i < len(args); i += 2 {
val := args[i+1]
rctx.URLParams.Add(args[i], val)
}
}