Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
23df4310f8 |
@@ -5,10 +5,8 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -31,7 +29,10 @@ func TestRequestTimeouts(t *testing.T) {
|
|||||||
env map[string]string
|
env map[string]string
|
||||||
// appTakesNothing has the app never read, while the client sends
|
// appTakesNothing has the app never read, while the client sends
|
||||||
// as fast as it can; otherwise the app reads, and the client
|
// as fast as it can; otherwise the app reads, and the client
|
||||||
// stops sending halfway.
|
// stops sending halfway. smallwebwaf then waits on the client
|
||||||
|
// only once it has connected to the app and passed on the first
|
||||||
|
// bytes; a test process held up for shortTimeout before that
|
||||||
|
// gets 504, which is the right answer, and the case fails.
|
||||||
appTakesNothing bool
|
appTakesNothing bool
|
||||||
want int
|
want int
|
||||||
}{
|
}{
|
||||||
@@ -68,47 +69,32 @@ func TestRequestTimeouts(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
app *httptest.Server
|
|
||||||
appURL string
|
appURL string
|
||||||
sendRequest func(*testing.T, string) net.Conn
|
sendRequest func(*testing.T, string) net.Conn
|
||||||
appGotBody atomic.Bool
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if tc.appTakesNothing {
|
if tc.appTakesNothing {
|
||||||
appURL, sendRequest = startAppThatTakesNothing(t), sendLargeBody
|
appURL, sendRequest = startAppThatTakesNothing(t), sendLargeBody
|
||||||
} else {
|
} else {
|
||||||
app = startApp(t, func(_ http.ResponseWriter, r *http.Request) {
|
appURL, sendRequest = startApp(t, readBody).URL, sendPartOfBody
|
||||||
n, _ := io.Copy(io.Discard, r.Body)
|
|
||||||
appGotBody.Store(n > 0)
|
|
||||||
})
|
|
||||||
appURL, sendRequest = app.URL, sendPartOfBody
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addr, out := startProxy(t, appURL, tc.env)
|
addr, out := startProxy(t, appURL, tc.env)
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
got := readResponse(t, sendRequest(t, addr))
|
conn := sendRequest(t, addr)
|
||||||
|
|
||||||
|
wantStatus(t, readResponse(t, conn), tc.want)
|
||||||
wantTimedOut(t, start)
|
wantTimedOut(t, start)
|
||||||
|
wantLine(t, out.requestLine(t), tc.want, requestlog.ActionTimedOut)
|
||||||
want := tc.want
|
|
||||||
|
|
||||||
if app != nil {
|
|
||||||
// Close returns once the app has finished with the request.
|
|
||||||
app.Close()
|
|
||||||
|
|
||||||
// Until some of the body has reached the app, smallwebwaf
|
|
||||||
// waits on the app, and SPEC.md asks for 504; the timeout
|
|
||||||
// runs out then only if the test process is held up.
|
|
||||||
if !appGotBody.Load() {
|
|
||||||
want = http.StatusGatewayTimeout
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
wantStatus(t, got, want)
|
|
||||||
wantLine(t, out.requestLine(t), want, requestlog.ActionTimedOut)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// readBody is an app that reads the request body, then answers.
|
||||||
|
func readBody(_ http.ResponseWriter, r *http.Request) {
|
||||||
|
_, _ = io.Copy(io.Discard, r.Body)
|
||||||
|
}
|
||||||
|
|
||||||
// startAppThatTakesNothing starts an app that accepts connections and
|
// startAppThatTakesNothing starts an app that accepts connections and
|
||||||
// never reads from them, and returns its URL.
|
// never reads from them, and returns its URL.
|
||||||
func startAppThatTakesNothing(t *testing.T) string {
|
func startAppThatTakesNothing(t *testing.T) string {
|
||||||
|
|||||||
Reference in New Issue
Block a user