feat: ora funge tutto benissimo per qualche motivo
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package listaUtenti
|
||||
|
||||
import (
|
||||
"phc/website/services/database"
|
||||
"phc/website/services/server/router"
|
||||
"phc/website/services/server/routes"
|
||||
"phc/website/sl"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func Configure(l *sl.ServiceLocator) error {
|
||||
router.UseRoutePage(l, "/utenti", "pages/lista-utenti/index.html")
|
||||
|
||||
db, err := sl.Use(l, database.Slot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
api, err := sl.Use(l, routes.Api)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
api.Get("/lista-utenti", func(c *fiber.Ctx) error {
|
||||
users, err := db.ReadUsers()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(users)
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package listaUtenti_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"phc/website/model"
|
||||
"phc/website/services/database"
|
||||
"phc/website/services/server/routes"
|
||||
"phc/website/sl"
|
||||
"testing"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/valyala/fasthttp"
|
||||
"github.com/valyala/fasthttp/fasthttputil"
|
||||
)
|
||||
|
||||
func Test1(t *testing.T) {
|
||||
l := sl.New()
|
||||
|
||||
sl.InjectValue[database.Database](l, database.Slot, &database.Memory{
|
||||
Users: []model.User{
|
||||
{
|
||||
Id: "claire",
|
||||
FullName: "Claire Doe",
|
||||
Nickname: "claire-doe",
|
||||
AuthSources: map[string]model.AuthSource{},
|
||||
},
|
||||
{
|
||||
Id: "john",
|
||||
FullName: "John Smith",
|
||||
Nickname: "john-smith",
|
||||
AuthSources: map[string]model.AuthSource{},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
r := fiber.New()
|
||||
sl.InjectValue(l, routes.Root, fiber.Router(r))
|
||||
|
||||
req, err := http.NewRequest("GET", "http://localhost:4000/api/lista-utenti", nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
defer ln.Close()
|
||||
|
||||
go func() {
|
||||
err := fasthttp.Serve(ln, r.Handler())
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to serve: %v", err))
|
||||
}
|
||||
}()
|
||||
|
||||
client := http.Client{
|
||||
Transport: &http.Transport{
|
||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
return ln.Dial()
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log(string(body))
|
||||
}
|
||||
Reference in New Issue
Block a user