diff options
Diffstat (limited to 'lilin_test.go')
| -rw-r--r-- | lilin_test.go | 100 |
1 files changed, 67 insertions, 33 deletions
diff --git a/lilin_test.go b/lilin_test.go index e4db531..091f7c7 100644 --- a/lilin_test.go +++ b/lilin_test.go @@ -1,37 +1,91 @@ // SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> // SPDX-License-Identifier: GPL-3.0-only -package lilin_test +package lilin import ( "context" "errors" + "io" "log" "net/http" "testing" "time" - "git.sr.ht/~shulhan/lilin" "git.sr.ht/~shulhan/lilin/internal" "git.sr.ht/~shulhan/pakakeh.go/lib/net" ) -var client *lilin.Client - const ( dummyHTTPAddress = `127.0.0.1:6330` ) +type dummyHTTPService struct { + mux *http.ServeMux + httpd *http.Server + reqbodyq chan []byte +} + +var dhs *dummyHTTPService +var client *Client + +func newDummyHTTPService() (dhs *dummyHTTPService) { + dhs = &dummyHTTPService{ + mux: http.NewServeMux(), + reqbodyq: make(chan []byte), + } + + dhs.mux.HandleFunc(`GET /health`, func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + dhs.mux.HandleFunc(`POST /mattermost`, func(w http.ResponseWriter, req *http.Request) { + var logp = `POST /mattermost` + var reqbody []byte + var err error + reqbody, err = io.ReadAll(req.Body) + if err != nil { + log.Fatalf(`%s: %s`, logp, err) + } + w.WriteHeader(http.StatusOK) + go func() { + dhs.reqbodyq <- reqbody + }() + }) + + dhs.httpd = &http.Server{ + Addr: dummyHTTPAddress, + Handler: dhs.mux, + ReadTimeout: 5 * time.Second, + WriteTimeout: 5 * time.Second, + } + return dhs +} + +func (dhs *dummyHTTPService) start() { + var err = dhs.httpd.ListenAndServe() + if err != nil { + log.Fatal(err) + } +} + func TestMain(m *testing.M) { internal.Now = func() time.Time { return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) } - var server *lilin.Server + dhs = newDummyHTTPService() + go dhs.start() + + var err = net.WaitAlive(`tcp`, dummyHTTPAddress, 5*time.Second) + if err != nil { + log.Fatal(err) + } + + var server *Server server = startServer() client = createClient(server) - go dummyHTTPService() m.Run() @@ -40,19 +94,19 @@ func TestMain(m *testing.M) { ctx, cancelfn = context.WithTimeout(context.Background(), 5*time.Second) defer cancelfn() - var err = server.Shutdown(ctx) + err = server.Shutdown(ctx) if err != nil { log.Fatal(err) } } -func startServer() (server *lilin.Server) { - var serverOpts = lilin.ServerConfig{ +func startServer() (server *Server) { + var serverOpts = ServerConfig{ BaseDir: `testdata`, } var err error - server, err = lilin.NewServer(serverOpts) + server, err = NewServer(serverOpts) if err != nil { log.Fatal(err) } @@ -71,35 +125,15 @@ func startServer() (server *lilin.Server) { return server } -func createClient(server *lilin.Server) (client *lilin.Client) { - var clientOpts = lilin.ClientConfig{ +func createClient(server *Server) (client *Client) { + var clientOpts = ClientConfig{ ServerURL: `http://` + server.Config.Address, } var err error - client, err = lilin.NewClient(clientOpts) + client, err = NewClient(clientOpts) if err != nil { log.Fatal(err) } return client } - -func dummyHTTPService() { - var mux = http.NewServeMux() - - mux.HandleFunc(`GET /health`, func(w http.ResponseWriter, _ *http.Request) { - w.WriteHeader(http.StatusOK) - }) - - var httpd = http.Server{ - Addr: dummyHTTPAddress, - Handler: mux, - ReadTimeout: 5 * time.Second, - WriteTimeout: 5 * time.Second, - } - - var err = httpd.ListenAndServe() - if err != nil { - log.Fatal(err) - } -} |
