Skip to content

Commit

Permalink
tests: rename tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Nov 14, 2024
1 parent a0bcd32 commit 2dda695
Show file tree
Hide file tree
Showing 22 changed files with 345 additions and 433 deletions.
60 changes: 30 additions & 30 deletions buffer/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/vulcand/oxy/v2/utils"
)

func TestSimple(t *testing.T) {
func TestBuffer_simple(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("hello"))
})
Expand All @@ -29,7 +29,7 @@ func TestSimple(t *testing.T) {

// this is our redirect to server
rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL = testutils.ParseURI(srv.URL)
req.URL = testutils.MustParseRequestURI(srv.URL)
fwd.ServeHTTP(w, req)
})

Expand All @@ -46,7 +46,7 @@ func TestSimple(t *testing.T) {
assert.Equal(t, "hello", string(body))
}

func TestChunkedEncodingSuccess(t *testing.T) {
func TestBuffer_chunkedEncodingSuccess(t *testing.T) {
var reqBody string
var contentLength int64
srv := testutils.NewHandler(func(w http.ResponseWriter, req *http.Request) {
Expand All @@ -63,7 +63,7 @@ func TestChunkedEncodingSuccess(t *testing.T) {

// this is our redirect to server
rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL = testutils.ParseURI(srv.URL)
req.URL = testutils.MustParseRequestURI(srv.URL)
fwd.ServeHTTP(w, req)
})

Expand All @@ -74,7 +74,7 @@ func TestChunkedEncodingSuccess(t *testing.T) {
proxy := httptest.NewServer(st)
t.Cleanup(proxy.Close)

conn, err := net.Dial("tcp", testutils.ParseURI(proxy.URL).Host)
conn, err := net.Dial("tcp", testutils.MustParseRequestURI(proxy.URL).Host)
require.NoError(t, err)

_, _ = fmt.Fprintf(conn, "POST / HTTP/1.1\r\nHost: 127.0.0.1:8080\r\nTransfer-Encoding: chunked\r\n\r\n4\r\ntest\r\n5\r\ntest1\r\n5\r\ntest2\r\n0\r\n\r\n")
Expand All @@ -86,7 +86,7 @@ func TestChunkedEncodingSuccess(t *testing.T) {
assert.EqualValues(t, len(reqBody), contentLength)
}

func TestChunkedEncodingLimitReached(t *testing.T) {
func TestBuffer_chunkedEncodingLimitReached(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("hello"))
})
Expand All @@ -97,7 +97,7 @@ func TestChunkedEncodingLimitReached(t *testing.T) {

// this is our redirect to server
rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL = testutils.ParseURI(srv.URL)
req.URL = testutils.MustParseRequestURI(srv.URL)
fwd.ServeHTTP(w, req)
})

Expand All @@ -108,7 +108,7 @@ func TestChunkedEncodingLimitReached(t *testing.T) {
proxy := httptest.NewServer(st)
t.Cleanup(proxy.Close)

conn, err := net.Dial("tcp", testutils.ParseURI(proxy.URL).Host)
conn, err := net.Dial("tcp", testutils.MustParseRequestURI(proxy.URL).Host)
require.NoError(t, err)
_, _ = fmt.Fprint(conn, "POST / HTTP/1.1\r\nHost: 127.0.0.1:8080\r\nTransfer-Encoding: chunked\r\n\r\n4\r\ntest\r\n5\r\ntest1\r\n5\r\ntest2\r\n0\r\n\r\n")
status, err := bufio.NewReader(conn).ReadString('\n')
Expand All @@ -117,7 +117,7 @@ func TestChunkedEncodingLimitReached(t *testing.T) {
assert.Equal(t, "HTTP/1.1 413 Request Entity Too Large\r\n", status)
}

func TestChunkedResponse(t *testing.T) {
func TestBuffer_chunkedResponse(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
h := w.(http.Hijacker)
conn, _, _ := h.Hijack()
Expand All @@ -129,7 +129,7 @@ func TestChunkedResponse(t *testing.T) {
fwd := forward.New(false)

rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL = testutils.ParseURI(srv.URL)
req.URL = testutils.MustParseRequestURI(srv.URL)
fwd.ServeHTTP(w, req)
})
st, err := New(rdr)
Expand All @@ -145,7 +145,7 @@ func TestChunkedResponse(t *testing.T) {
assert.Equal(t, strconv.Itoa(len("testtest1test2")), re.Header.Get("Content-Length"))
}

func TestRequestLimitReached(t *testing.T) {
func TestBuffer_requestLimitReached(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("hello"))
})
Expand All @@ -156,7 +156,7 @@ func TestRequestLimitReached(t *testing.T) {

// this is our redirect to server
rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL = testutils.ParseURI(srv.URL)
req.URL = testutils.MustParseRequestURI(srv.URL)
fwd.ServeHTTP(w, req)
})

Expand All @@ -172,7 +172,7 @@ func TestRequestLimitReached(t *testing.T) {
assert.Equal(t, http.StatusRequestEntityTooLarge, re.StatusCode)
}

func TestResponseLimitReached(t *testing.T) {
func TestBuffer_responseLimitReached(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("hello, this response is too large"))
})
Expand All @@ -183,7 +183,7 @@ func TestResponseLimitReached(t *testing.T) {

// this is our redirect to server
rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL = testutils.ParseURI(srv.URL)
req.URL = testutils.MustParseRequestURI(srv.URL)
fwd.ServeHTTP(w, req)
})

Expand All @@ -199,7 +199,7 @@ func TestResponseLimitReached(t *testing.T) {
assert.Equal(t, http.StatusInternalServerError, re.StatusCode)
}

func TestFileStreamingResponse(t *testing.T) {
func TestBuffer_fileStreamingResponse(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("hello, this response is too large to fit in memory"))
})
Expand All @@ -210,7 +210,7 @@ func TestFileStreamingResponse(t *testing.T) {

// this is our redirect to server
rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL = testutils.ParseURI(srv.URL)
req.URL = testutils.MustParseRequestURI(srv.URL)
fwd.ServeHTTP(w, req)
})

Expand All @@ -227,7 +227,7 @@ func TestFileStreamingResponse(t *testing.T) {
assert.Equal(t, "hello, this response is too large to fit in memory", string(body))
}

func TestCustomErrorHandler(t *testing.T) {
func TestBuffer_customErrorHandler(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("hello, this response is too large"))
})
Expand All @@ -238,7 +238,7 @@ func TestCustomErrorHandler(t *testing.T) {

// this is our redirect to server
rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL = testutils.ParseURI(srv.URL)
req.URL = testutils.MustParseRequestURI(srv.URL)
fwd.ServeHTTP(w, req)
})

Expand All @@ -258,7 +258,7 @@ func TestCustomErrorHandler(t *testing.T) {
assert.Equal(t, http.StatusTeapot, re.StatusCode)
}

func TestNotModified(t *testing.T) {
func TestBuffer_notModified(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotModified)
})
Expand All @@ -269,7 +269,7 @@ func TestNotModified(t *testing.T) {

// this is our redirect to server
rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL = testutils.ParseURI(srv.URL)
req.URL = testutils.MustParseRequestURI(srv.URL)
fwd.ServeHTTP(w, req)
})

Expand All @@ -285,7 +285,7 @@ func TestNotModified(t *testing.T) {
assert.Equal(t, http.StatusNotModified, re.StatusCode)
}

func TestNoBody(t *testing.T) {
func TestBuffer_noBody(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
Expand All @@ -296,7 +296,7 @@ func TestNoBody(t *testing.T) {

// this is our redirect to server
rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL = testutils.ParseURI(srv.URL)
req.URL = testutils.MustParseRequestURI(srv.URL)
fwd.ServeHTTP(w, req)
})

Expand All @@ -313,7 +313,7 @@ func TestNoBody(t *testing.T) {
}

// Make sure that stream handler preserves TLS settings.
func TestPreservesTLS(t *testing.T) {
func TestBuffer_preservesTLS(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
Expand All @@ -327,7 +327,7 @@ func TestPreservesTLS(t *testing.T) {
// this is our redirect to server
rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
cs = req.TLS
req.URL = testutils.ParseURI(srv.URL)
req.URL = testutils.MustParseRequestURI(srv.URL)
fwd.ServeHTTP(w, req)
})

Expand All @@ -345,7 +345,7 @@ func TestPreservesTLS(t *testing.T) {
assert.NotNil(t, cs)
}

func TestNotNilBody(t *testing.T) {
func TestBuffer_notNilBody(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("hello"))
})
Expand All @@ -356,7 +356,7 @@ func TestNotNilBody(t *testing.T) {

// this is our redirect to server
rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL = testutils.ParseURI(srv.URL)
req.URL = testutils.MustParseRequestURI(srv.URL)
// During a request check if the request body is no nil before sending to the next middleware
// Because we can send a POST request without body
assert.NotNil(t, req.Body)
Expand All @@ -381,7 +381,7 @@ func TestNotNilBody(t *testing.T) {
assert.Equal(t, "hello", string(body))
}

func TestGRPCErrorResponse(t *testing.T) {
func TestBuffer_GRPC_ErrorResponse(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Grpc-Status", "10" /* ABORTED */)
w.WriteHeader(http.StatusOK)
Expand All @@ -396,7 +396,7 @@ func TestGRPCErrorResponse(t *testing.T) {

// this is our redirect to server
rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL = testutils.ParseURI(srv.URL)
req.URL = testutils.MustParseRequestURI(srv.URL)
fwd.ServeHTTP(w, req)
})

Expand All @@ -413,7 +413,7 @@ func TestGRPCErrorResponse(t *testing.T) {
assert.Empty(t, body)
}

func TestGRPCOKResponse(t *testing.T) {
func TestBuffer_GRPC_OKResponse(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Grpc-Status", "0" /* OK */)
_, _ = w.Write([]byte("grpc-body"))
Expand All @@ -429,7 +429,7 @@ func TestGRPCOKResponse(t *testing.T) {

// this is our redirect to server
rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL = testutils.ParseURI(srv.URL)
req.URL = testutils.MustParseRequestURI(srv.URL)
fwd.ServeHTTP(w, req)
})

Expand Down
20 changes: 10 additions & 10 deletions buffer/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/vulcand/oxy/v2/testutils"
)

func TestSuccess(t *testing.T) {
func TestBuffer_success(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("hello"))
})
Expand All @@ -23,15 +23,15 @@ func TestSuccess(t *testing.T) {
proxy := httptest.NewServer(rt)
t.Cleanup(proxy.Close)

require.NoError(t, lb.UpsertServer(testutils.ParseURI(srv.URL)))
require.NoError(t, lb.UpsertServer(testutils.MustParseRequestURI(srv.URL)))

re, body, err := testutils.Get(proxy.URL)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
assert.Equal(t, "hello", string(body))
}

func TestRetryOnError(t *testing.T) {
func TestBuffer_retryOnError(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("hello"))
})
Expand All @@ -42,16 +42,16 @@ func TestRetryOnError(t *testing.T) {
proxy := httptest.NewServer(rt)
t.Cleanup(proxy.Close)

require.NoError(t, lb.UpsertServer(testutils.ParseURI("http://localhost:64321")))
require.NoError(t, lb.UpsertServer(testutils.ParseURI(srv.URL)))
require.NoError(t, lb.UpsertServer(testutils.MustParseRequestURI("http://localhost:64321")))
require.NoError(t, lb.UpsertServer(testutils.MustParseRequestURI(srv.URL)))

re, body, err := testutils.Get(proxy.URL, testutils.Body("some request parameters"))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, re.StatusCode)
assert.Equal(t, "hello", string(body))
}

func TestRetryExceedAttempts(t *testing.T) {
func TestBuffer_retryExceedAttempts(t *testing.T) {
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("hello"))
})
Expand All @@ -62,10 +62,10 @@ func TestRetryExceedAttempts(t *testing.T) {
proxy := httptest.NewServer(rt)
t.Cleanup(proxy.Close)

require.NoError(t, lb.UpsertServer(testutils.ParseURI("http://localhost:64321")))
require.NoError(t, lb.UpsertServer(testutils.ParseURI("http://localhost:64322")))
require.NoError(t, lb.UpsertServer(testutils.ParseURI("http://localhost:64323")))
require.NoError(t, lb.UpsertServer(testutils.ParseURI(srv.URL)))
require.NoError(t, lb.UpsertServer(testutils.MustParseRequestURI("http://localhost:64321")))
require.NoError(t, lb.UpsertServer(testutils.MustParseRequestURI("http://localhost:64322")))
require.NoError(t, lb.UpsertServer(testutils.MustParseRequestURI("http://localhost:64323")))
require.NoError(t, lb.UpsertServer(testutils.MustParseRequestURI(srv.URL)))

re, _, err := testutils.Get(proxy.URL)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 2dda695

Please sign in to comment.