|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "os/signal" |
| 12 | + "syscall" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +func main() { |
| 17 | + os.Exit(run()) |
| 18 | +} |
| 19 | + |
| 20 | +func run() int { |
| 21 | + httpPort := flag.Int("http-port", 8000, "normal HTTP port (non-SSE)") |
| 22 | + ssePort := flag.Int("sse-port", 8047, "SSE port") |
| 23 | + flag.Parse() |
| 24 | + |
| 25 | + httpMux := http.NewServeMux() |
| 26 | + httpMux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) { |
| 27 | + w.WriteHeader(http.StatusOK) |
| 28 | + _, _ = w.Write([]byte("ok\n")) |
| 29 | + }) |
| 30 | + // Intentionally do NOT register /subscribe/student/events (or "/") on :8000. |
| 31 | + // This allows us to reproduce the 404 when Keploy replays the SSE preflight on the wrong port. |
| 32 | + |
| 33 | + sseMux := http.NewServeMux() |
| 34 | + sseMux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) { |
| 35 | + w.WriteHeader(http.StatusOK) |
| 36 | + _, _ = w.Write([]byte("ok\n")) |
| 37 | + }) |
| 38 | + sseMux.HandleFunc("/subscribe/student/events", handleEvents) |
| 39 | + |
| 40 | + httpSrv := &http.Server{Addr: fmt.Sprintf("0.0.0.0:%d", *httpPort), Handler: httpMux} |
| 41 | + sseSrv := &http.Server{Addr: fmt.Sprintf("0.0.0.0:%d", *ssePort), Handler: sseMux} |
| 42 | + |
| 43 | + type serverErr struct { |
| 44 | + name string |
| 45 | + addr string |
| 46 | + err error |
| 47 | + } |
| 48 | + |
| 49 | + exitCode := 0 |
| 50 | + errCh := make(chan serverErr, 2) |
| 51 | + go func() { |
| 52 | + log.Printf("HTTP listening on %s", httpSrv.Addr) |
| 53 | + errCh <- serverErr{name: "HTTP", addr: httpSrv.Addr, err: httpSrv.ListenAndServe()} |
| 54 | + }() |
| 55 | + go func() { |
| 56 | + log.Printf("SSE listening on %s", sseSrv.Addr) |
| 57 | + errCh <- serverErr{name: "SSE", addr: sseSrv.Addr, err: sseSrv.ListenAndServe()} |
| 58 | + }() |
| 59 | + |
| 60 | + stop := make(chan os.Signal, 1) |
| 61 | + signal.Notify(stop, os.Interrupt, syscall.SIGTERM) |
| 62 | + |
| 63 | + select { |
| 64 | + case sig := <-stop: |
| 65 | + log.Printf("signal received: %s", sig) |
| 66 | + case server := <-errCh: |
| 67 | + if server.err != nil && server.err != http.ErrServerClosed { |
| 68 | + log.Printf("%s listener error on %s: %v", server.name, server.addr, server.err) |
| 69 | + log.Printf("hint: check for port conflicts/permissions, then retry") |
| 70 | + exitCode = 1 |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 75 | + defer cancel() |
| 76 | + _ = httpSrv.Shutdown(ctx) |
| 77 | + _ = sseSrv.Shutdown(ctx) |
| 78 | + |
| 79 | + return exitCode |
| 80 | +} |
| 81 | + |
| 82 | +func writeCORS(w http.ResponseWriter) { |
| 83 | + w.Header().Set("Access-Control-Allow-Headers", "*") |
| 84 | + w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS") |
| 85 | + w.Header().Set("Access-Control-Allow-Origin", "*") |
| 86 | + w.Header().Set("Access-Control-Max-Age", "7200") |
| 87 | +} |
| 88 | + |
| 89 | +func handleEvents(w http.ResponseWriter, r *http.Request) { |
| 90 | + writeCORS(w) |
| 91 | + |
| 92 | + // CORS preflight: respond successfully, but do NOT set text/event-stream. |
| 93 | + // This is key to reproducing the Keploy issue: the test case won't be detected as SSE. |
| 94 | + if r.Method == http.MethodOptions { |
| 95 | + w.Header().Set("Content-Length", "0") |
| 96 | + w.WriteHeader(http.StatusOK) |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + if r.Method != http.MethodGet { |
| 101 | + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + flusher, ok := w.(http.Flusher) |
| 106 | + if !ok { |
| 107 | + http.Error(w, "streaming unsupported", http.StatusInternalServerError) |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + w.Header().Set("Content-Type", "text/event-stream") |
| 112 | + w.Header().Set("Cache-Control", "no-cache") |
| 113 | + w.Header().Set("Connection", "keep-alive") |
| 114 | + w.WriteHeader(http.StatusOK) |
| 115 | + |
| 116 | + doubtID := r.URL.Query().Get("doubtId") |
| 117 | + if doubtID == "" { |
| 118 | + doubtID = "missing" |
| 119 | + } |
| 120 | + |
| 121 | + ctx := r.Context() |
| 122 | + for i := 0; i < 3; i++ { |
| 123 | + select { |
| 124 | + case <-ctx.Done(): |
| 125 | + return |
| 126 | + default: |
| 127 | + } |
| 128 | + |
| 129 | + payload, err := json.Marshal(map[string]any{"doubtId": doubtID, "n": i}) |
| 130 | + if err != nil { |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + if _, err := fmt.Fprintf(w, "event: message\ndata: %s\n\n", payload); err != nil { |
| 135 | + return |
| 136 | + } |
| 137 | + flusher.Flush() |
| 138 | + |
| 139 | + if i < 2 { |
| 140 | + select { |
| 141 | + case <-ctx.Done(): |
| 142 | + return |
| 143 | + case <-time.After(250 * time.Millisecond): |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments