<commit_before>package proxy

import (
	"fmt"
	"log"
	"net/http"
	"net/url"
	"testing"

	"github.com/stretchr/testify/assert"
)

var (
	dummyProxy   *Proxy
	dummyRequest *http.Request
	dummyURL     *url.URL
)

func init() {
	dummyProxy = &Proxy{}
	var err error
	dummyRequest, err = createDummyRequest()
	if err != nil {
		log.Fatal(err)
	}
}

func createDummyRequest() (*http.Request, error) {
	dummyHeaders := http.Header{
		"User-Agent":          {"dummy-client"},
		"X-Chocon-Test-Value": {"6"},
		// ignored headers
		"Connection":          {"Keep-Alive"},
		"Keep-Alive":          {"timeout=30, max=100"},
		"Proxy-Authenticate":  {"Basic"},
		"Proxy-Authorization": {"Basic dummy"},
		"Te":                  {"deflate"},
		"Trailers":            {"Expires"},
		"Transfer-Encoding":   {"chunked"},
		"Upgrade":             {"WebSocket"},
	}
	dummyURL = &url.URL{
		Scheme: "http",
		Path:   "/dummy",
	}
	req, err := http.NewRequest("GET", "/dummy", nil)
	if err != nil {
		return nil, err
	}
	req.Header = dummyHeaders
	req.Close = true
	req.Proto = "HTTP/1.0"
	req.ProtoMajor = 1
	req.ProtoMinor = 0
	req.URL = dummyURL

	return req, nil
}

func TestCopyRequest(t *testing.T) {
	req := dummyProxy.copyRequest(dummyRequest)

	assert.Equal(t, req.Proto, "HTTP/1.1")
	assert.Equal(t, req.ProtoMajor, 1)
	assert.Equal(t, req.ProtoMinor, 1)
	assert.Equal(t, req.Close, false)
	assert.Equal(t, req.URL.Scheme, "http")
	assert.Equal(t, req.URL.Path, dummyURL.Path)

	assert.Equal(t, req.Header["User-Agent"][0], "dummy-client")
	assert.Equal(t, req.Header["X-Chocon-Test-Value"][0], "6")

	for k, _ := range req.Header {
		if _, ok := ignoredHeaderNames[k]; ok {
			assert.Fail(t, fmt.Sprintf("header filed: %s must be removed", k))
		}
	}
}

func BenchmarkCopyRequest(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = dummyProxy.copyRequest(dummyRequest)
	}
}<commit_msg>add rewrite test<commit_after>package proxy

import (
	"fmt"
	"log"
	"net/http"
	"net/url"
	"testing"

	"github.com/stretchr/testify/assert"
)

var (
	dummyProxy   *Proxy
	dummyRequest *http.Request
	dummyURL     *url.URL
)

func init() {
	dummyProxy = &Proxy{}
	var err error
	dummyRequest, err = createDummyRequest()
	if err != nil {
		log.Fatal(err)
	}
}

func createDummyRequest() (*http.Request, error) {
	dummyHeaders := http.Header{
		"User-Agent":          {"dummy-client"},
		"X-Chocon-Test-Value": {"6"},
		// ignored headers
		"Connection":          {"Keep-Alive"},
		"Keep-Alive":          {"timeout=30, max=100"},
		"Proxy-Authenticate":  {"Basic"},
		"Proxy-Authorization": {"Basic dummy"},
		"Te":                  {"deflate"},
		"Trailers":            {"Expires"},
		"Transfer-Encoding":   {"chunked"},
		"Upgrade":             {"WebSocket"},
	}
	dummyURL = &url.URL{
		Scheme: "http",
		Path:   "/dummy",
	}
	req, err := http.NewRequest("GET", "/dummy", nil)
	if err != nil {
		return nil, err
	}
	req.Header = dummyHeaders
	req.Close = true
	req.Proto = "HTTP/1.0"
	req.ProtoMajor = 1
	req.ProtoMinor = 0
	req.URL = dummyURL

	return req, nil
}

func TestCopyRequest(t *testing.T) {
	req := dummyProxy.copyRequest(dummyRequest)

	assert.Equal(t, req.Proto, "HTTP/1.1")
	assert.Equal(t, req.ProtoMajor, 1)
	assert.Equal(t, req.ProtoMinor, 1)
	assert.Equal(t, req.Close, false)
	assert.Equal(t, req.URL.Scheme, "http")
	assert.Equal(t, req.URL.Path, dummyURL.Path)

	assert.Equal(t, req.Header["User-Agent"][0], "dummy-client")
	assert.Equal(t, req.Header["X-Chocon-Test-Value"][0], "6")

	for k := range req.Header {
		if _, ok := ignoredHeaderNames[k]; ok {
			assert.Fail(t, fmt.Sprintf("header filed: %s must be removed", k))
		}
	}
}

func BenchmarkCopyRequest(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = dummyProxy.copyRequest(dummyRequest)
	}
}

func BenchmarkRewriteHost(b *testing.B) {
	status := &Status{Code: http.StatusOK}
	originalReq, _ := http.NewRequest("GET", "/dummy", nil)
	originalReq.URL.Host = "example.com.ccnproxy:3000"
	originalReq.Host = originalReq.URL.Host
	req, _ := http.NewRequest("GET", "/dummy", nil)
	req.URL.Scheme = "http"
	for n := 0; n < b.N; n++ {
		dummyProxy.rewriteProxyHost(originalReq, req, status)
	}
}

func TestRewriteHost(t *testing.T) {
	cases := []struct {
		originalReqHost string
		reqHost         string
		scheme          string
	}{
		{"example.com.ccnproxy:3000", "example.com:3000", "http"},
		{"example.com.ccnproxy", "example.com", "http"},
		{"example.com.ccnproxy.local:3000", "example.com:3000", "http"},
		{"example.com.ccnproxy.local", "example.com", "http"},
		{"example.com.ccnproxy-ssl:3000", "example.com:3000", "https"},
		{"example.com.ccnproxy-ssl", "example.com", "https"},
	}

	for _, c := range cases {
		t.Run(c.originalReqHost, func(t *testing.T) {
			status := &Status{Code: http.StatusOK}
			originalReq, _ := http.NewRequest("GET", "/dummy", nil)
			req, _ := http.NewRequest("GET", "/dummy", nil)
			req.URL.Scheme = "http"
			originalReq.URL.Host = c.originalReqHost
			originalReq.Host = originalReq.URL.Host
			dummyProxy.rewriteProxyHost(originalReq, req, status)
			assert.Equal(t, status.Code, http.StatusOK)
			assert.Equal(t, req.Host, c.reqHost)
			assert.Equal(t, req.URL.Scheme, c.scheme)
		})
	}
}
{'fraction_non_alphanumeric': 0.18351987649556156, 'fraction_numerical': 0.012543419529139328, 'mean_word_length': 3.5226876090750436, 'pattern_counts': {'":': 20, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1318863', 'n_tokens_mistral': 2076, 'n_tokens_neox': 1941, 'n_words': 390}