|
1 | 1 | package proxy
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bufio" |
4 | 5 | "bytes"
|
5 | 6 | "compress/gzip"
|
| 7 | + "context" |
6 | 8 | "fmt"
|
7 | 9 | "io"
|
8 | 10 | "net/http"
|
| 11 | + "net/http/httptest" |
| 12 | + "net/url" |
| 13 | + "strconv" |
9 | 14 | "strings"
|
10 | 15 | "testing"
|
| 16 | + "time" |
11 | 17 |
|
12 | 18 | "github.com/google/go-cmp/cmp"
|
13 | 19 | )
|
@@ -210,4 +216,94 @@ func TestProxy(t *testing.T) {
|
210 | 216 | t.Errorf("unexpected response body (-got +want):\n%s", diff)
|
211 | 217 | }
|
212 | 218 | })
|
| 219 | + |
| 220 | + t.Run("notify-proxy: sending POST request to /_templ/reload/events should receive reload sse event", func(t *testing.T) { |
| 221 | + // Arrange 1: create a test proxy server. |
| 222 | + dummyHandler := func(w http.ResponseWriter, r *http.Request) {} |
| 223 | + dummyServer := httptest.NewServer(http.HandlerFunc(dummyHandler)) |
| 224 | + defer dummyServer.Close() |
| 225 | + |
| 226 | + u, err := url.Parse(dummyServer.URL) |
| 227 | + if err != nil { |
| 228 | + t.Fatalf("unexpected error parsing URL: %v", err) |
| 229 | + } |
| 230 | + handler := New("0.0.0.0", 0, u) |
| 231 | + proxyServer := httptest.NewServer(handler) |
| 232 | + defer proxyServer.Close() |
| 233 | + |
| 234 | + u2, err := url.Parse(proxyServer.URL) |
| 235 | + if err != nil { |
| 236 | + t.Fatalf("unexpected error parsing URL: %v", err) |
| 237 | + } |
| 238 | + port, err := strconv.Atoi(u2.Port()) |
| 239 | + if err != nil { |
| 240 | + t.Fatalf("unexpected error parsing port: %v", err) |
| 241 | + } |
| 242 | + |
| 243 | + // Arrange 2: start a goroutine to listen for sse events. |
| 244 | + ctx := context.Background() |
| 245 | + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) |
| 246 | + defer cancel() |
| 247 | + |
| 248 | + errChan := make(chan error) |
| 249 | + sseRespCh := make(chan string) |
| 250 | + sseListening := make(chan bool) // Coordination channel that ensures the SSE listener is started before notifying the proxy. |
| 251 | + go func() { |
| 252 | + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/_templ/reload/events", proxyServer.URL), nil) |
| 253 | + if err != nil { |
| 254 | + errChan <- err |
| 255 | + return |
| 256 | + } |
| 257 | + resp, err := http.DefaultClient.Do(req) |
| 258 | + if err != nil { |
| 259 | + errChan <- err |
| 260 | + return |
| 261 | + } |
| 262 | + defer resp.Body.Close() |
| 263 | + |
| 264 | + sseListening <- true |
| 265 | + lines := []string{} |
| 266 | + scanner := bufio.NewScanner(resp.Body) |
| 267 | + for scanner.Scan() { |
| 268 | + lines = append(lines, scanner.Text()) |
| 269 | + if scanner.Text() == "data: reload" { |
| 270 | + sseRespCh <- strings.Join(lines, "\n") |
| 271 | + return |
| 272 | + } |
| 273 | + } |
| 274 | + err = scanner.Err() |
| 275 | + // We expect the connection to be closed by the server: this is the only way to terminate the sse connection. |
| 276 | + if err != nil { |
| 277 | + errChan <- err |
| 278 | + return |
| 279 | + } |
| 280 | + }() |
| 281 | + |
| 282 | + // Act: notify the proxy. |
| 283 | + select { // Either SSE is listening or an error occurred. |
| 284 | + case <-sseListening: |
| 285 | + err = NotifyProxy(u2.Hostname(), port) |
| 286 | + if err != nil { |
| 287 | + t.Fatalf("unexpected error notifying proxy: %v", err) |
| 288 | + } |
| 289 | + case err := <-errChan: |
| 290 | + if err == nil { |
| 291 | + t.Fatalf("unexpected sse response: %v", err) |
| 292 | + } |
| 293 | + } |
| 294 | + |
| 295 | + // Assert. |
| 296 | + select { // Either SSE has a expected response or an error or timeout occurred. |
| 297 | + case resp := <-sseRespCh: |
| 298 | + if !strings.Contains(resp, "event: message\ndata: reload") { |
| 299 | + t.Errorf("expected sse reload event to be received, got: %q", resp) |
| 300 | + } |
| 301 | + case err := <-errChan: |
| 302 | + if err == nil { |
| 303 | + t.Fatalf("unexpected sse response: %v", err) |
| 304 | + } |
| 305 | + case <-ctx.Done(): |
| 306 | + t.Fatalf("timeout waiting for sse response") |
| 307 | + } |
| 308 | + }) |
213 | 309 | }
|
0 commit comments