-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassets_test.go
More file actions
79 lines (67 loc) · 2.08 KB
/
Copy pathassets_test.go
File metadata and controls
79 lines (67 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"net"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"testing"
)
func TestDesktopAssetMiddlewareRewritesRootToControlPanel(t *testing.T) {
tests := []string{"/", "", "/index.html"}
for _, path := range tests {
t.Run(path, func(t *testing.T) {
var gotPath string
next := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
})
request := httptest.NewRequest(http.MethodGet, "http://wails.localhost"+path, nil)
desktopAssetMiddleware(next).ServeHTTP(httptest.NewRecorder(), request)
if gotPath != "/control.html" {
t.Fatalf("expected /control.html, got %q", gotPath)
}
})
}
}
func TestDesktopAssetMiddlewareKeepsNonRootAssets(t *testing.T) {
var gotPath string
next := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
})
request := httptest.NewRequest(http.MethodGet, "http://wails.localhost/icons/icon.png", nil)
desktopAssetMiddleware(next).ServeHTTP(httptest.NewRecorder(), request)
if gotPath != "/icons/icon.png" {
t.Fatalf("expected /icons/icon.png, got %q", gotPath)
}
}
func TestProxyToSidecarInjectsControlToken(t *testing.T) {
const controlToken = "desktop-control-token"
var gotToken string
sidecar := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotToken = r.Header.Get("x-codex-token")
w.WriteHeader(http.StatusOK)
}))
defer sidecar.Close()
sidecarURL, err := url.Parse(sidecar.URL)
if err != nil {
t.Fatal(err)
}
_, portText, err := net.SplitHostPort(sidecarURL.Host)
if err != nil {
t.Fatal(err)
}
port, err := strconv.Atoi(portText)
if err != nil {
t.Fatal(err)
}
app := &App{port: port, token: controlToken}
request := httptest.NewRequest(http.MethodGet, "http://wails.localhost/codex/control-config", nil)
recorder := httptest.NewRecorder()
app.proxyToSidecar(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", recorder.Code)
}
if gotToken != controlToken {
t.Fatalf("expected injected token %q, got %q", controlToken, gotToken)
}
}