mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-06 10:58:49 +03:00
da8b7fe58f
* caddyhttp: Honor grace period in background This avoids blocking during config reloads. * Don't quit process until servers shut down * Make tests more likely to pass on fast CI (#5045) * caddyhttp: Even faster shutdowns Simultaneously shut down all HTTP servers, rather than one at a time. In practice there usually won't be more than 1 that lingers. But this code ensures that they all Shutdown() in their own goroutine and then we wait for them at the end (if exiting). We also wait for them to start up so we can be fairly confident the shutdowns have begun; i.e. old servers no longer accepting new connections. * Fix comment typo * Pull functions out of loop, for readability
137 lines
3 KiB
Go
137 lines
3 KiB
Go
package integration
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"github.com/caddyserver/caddy/v2/caddytest"
|
|
)
|
|
|
|
func TestMap(t *testing.T) {
|
|
// arrange
|
|
tester := caddytest.NewTester(t)
|
|
tester.InitServer(`{
|
|
http_port 9080
|
|
https_port 9443
|
|
grace_period 1
|
|
}
|
|
|
|
localhost:9080 {
|
|
|
|
map {http.request.method} {dest-1} {dest-2} {
|
|
default unknown1 unknown2
|
|
~G(.)(.) G${1}${2}-called
|
|
POST post-called foobar
|
|
}
|
|
|
|
respond /version 200 {
|
|
body "hello from localhost {dest-1} {dest-2}"
|
|
}
|
|
}
|
|
`, "caddyfile")
|
|
|
|
// act and assert
|
|
tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost GET-called unknown2")
|
|
tester.AssertPostResponseBody("http://localhost:9080/version", []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost post-called foobar")
|
|
}
|
|
|
|
func TestMapRespondWithDefault(t *testing.T) {
|
|
// arrange
|
|
tester := caddytest.NewTester(t)
|
|
tester.InitServer(`{
|
|
http_port 9080
|
|
https_port 9443
|
|
}
|
|
|
|
localhost:9080 {
|
|
|
|
map {http.request.method} {dest-name} {
|
|
default unknown
|
|
GET get-called
|
|
}
|
|
|
|
respond /version 200 {
|
|
body "hello from localhost {dest-name}"
|
|
}
|
|
}
|
|
`, "caddyfile")
|
|
|
|
// act and assert
|
|
tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost get-called")
|
|
tester.AssertPostResponseBody("http://localhost:9080/version", []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost unknown")
|
|
}
|
|
|
|
func TestMapAsJSON(t *testing.T) {
|
|
// arrange
|
|
tester := caddytest.NewTester(t)
|
|
tester.InitServer(`
|
|
{
|
|
"apps": {
|
|
"http": {
|
|
"http_port": 9080,
|
|
"https_port": 9443,
|
|
"servers": {
|
|
"srv0": {
|
|
"listen": [
|
|
":9080"
|
|
],
|
|
"routes": [
|
|
{
|
|
"handle": [
|
|
{
|
|
"handler": "subroute",
|
|
"routes": [
|
|
{
|
|
"handle": [
|
|
{
|
|
"handler": "map",
|
|
"source": "{http.request.method}",
|
|
"destinations": ["{dest-name}"],
|
|
"defaults": ["unknown"],
|
|
"mappings": [
|
|
{
|
|
"input": "GET",
|
|
"outputs": ["get-called"]
|
|
},
|
|
{
|
|
"input": "POST",
|
|
"outputs": ["post-called"]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"handle": [
|
|
{
|
|
"body": "hello from localhost {dest-name}",
|
|
"handler": "static_response",
|
|
"status_code": 200
|
|
}
|
|
],
|
|
"match": [
|
|
{
|
|
"path": ["/version"]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
],
|
|
"match": [
|
|
{
|
|
"host": ["localhost"]
|
|
}
|
|
],
|
|
"terminal": true
|
|
}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}`, "json")
|
|
|
|
tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost get-called")
|
|
tester.AssertPostResponseBody("http://localhost:9080/version", []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost post-called")
|
|
}
|