gohorsejobs/backend/internal/router/router_test.go

43 lines
1.4 KiB
Go

package router_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/rede5/gohorsejobs/backend/internal/router"
"github.com/stretchr/testify/assert"
)
func TestRootHandler(t *testing.T) {
// Initialize the router
// Note: NewRouter might try to connect to the DB.
// For this unit test, if NewRouter is tightly coupled to DB, we might face issues.
// However, seeing as we just need to test the / route which doesn't use the DB,
// let's try to run it. If it fails due to DB connection,
// we'll have to mock the dependencies or skip full router initialization for just this handler.
//
// Given the context of the previous file view, NewRouter initializes services that use database.DB.
// If database.DB is nil (which it is here), it might pass fine until a handler ACTUALLY tries to use it.
// The root handler does NOT use the DB, so this might work.
r := router.NewRouter()
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var response map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err)
assert.Equal(t, "🐴 GoHorseJobs API is running!", response["message"])
// assert.NotEmpty(t, response["ip"]) // RemoteAddr might be empty in httptest
assert.Equal(t, "/docs", response["docs"])
assert.Equal(t, "/health", response["health"])
assert.Equal(t, "1.0.0", response["version"])
}