|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/roadrunner-server/errors" |
| 14 | + "github.com/roadrunner-server/http/v5/api" |
| 15 | + "github.com/roadrunner-server/http/v5/config" |
| 16 | + "github.com/roadrunner-server/pool/payload" |
| 17 | + "github.com/roadrunner-server/pool/pool" |
| 18 | + staticPool "github.com/roadrunner-server/pool/pool/static_pool" |
| 19 | + "github.com/roadrunner-server/pool/worker" |
| 20 | + "go.uber.org/zap" |
| 21 | +) |
| 22 | + |
| 23 | +// mockPool satisfies api.Pool. Only Exec is exercised by the tests below. |
| 24 | +type mockPool struct{ execErr error } |
| 25 | + |
| 26 | +func (m *mockPool) Workers() []*worker.Process { return nil } |
| 27 | +func (m *mockPool) RemoveWorker(_ context.Context) error { return nil } |
| 28 | +func (m *mockPool) AddWorker() error { return nil } |
| 29 | +func (m *mockPool) Exec(_ context.Context, _ *payload.Payload, _ chan struct{}) (chan *staticPool.PExec, error) { |
| 30 | + return nil, m.execErr |
| 31 | +} |
| 32 | +func (m *mockPool) Reset(_ context.Context) error { return nil } |
| 33 | +func (m *mockPool) Destroy(_ context.Context) {} |
| 34 | + |
| 35 | +func newTestHandler(t *testing.T, cfg *config.Config, p api.Pool) *Handler { |
| 36 | + t.Helper() |
| 37 | + h, err := NewHandler(cfg, p, zap.NewNop()) |
| 38 | + if err != nil { |
| 39 | + t.Fatal(err) |
| 40 | + } |
| 41 | + return h |
| 42 | +} |
| 43 | + |
| 44 | +func defaultCfg() *config.Config { |
| 45 | + return &config.Config{ |
| 46 | + MaxRequestSize: 1024, |
| 47 | + InternalErrorCode: 500, |
| 48 | + Uploads: &config.Uploads{ |
| 49 | + Dir: os.TempDir(), |
| 50 | + Forbidden: map[string]struct{}{}, |
| 51 | + Allowed: map[string]struct{}{}, |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// ── Group A: ServeHTTP errors before pool.Exec (nil pool is safe) ──────────── |
| 57 | + |
| 58 | +func TestServeHTTP_InvalidMultipart_Returns400(t *testing.T) { |
| 59 | + h := newTestHandler(t, defaultCfg(), nil) |
| 60 | + |
| 61 | + // Boundary declared in header but body has no valid multipart parts → EOF. |
| 62 | + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("")) |
| 63 | + req.Header.Set("Content-Type", "multipart/form-data; boundary=1111") |
| 64 | + |
| 65 | + rr := httptest.NewRecorder() |
| 66 | + h.ServeHTTP(rr, req) |
| 67 | + |
| 68 | + if rr.Code != http.StatusBadRequest { |
| 69 | + t.Errorf("expected 400, got %d", rr.Code) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestServeHTTP_StreamBody_MaxBytesExceeded_Returns413(t *testing.T) { |
| 74 | + h := newTestHandler(t, defaultCfg(), nil) |
| 75 | + |
| 76 | + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("this body is too long")) |
| 77 | + req.Header.Set("Content-Type", "application/json") |
| 78 | + |
| 79 | + rr := httptest.NewRecorder() |
| 80 | + // Wrap body so that reading more than 5 bytes returns *http.MaxBytesError. |
| 81 | + req.Body = http.MaxBytesReader(rr, req.Body, 5) |
| 82 | + |
| 83 | + h.ServeHTTP(rr, req) |
| 84 | + |
| 85 | + if rr.Code != http.StatusRequestEntityTooLarge { |
| 86 | + t.Errorf("expected 413, got %d", rr.Code) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestServeHTTP_TruncatedMultipart_Returns400(t *testing.T) { |
| 91 | + h := newTestHandler(t, defaultCfg(), nil) |
| 92 | + |
| 93 | + // Multipart body with an open part but no closing boundary → ErrUnexpectedEOF. |
| 94 | + body := "--1111\r\nContent-Disposition: form-data; name=\"f\"\r\n\r\nval" |
| 95 | + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body)) |
| 96 | + req.Header.Set("Content-Type", "multipart/form-data; boundary=1111") |
| 97 | + |
| 98 | + rr := httptest.NewRecorder() |
| 99 | + h.ServeHTTP(rr, req) |
| 100 | + |
| 101 | + if rr.Code != http.StatusBadRequest { |
| 102 | + t.Errorf("expected 400, got %d", rr.Code) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// ── Group B: Direct calls to handleError, URI, FetchIP ─────────────────────── |
| 107 | + |
| 108 | +func TestHandleError_NoFreeWorkers_SetsNoWorkersHeader(t *testing.T) { |
| 109 | + h := newTestHandler(t, defaultCfg(), nil) |
| 110 | + |
| 111 | + rr := httptest.NewRecorder() |
| 112 | + h.handleError(rr, errors.E(errors.NoFreeWorkers)) |
| 113 | + |
| 114 | + if got := rr.Header().Get(noWorkers); got != trueStr { |
| 115 | + t.Errorf("expected No-Workers: true, got %q", got) |
| 116 | + } |
| 117 | + if rr.Code != 500 { |
| 118 | + t.Errorf("expected status 500, got %d", rr.Code) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestHandleError_CustomInternalCode(t *testing.T) { |
| 123 | + cfg := defaultCfg() |
| 124 | + cfg.InternalErrorCode = 503 |
| 125 | + h := newTestHandler(t, cfg, nil) |
| 126 | + |
| 127 | + rr := httptest.NewRecorder() |
| 128 | + h.handleError(rr, fmt.Errorf("boom")) |
| 129 | + |
| 130 | + if rr.Code != 503 { |
| 131 | + t.Errorf("expected 503, got %d", rr.Code) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestHandleError_DebugMode_WritesEscapedError(t *testing.T) { |
| 136 | + cfg := defaultCfg() |
| 137 | + cfg.Pool = &pool.Config{Debug: true} |
| 138 | + h := newTestHandler(t, cfg, nil) |
| 139 | + |
| 140 | + rr := httptest.NewRecorder() |
| 141 | + h.handleError(rr, fmt.Errorf("boom<script>")) |
| 142 | + |
| 143 | + body := rr.Body.String() |
| 144 | + if strings.Contains(body, "<script>") { |
| 145 | + t.Error("response body contains unescaped <script> tag (XSS risk)") |
| 146 | + } |
| 147 | + if !strings.Contains(body, "<script>") { |
| 148 | + t.Errorf("expected HTML-escaped error in body, got: %q", body) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func TestURI_PlainHTTP(t *testing.T) { |
| 153 | + r := httptest.NewRequest(http.MethodGet, "/path?q=1", nil) |
| 154 | + r.Host = "example.com" |
| 155 | + |
| 156 | + got := URI(r) |
| 157 | + want := "http://example.com/path?q=1" |
| 158 | + if got != want { |
| 159 | + t.Errorf("URI() = %q, want %q", got, want) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func TestURI_TLSRequest_HTTPSScheme(t *testing.T) { |
| 164 | + r := httptest.NewRequest(http.MethodGet, "/path?q=1", nil) |
| 165 | + r.Host = "example.com" |
| 166 | + r.TLS = &tls.ConnectionState{} |
| 167 | + |
| 168 | + got := URI(r) |
| 169 | + want := "https://example.com/path?q=1" |
| 170 | + if got != want { |
| 171 | + t.Errorf("URI() = %q, want %q", got, want) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func TestURI_StripsCRLFInjection(t *testing.T) { |
| 176 | + r := httptest.NewRequest(http.MethodGet, "/path", nil) |
| 177 | + r.Host = "example.com" |
| 178 | + // Inject CRLF into the raw query — a classic HTTP response-splitting vector. |
| 179 | + r.URL.RawQuery = "param=value\r\nX-Injected: true" |
| 180 | + |
| 181 | + got := URI(r) |
| 182 | + if strings.ContainsAny(got, "\r\n") { |
| 183 | + t.Errorf("URI() result contains CRLF characters: %q", got) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func TestFetchIP_StripPortFromIPv4(t *testing.T) { |
| 188 | + got := FetchIP("127.0.0.1:8080", zap.NewNop()) |
| 189 | + if got != "127.0.0.1" { |
| 190 | + t.Errorf("FetchIP() = %q, want %q", got, "127.0.0.1") |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +func TestFetchIP_BareIPv6_NoPort(t *testing.T) { |
| 195 | + // "::1" contains colons but is not host:port — SplitHostPort fails, |
| 196 | + // ParseIP succeeds. |
| 197 | + got := FetchIP("::1", zap.NewNop()) |
| 198 | + if got != "::1" { |
| 199 | + t.Errorf("FetchIP() = %q, want %q", got, "::1") |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +// ── Group C: mockPool tests ─────────────────────────────────────────────────── |
| 204 | + |
| 205 | +func TestServeHTTP_PoolExecError_Returns500(t *testing.T) { |
| 206 | + mp := &mockPool{execErr: fmt.Errorf("worker died")} |
| 207 | + h := newTestHandler(t, defaultCfg(), mp) |
| 208 | + |
| 209 | + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"key":"val"}`)) |
| 210 | + req.Header.Set("Content-Type", "application/json") |
| 211 | + |
| 212 | + rr := httptest.NewRecorder() |
| 213 | + h.ServeHTTP(rr, req) |
| 214 | + |
| 215 | + if rr.Code != 500 { |
| 216 | + t.Errorf("expected 500, got %d", rr.Code) |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +func TestServeHTTP_NoFreeWorkers_SetsHeader(t *testing.T) { |
| 221 | + mp := &mockPool{execErr: errors.E(errors.NoFreeWorkers)} |
| 222 | + h := newTestHandler(t, defaultCfg(), mp) |
| 223 | + |
| 224 | + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"key":"val"}`)) |
| 225 | + req.Header.Set("Content-Type", "application/json") |
| 226 | + |
| 227 | + rr := httptest.NewRecorder() |
| 228 | + h.ServeHTTP(rr, req) |
| 229 | + |
| 230 | + if got := rr.Header().Get(noWorkers); got != trueStr { |
| 231 | + t.Errorf("expected No-Workers: true, got %q", got) |
| 232 | + } |
| 233 | + if rr.Code != 500 { |
| 234 | + t.Errorf("expected 500, got %d", rr.Code) |
| 235 | + } |
| 236 | +} |
0 commit comments