quite a lot of things
This commit is contained in:
@@ -1,39 +1,34 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"billit/internal/database"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func TestHandler(t *testing.T) {
|
||||
e := echo.New()
|
||||
func TestHomeRoute(t *testing.T) {
|
||||
// Create a minimal server with db for testing
|
||||
db := database.New()
|
||||
s := &Server{db: db}
|
||||
handler := s.RegisterRoutes()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
resp := httptest.NewRecorder()
|
||||
c := e.NewContext(req, resp)
|
||||
s := &Server{}
|
||||
// Assertions
|
||||
if err := s.HelloWorldHandler(c); err != nil {
|
||||
t.Errorf("handler() error = %v", err)
|
||||
return
|
||||
}
|
||||
handler.ServeHTTP(resp, req)
|
||||
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Errorf("handler() wrong status code = %v", resp.Code)
|
||||
return
|
||||
}
|
||||
expected := map[string]string{"message": "Hello World"}
|
||||
var actual map[string]string
|
||||
// Decode the response body into the actual map
|
||||
if err := json.NewDecoder(resp.Body).Decode(&actual); err != nil {
|
||||
t.Errorf("handler() error decoding response body: %v", err)
|
||||
return
|
||||
}
|
||||
// Compare the decoded response with the expected value
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
t.Errorf("handler() wrong response body. expected = %v, actual = %v", expected, actual)
|
||||
return
|
||||
t.Errorf("home route wrong status code = %v, want %v", resp.Code, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterSetup(t *testing.T) {
|
||||
// Test that Echo router can be set up without panic
|
||||
e := echo.New()
|
||||
if e == nil {
|
||||
t.Error("failed to create echo instance")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user