mirror of
https://github.com/umputun/reproxy.git
synced 2024-11-29 12:55:03 +03:00
7139c57766
* wip * resolve merge artifacts * full coverage for conductor * wire plugin conductor to main and proxy * wip, with separate match handler * split matching logic with another handler, add initial docs * move parts of proxy to handlers, add tests * add headers in to be sent to proxied url * merged from master * add example with docker compose * supress excesive debug reporting 0-9 disabled in docker * add plugin tests * randomize test port * lint: minor warns * lint: err shadow
86 lines
3.1 KiB
Go
86 lines
3.1 KiB
Go
package proxy
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func Test_headersHandler(t *testing.T) {
|
|
wr := httptest.NewRecorder()
|
|
handler := headersHandler([]string{"k1:v1", "k2:v2"})(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
t.Logf("req: %v", r)
|
|
}))
|
|
req, err := http.NewRequest("GET", "http://example.com", nil)
|
|
require.NoError(t, err)
|
|
handler.ServeHTTP(wr, req)
|
|
assert.Equal(t, "v1", wr.Result().Header.Get("k1"))
|
|
assert.Equal(t, "v2", wr.Result().Header.Get("k2"))
|
|
}
|
|
|
|
func Test_maxReqSizeHandler(t *testing.T) {
|
|
{
|
|
wr := httptest.NewRecorder()
|
|
handler := maxReqSizeHandler(10)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
t.Logf("req: %v", r)
|
|
}))
|
|
req, err := http.NewRequest("POST", "http://example.com", bytes.NewBufferString("123456"))
|
|
require.NoError(t, err)
|
|
handler.ServeHTTP(wr, req)
|
|
assert.Equal(t, http.StatusOK, wr.Result().StatusCode, "good size, full response")
|
|
}
|
|
{
|
|
wr := httptest.NewRecorder()
|
|
handler := maxReqSizeHandler(10)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
t.Logf("req: %v", r)
|
|
}))
|
|
req, err := http.NewRequest("POST", "http://example.com", bytes.NewBufferString("123456789012345"))
|
|
require.NoError(t, err)
|
|
handler.ServeHTTP(wr, req)
|
|
assert.Equal(t, http.StatusRequestEntityTooLarge, wr.Result().StatusCode)
|
|
}
|
|
{
|
|
wr := httptest.NewRecorder()
|
|
handler := maxReqSizeHandler(0)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
t.Logf("req: %v", r)
|
|
}))
|
|
req, err := http.NewRequest("POST", "http://example.com", bytes.NewBufferString("123456"))
|
|
require.NoError(t, err)
|
|
handler.ServeHTTP(wr, req)
|
|
assert.Equal(t, http.StatusOK, wr.Result().StatusCode, "good size, full response")
|
|
}
|
|
}
|
|
|
|
func Test_signatureHandler(t *testing.T) {
|
|
{
|
|
wr := httptest.NewRecorder()
|
|
handler := signatureHandler(true, "v0.0.1")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
t.Logf("req: %v", r)
|
|
}))
|
|
req, err := http.NewRequest("POST", "http://example.com", bytes.NewBufferString("123456"))
|
|
require.NoError(t, err)
|
|
handler.ServeHTTP(wr, req)
|
|
assert.Equal(t, http.StatusOK, wr.Result().StatusCode)
|
|
assert.Equal(t, "reproxy", wr.Result().Header.Get("App-Name"), wr.Result().Header)
|
|
assert.Equal(t, "umputun", wr.Result().Header.Get("Author"), wr.Result().Header)
|
|
assert.Equal(t, "v0.0.1", wr.Result().Header.Get("App-Version"), wr.Result().Header)
|
|
}
|
|
{
|
|
wr := httptest.NewRecorder()
|
|
handler := signatureHandler(false, "v0.0.1")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
t.Logf("req: %v", r)
|
|
}))
|
|
req, err := http.NewRequest("POST", "http://example.com", bytes.NewBufferString("123456"))
|
|
require.NoError(t, err)
|
|
handler.ServeHTTP(wr, req)
|
|
assert.Equal(t, http.StatusOK, wr.Result().StatusCode)
|
|
assert.Equal(t, "", wr.Result().Header.Get("App-Name"), wr.Result().Header)
|
|
assert.Equal(t, "", wr.Result().Header.Get("Author"), wr.Result().Header)
|
|
assert.Equal(t, "", wr.Result().Header.Get("App-Version"), wr.Result().Header)
|
|
}
|
|
}
|