2020-06-27 00:12:37 +03:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2/caddytest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMap(t *testing.T) {
|
|
|
|
// arrange
|
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`{
|
2022-09-24 22:00:55 +03:00
|
|
|
skip_install_trust
|
|
|
|
admin localhost:2999
|
2020-06-27 00:12:37 +03:00
|
|
|
http_port 9080
|
|
|
|
https_port 9443
|
2022-09-24 22:00:55 +03:00
|
|
|
grace_period 1ns
|
2020-06-27 00:12:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
localhost:9080 {
|
|
|
|
|
2020-10-02 23:23:40 +03:00
|
|
|
map {http.request.method} {dest-1} {dest-2} {
|
2020-10-03 00:23:52 +03:00
|
|
|
default unknown1 unknown2
|
2021-03-11 00:22:33 +03:00
|
|
|
~G(.)(.) G${1}${2}-called
|
2020-10-02 23:23:40 +03:00
|
|
|
POST post-called foobar
|
2020-06-27 00:12:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
respond /version 200 {
|
2020-10-02 23:23:40 +03:00
|
|
|
body "hello from localhost {dest-1} {dest-2}"
|
2020-06-27 00:12:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`, "caddyfile")
|
|
|
|
|
|
|
|
// act and assert
|
2021-03-11 00:22:33 +03:00
|
|
|
tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost GET-called unknown2")
|
2020-10-02 23:23:40 +03:00
|
|
|
tester.AssertPostResponseBody("http://localhost:9080/version", []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost post-called foobar")
|
2020-06-27 00:12:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMapRespondWithDefault(t *testing.T) {
|
|
|
|
// arrange
|
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`{
|
2022-09-24 22:00:55 +03:00
|
|
|
skip_install_trust
|
|
|
|
admin localhost:2999
|
2020-06-27 00:12:37 +03:00
|
|
|
http_port 9080
|
|
|
|
https_port 9443
|
|
|
|
}
|
|
|
|
|
|
|
|
localhost:9080 {
|
|
|
|
|
2020-10-02 23:23:40 +03:00
|
|
|
map {http.request.method} {dest-name} {
|
2020-06-27 00:12:37 +03:00
|
|
|
default unknown
|
2020-10-02 23:23:40 +03:00
|
|
|
GET get-called
|
2020-06-27 00:12:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-09-02 06:40:15 +03:00
|
|
|
func TestMapAsJSON(t *testing.T) {
|
2020-06-27 00:12:37 +03:00
|
|
|
// arrange
|
|
|
|
tester := caddytest.NewTester(t)
|
2020-10-02 23:23:40 +03:00
|
|
|
tester.InitServer(`
|
|
|
|
{
|
2022-09-24 22:00:55 +03:00
|
|
|
"admin": {
|
|
|
|
"listen": "localhost:2999"
|
|
|
|
},
|
2020-06-27 00:12:37 +03:00
|
|
|
"apps": {
|
2022-09-24 22:00:55 +03:00
|
|
|
"pki": {
|
|
|
|
"certificate_authorities" : {
|
|
|
|
"local" : {
|
|
|
|
"install_trust": false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-06-27 00:12:37 +03:00
|
|
|
"http": {
|
2020-10-02 23:23:40 +03:00
|
|
|
"http_port": 9080,
|
|
|
|
"https_port": 9443,
|
|
|
|
"servers": {
|
|
|
|
"srv0": {
|
|
|
|
"listen": [
|
|
|
|
":9080"
|
|
|
|
],
|
2020-06-27 00:12:37 +03:00
|
|
|
"routes": [
|
|
|
|
{
|
2020-10-02 23:23:40 +03:00
|
|
|
"handle": [
|
2020-06-27 00:12:37 +03:00
|
|
|
{
|
2020-10-02 23:23:40 +03:00
|
|
|
"handler": "subroute",
|
|
|
|
"routes": [
|
|
|
|
{
|
|
|
|
"handle": [
|
|
|
|
{
|
|
|
|
"handler": "map",
|
|
|
|
"source": "{http.request.method}",
|
2022-09-02 06:40:15 +03:00
|
|
|
"destinations": ["{dest-name}"],
|
2020-10-02 23:23:40 +03:00
|
|
|
"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": [
|
2020-06-27 00:12:37 +03:00
|
|
|
{
|
2020-10-02 23:23:40 +03:00
|
|
|
"host": ["localhost"]
|
2020-06-27 00:12:37 +03:00
|
|
|
}
|
2020-10-02 23:23:40 +03:00
|
|
|
],
|
|
|
|
"terminal": true
|
2020-06-27 00:12:37 +03:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 23:23:40 +03:00
|
|
|
}`, "json")
|
2020-06-27 00:12:37 +03:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|