<commit_before>package httpconnect

import (
	"fmt"
	"net"
	"net/http"
	"net/http/httptest"
	"net/url"
	"strconv"
	"testing"
	"time"

	"github.com/getlantern/http-proxy/filters"
	"github.com/stretchr/testify/assert"
)

func TestFilterTunnelPorts(t *testing.T) {
	origin := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		w.WriteHeader(200)
		w.Write([]byte("hi"))
	}))
	origin.StartTLS()
	defer origin.Close()
	ou, _ := url.Parse(origin.URL)
	_, _port, _ := net.SplitHostPort(ou.Host)
	port, _ := strconv.Atoi(_port)

	server := httptest.NewServer(filters.Join(
		New(&Options{AllowedPorts: []int{port, 443}, IdleTimeout: 30 * time.Second}),
		filters.Adapt(http.NotFoundHandler())))
	defer server.Close()
	u, _ := url.Parse(server.URL)
	client := http.Client{Transport: &http.Transport{
		Proxy: func(req *http.Request) (*url.URL, error) {
			return u, nil
		},
		DisableKeepAlives: true,
	}}

	req, _ := http.NewRequest("CONNECT", "https://site.com:abc", nil)
	resp, _ := client.Do(req)
	assert.Nil(t, resp, "CONNECT request with non-integer port should fail with 400")

	req, _ = http.NewRequest("GET", "https://www.google.com/humans.txt", nil)
	resp, err := client.Do(req)
	if !assert.NoError(t, err) {
		return
	}
	_ = resp.Body.Close()
	assert.Equal(t, http.StatusOK, resp.StatusCode, "CONNECT request to allowed port should succeed")

	req, _ = http.NewRequest("GET", fmt.Sprintf("https://site.com:%d", (port-1)), nil)
	resp, _ = client.Do(req)
	assert.Nil(t, resp, "CONNECT request to disallowed port should fail with 403")
}<commit_msg>Changed GET back to CONNECT<commit_after>package httpconnect

import (
	"fmt"
	"net"
	"net/http"
	"net/http/httptest"
	"net/url"
	"strconv"
	"testing"
	"time"

	"github.com/getlantern/http-proxy/filters"
	"github.com/stretchr/testify/assert"
)

func TestFilterTunnelPorts(t *testing.T) {
	origin := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		w.WriteHeader(200)
		w.Write([]byte("hi"))
	}))
	origin.StartTLS()
	defer origin.Close()
	ou, _ := url.Parse(origin.URL)
	_, _port, _ := net.SplitHostPort(ou.Host)
	port, _ := strconv.Atoi(_port)

	server := httptest.NewServer(filters.Join(
		New(&Options{AllowedPorts: []int{port, 443}, IdleTimeout: 30 * time.Second}),
		filters.Adapt(http.NotFoundHandler())))
	defer server.Close()
	u, _ := url.Parse(server.URL)
	client := http.Client{Transport: &http.Transport{
		Proxy: func(req *http.Request) (*url.URL, error) {
			return u, nil
		},
		DisableKeepAlives: true,
	}}

	req, _ := http.NewRequest("CONNECT", "https://site.com:abc", nil)
	resp, _ := client.Do(req)
	assert.Nil(t, resp, "CONNECT request with non-integer port should fail with 400")

	req, _ = http.NewRequest("GET", "https://www.google.com/humans.txt", nil)
	resp, err := client.Do(req)
	if !assert.NoError(t, err) {
		return
	}
	_ = resp.Body.Close()
	assert.Equal(t, http.StatusOK, resp.StatusCode, "CONNECT request to allowed port should succeed")

	req, _ = http.NewRequest("CONNECT", fmt.Sprintf("https://site.com:%d", (port-1)), nil)
	resp, _ = client.Do(req)
	assert.Nil(t, resp, "CONNECT request to disallowed port should fail with 403")
}
{'fraction_non_alphanumeric': 0.1735330836454432, 'fraction_numerical': 0.009363295880149813, 'mean_word_length': 4.69271758436945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1662115', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1197, 'n_words': 234}