Skip to content

Commit

Permalink
add rewrite test
Browse files Browse the repository at this point in the history
  • Loading branch information
kazeburo committed Apr 9, 2020
1 parent 0a2c728 commit 731f6ed
Showing 1 changed file with 43 additions and 1 deletion.
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)
})
}
}

0 comments on commit 731f6ed

Please sign in to comment.