Antonio Mika
·
2026-10-10
1package pipe
2
3import (
4 "log/slog"
5 "net/http"
6 "net/http/httptest"
7 "testing"
8
9 "github.com/picosh/pico/pkg/shared/router"
10)
11
12func TestRoutes(t *testing.T) {
13 t.Chdir("../../..")
14 t.Setenv("PICO_PIPE_ENABLED", "false")
15 cfg := NewConfig("pipe-test")
16 cfg.Logger = slog.New(slog.DiscardHandler)
17 web := &webRouter{cfg: cfg, db: NewTestDB(cfg.Logger)}
18 handler := router.NewHostRouter(cfg.Domain, cfg.Space, web.mainMux(), web.sitesMux())
19
20 site := "https://tester." + cfg.Domain
21 main := "https://" + cfg.Domain
22 for _, tt := range []struct {
23 method string
24 url string
25 status int
26 cors bool
27 }{
28 {method: http.MethodOptions, url: main + "/topic/a/b", status: http.StatusOK, cors: true},
29 {method: http.MethodOptions, url: main + "/pubsub/a", status: http.StatusOK, cors: true},
30 {method: http.MethodOptions, url: main + "/pipe/a", status: http.StatusOK, cors: true},
31 {method: http.MethodPut, url: main + "/topic/a", status: http.StatusMethodNotAllowed},
32 {method: http.MethodGet, url: main + "/favicon.ico", status: http.StatusOK},
33 {method: http.MethodGet, url: main + "/missing", status: http.StatusNotFound},
34 // Sites only serve the static files.
35 {method: http.MethodGet, url: site + "/anim.js", status: http.StatusOK},
36 {method: http.MethodGet, url: site + "/check", status: http.StatusNotFound},
37 {method: http.MethodOptions, url: site + "/topic/a", status: http.StatusNotFound},
38 } {
39 res := httptest.NewRecorder()
40 handler.ServeHTTP(res, httptest.NewRequest(tt.method, tt.url, nil))
41 cors := res.Header().Get("Access-Control-Allow-Origin") == "*"
42 if res.Code != tt.status || cors != tt.cors {
43 t.Errorf("%s %s = %d cors %v, want %d cors %v", tt.method, tt.url, res.Code, cors, tt.status, tt.cors)
44 }
45 }
46}