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, "