2020-03-14 01:32:53 +08:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2020-04-27 09:23:46 +08:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-03-14 01:32:53 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2/caddytest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRespond(t *testing.T) {
|
|
|
|
|
|
|
|
// arrange
|
2020-04-27 09:23:46 +08:00
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`
|
2020-03-14 01:32:53 +08:00
|
|
|
{
|
|
|
|
http_port 9080
|
|
|
|
https_port 9443
|
|
|
|
}
|
|
|
|
|
|
|
|
localhost:9080 {
|
|
|
|
respond /version 200 {
|
2020-03-18 02:39:01 +08:00
|
|
|
body "hello from localhost"
|
2020-03-14 01:32:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`, "caddyfile")
|
|
|
|
|
|
|
|
// act and assert
|
2020-04-27 09:23:46 +08:00
|
|
|
tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost")
|
2020-03-14 01:32:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRedirect(t *testing.T) {
|
|
|
|
|
|
|
|
// arrange
|
2020-04-27 09:23:46 +08:00
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`
|
2020-03-14 01:32:53 +08:00
|
|
|
{
|
|
|
|
http_port 9080
|
|
|
|
https_port 9443
|
|
|
|
}
|
|
|
|
|
|
|
|
localhost:9080 {
|
|
|
|
|
|
|
|
redir / http://localhost:9080/hello 301
|
|
|
|
|
|
|
|
respond /hello 200 {
|
2020-03-18 02:39:01 +08:00
|
|
|
body "hello from localhost"
|
2020-03-14 01:32:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`, "caddyfile")
|
|
|
|
|
|
|
|
// act and assert
|
2020-04-27 09:23:46 +08:00
|
|
|
tester.AssertRedirect("http://localhost:9080/", "http://localhost:9080/hello", 301)
|
2020-03-14 01:32:53 +08:00
|
|
|
|
|
|
|
// follow redirect
|
2020-04-27 09:23:46 +08:00
|
|
|
tester.AssertGetResponse("http://localhost:9080/", 200, "hello from localhost")
|
2020-03-14 01:32:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicateHosts(t *testing.T) {
|
|
|
|
|
|
|
|
// act and assert
|
|
|
|
caddytest.AssertLoadError(t,
|
|
|
|
`
|
|
|
|
localhost:9080 {
|
|
|
|
}
|
|
|
|
|
|
|
|
localhost:9080 {
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
"caddyfile",
|
|
|
|
"duplicate site address not allowed")
|
|
|
|
}
|
2020-04-27 09:23:46 +08:00
|
|
|
|
|
|
|
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, "<h2>Cookie.ClientName caddytest</h2>")
|
|
|
|
}
|