From 570d84f7d3642b85f96906ceab6402678177949e Mon Sep 17 00:00:00 2001 From: Mark Sargent <99003+sarge@users.noreply.github.com> Date: Mon, 27 Apr 2020 13:23:46 +1200 Subject: refactored caddytest helpers (#3285) * refactored caddytest helpers * added cookie jar support. Added support for more http verbs --- caddytest/integration/caddyfile_test.go | 45 ++++++++++++++++++++++++++---- caddytest/integration/sni_test.go | 15 ++++++---- caddytest/integration/testdata/cookie.html | 1 + 3 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 caddytest/integration/testdata/cookie.html (limited to 'caddytest/integration') diff --git a/caddytest/integration/caddyfile_test.go b/caddytest/integration/caddyfile_test.go index dd3dcba..4e9bdd9 100644 --- a/caddytest/integration/caddyfile_test.go +++ b/caddytest/integration/caddyfile_test.go @@ -1,6 +1,8 @@ package integration import ( + "net/http" + "net/url" "testing" "github.com/caddyserver/caddy/v2/caddytest" @@ -9,7 +11,8 @@ import ( func TestRespond(t *testing.T) { // arrange - caddytest.InitServer(t, ` + tester := caddytest.NewTester(t) + tester.InitServer(` { http_port 9080 https_port 9443 @@ -23,13 +26,14 @@ func TestRespond(t *testing.T) { `, "caddyfile") // act and assert - caddytest.AssertGetResponse(t, "http://localhost:9080/version", 200, "hello from localhost") + tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost") } func TestRedirect(t *testing.T) { // arrange - caddytest.InitServer(t, ` + tester := caddytest.NewTester(t) + tester.InitServer(` { http_port 9080 https_port 9443 @@ -46,10 +50,10 @@ func TestRedirect(t *testing.T) { `, "caddyfile") // act and assert - caddytest.AssertRedirect(t, "http://localhost:9080/", "http://localhost:9080/hello", 301) + tester.AssertRedirect("http://localhost:9080/", "http://localhost:9080/hello", 301) // follow redirect - caddytest.AssertGetResponse(t, "http://localhost:9080/", 200, "hello from localhost") + tester.AssertGetResponse("http://localhost:9080/", 200, "hello from localhost") } func TestDuplicateHosts(t *testing.T) { @@ -66,3 +70,34 @@ func TestDuplicateHosts(t *testing.T) { "caddyfile", "duplicate site address not allowed") } + +func TestReadCookie(t *testing.T) { + + localhost, _ := url.Parse("http://localhost") + cookie := http.Cookie{ + Name: "clientname", + Value: "caddytest", + } + + // arrange + tester := caddytest.NewTester(t) + tester.Client.Jar.SetCookies(localhost, []*http.Cookie{&cookie}) + tester.InitServer(` + { + http_port 9080 + https_port 9443 + } + + localhost:9080 { + templates { + root testdata + } + file_server { + root testdata + } + } + `, "caddyfile") + + // act and assert + tester.AssertGetResponse("http://localhost:9080/cookie.html", 200, "

Cookie.ClientName caddytest

") +} diff --git a/caddytest/integration/sni_test.go b/caddytest/integration/sni_test.go index f26131e..46f7c83 100644 --- a/caddytest/integration/sni_test.go +++ b/caddytest/integration/sni_test.go @@ -9,7 +9,8 @@ import ( func TestDefaultSNI(t *testing.T) { // arrange - caddytest.InitServer(t, `{ + tester := caddytest.NewTester(t) + tester.InitServer(`{ "apps": { "http": { "http_port": 9080, @@ -98,13 +99,14 @@ func TestDefaultSNI(t *testing.T) { // act and assert // makes a request with no sni - caddytest.AssertGetResponse(t, "https://127.0.0.1:9443/version", 200, "hello from a") + tester.AssertGetResponse("https://127.0.0.1:9443/version", 200, "hello from a") } func TestDefaultSNIWithNamedHostAndExplicitIP(t *testing.T) { // arrange - caddytest.InitServer(t, ` + tester := caddytest.NewTester(t) + tester.InitServer(` { "apps": { "http": { @@ -198,13 +200,14 @@ func TestDefaultSNIWithNamedHostAndExplicitIP(t *testing.T) { // act and assert // makes a request with no sni - caddytest.AssertGetResponse(t, "https://127.0.0.1:9443/version", 200, "hello from a") + tester.AssertGetResponse("https://127.0.0.1:9443/version", 200, "hello from a") } func TestDefaultSNIWithPortMappingOnly(t *testing.T) { // arrange - caddytest.InitServer(t, ` + tester := caddytest.NewTester(t) + tester.InitServer(` { "apps": { "http": { @@ -270,7 +273,7 @@ func TestDefaultSNIWithPortMappingOnly(t *testing.T) { // act and assert // makes a request with no sni - caddytest.AssertGetResponse(t, "https://127.0.0.1:9443/version", 200, "hello from a") + tester.AssertGetResponse("https://127.0.0.1:9443/version", 200, "hello from a") } func TestHttpOnlyOnDomainWithSNI(t *testing.T) { diff --git a/caddytest/integration/testdata/cookie.html b/caddytest/integration/testdata/cookie.html new file mode 100644 index 0000000..fa53a39 --- /dev/null +++ b/caddytest/integration/testdata/cookie.html @@ -0,0 +1 @@ +

Cookie.ClientName {{.Cookie "clientname"}}

\ No newline at end of file -- cgit v1.2.3