Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(testingx): more tests for NewHTTPProxyHandler #1276

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions internal/testingx/httpproxy_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package testingx_test

import (
"context"
"errors"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/mocks"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/testingproxy"
"github.com/ooni/probe-cli/v3/internal/testingx"
)

func TestHTTPProxyHandler(t *testing.T) {
Expand All @@ -16,4 +25,109 @@ func TestHTTPProxyHandler(t *testing.T) {
testCase.Run(t)
})
}

t.Run("rejects requests without a host header", func(t *testing.T) {
rr := httptest.NewRecorder()
netx := &netxlite.Netx{Underlying: &mocks.UnderlyingNetwork{
// all nil: panic if we hit the network
}}
handler := testingx.NewHTTPProxyHandler(log.Log, netx)
req := &http.Request{
Method: http.MethodGet,
Host: "", // explicitly empty
}
handler.ServeHTTP(rr, req)
res := rr.Result()
if res.StatusCode != http.StatusBadRequest {
t.Fatal("unexpected status code", res.StatusCode)
}
})

t.Run("rejects requests with a via header", func(t *testing.T) {
rr := httptest.NewRecorder()
netx := &netxlite.Netx{Underlying: &mocks.UnderlyingNetwork{
// all nil: panic if we hit the network
}}
handler := testingx.NewHTTPProxyHandler(log.Log, netx)
req := &http.Request{
Method: http.MethodGet,
Host: "www.example.com",
Header: http.Header{
"Via": {"antani/0.1.0"},
},
}
handler.ServeHTTP(rr, req)
res := rr.Result()
if res.StatusCode != http.StatusBadRequest {
t.Fatal("unexpected status code", res.StatusCode)
}
})

t.Run("rejects requests with a POST method", func(t *testing.T) {
rr := httptest.NewRecorder()
netx := &netxlite.Netx{Underlying: &mocks.UnderlyingNetwork{
// all nil: panic if we hit the network
}}
handler := testingx.NewHTTPProxyHandler(log.Log, netx)
req := &http.Request{
Method: http.MethodPost,
Host: "www.example.com",
Header: http.Header{},
}
handler.ServeHTTP(rr, req)
res := rr.Result()
if res.StatusCode != http.StatusNotImplemented {
t.Fatal("unexpected status code", res.StatusCode)
}
})

t.Run("returns 502 when the round trip fails", func(t *testing.T) {
t.Run("with a GET request", func(t *testing.T) {
rr := httptest.NewRecorder()
netx := &netxlite.Netx{Underlying: &mocks.UnderlyingNetwork{
MockGetaddrinfoLookupANY: func(ctx context.Context, domain string) ([]string, string, error) {
return nil, "", errors.New("mocked error")
},
MockGetaddrinfoResolverNetwork: func() string {
return "antani"
},
}}
handler := testingx.NewHTTPProxyHandler(log.Log, netx)
req := &http.Request{
Method: http.MethodGet,
Host: "www.example.com",
Header: http.Header{},
URL: &url.URL{},
}
handler.ServeHTTP(rr, req)
res := rr.Result()
if res.StatusCode != http.StatusBadGateway {
t.Fatal("unexpected status code", res.StatusCode)
}
})

t.Run("with a CONNECT request", func(t *testing.T) {
rr := httptest.NewRecorder()
netx := &netxlite.Netx{Underlying: &mocks.UnderlyingNetwork{
MockGetaddrinfoLookupANY: func(ctx context.Context, domain string) ([]string, string, error) {
return nil, "", errors.New("mocked error")
},
MockGetaddrinfoResolverNetwork: func() string {
return "antani"
},
}}
handler := testingx.NewHTTPProxyHandler(log.Log, netx)
req := &http.Request{
Method: http.MethodConnect,
Host: "www.example.com:443",
Header: http.Header{},
URL: &url.URL{},
}
handler.ServeHTTP(rr, req)
res := rr.Result()
if res.StatusCode != http.StatusBadGateway {
t.Fatal("unexpected status code", res.StatusCode)
}
})
})
}