Skip to content

Commit

Permalink
wait for the api service before start testing, trim api response befo…
Browse files Browse the repository at this point in the history
…re compare with the expected response
  • Loading branch information
lucasmenendez committed Sep 6, 2024
1 parent e10cb14 commit 69ea0f0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func New(conf *APIConfig) *API {

// Start starts the API HTTP server (non blocking).
func (a *API) Start() {
log.Infof("starting API server on %s:%d", a.host, a.port)
go func() {
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", a.host, a.port), a.initRouter()); err != nil {
log.Fatalf("failed to start the API server: %v", err)
Expand Down
40 changes: 40 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"testing"
"time"

"github.com/vocdoni/saas-backend/account"
"github.com/vocdoni/saas-backend/db"
Expand All @@ -29,10 +31,14 @@ const (
testPort = 7788
)

// testURL helper function returns the full URL for the given path using the
// test host and port.
func testURL(path string) string {
return fmt.Sprintf("http://%s:%d%s", testHost, testPort, path)
}

// mustMarshall helper function marshalls the input interface into a byte slice.
// It panics if the marshalling fails.
func mustMarshall(i any) []byte {
b, err := json.Marshal(i)
if err != nil {
Expand All @@ -41,6 +47,36 @@ func mustMarshall(i any) []byte {
return b
}

// pingAPI helper function pings the API endpoint and retries the request
// if it fails until the retries limit is reached. It returns an error if the
// request fails or the status code is not 200 as many times as the retries
// limit.
func pingAPI(endpoint string, retries int) error {
// create a new ping request
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return err
}
// try to ping the API
var pingErr error
for i := 0; i < retries; i++ {
var resp *http.Response
if resp, pingErr = http.DefaultClient.Do(req); pingErr == nil {
if resp.StatusCode == http.StatusOK {
return nil
}
pingErr = fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
time.Sleep(time.Second)
}
return pingErr
}

// TestMain function starts the MongoDB container, the Voconed container, and
// the API server before running the tests. It also creates a new MongoDB
// connection with a random database name, a new Voconed API client, and a new
// account with the Voconed private key and the API container endpoint. It
// starts the API server and waits for it to start before running the tests.
func TestMain(m *testing.M) {
ctx := context.Background()
// start a MongoDB container for testing
Expand Down Expand Up @@ -101,6 +137,10 @@ func TestMain(m *testing.M) {
Account: testAccount,
FullTransparentMode: false,
}).Start()
// wait for the API to start
if err := pingAPI(testURL(pingEndpoint), 5); err != nil {
panic(err)
}
// run the tests
os.Exit(m.Run())
}
4 changes: 4 additions & 0 deletions api/routes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package api

const (
// ping route
// GET /ping to check the server status
pingEndpoint = "/ping"

// auth routes

// POST /auth/refresh to refresh the JWT token
Expand Down
3 changes: 2 additions & 1 deletion api/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io"
"net/http"
"strings"
"testing"

qt "github.com/frankban/quicktest"
Expand Down Expand Up @@ -133,7 +134,7 @@ func Test_registerHandler(t *testing.T) {
if testCase.expectedBody != nil {
body, err := io.ReadAll(resp.Body)
c.Assert(err, qt.IsNil)
c.Assert(body, qt.DeepEquals, testCase.expectedBody)
c.Assert(strings.TrimSpace(string(body)), qt.Equals, string(testCase.expectedBody))
}
}
}

0 comments on commit 69ea0f0

Please sign in to comment.