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

add rewrite test #42

Merged
merged 1 commit into from
Apr 9, 2020
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
44 changes: 43 additions & 1 deletion proxy/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestCopyRequest(t *testing.T) {
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 {
for k := range req.Header {
if _, ok := ignoredHeaderNames[k]; ok {
assert.Fail(t, fmt.Sprintf("header filed: %s must be removed", k))
}
Expand All @@ -82,3 +82,45 @@ func BenchmarkCopyRequest(b *testing.B) {
_ = 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)
})
}
}