caddy/caddytest/integration/map_test.go

153 lines
3.7 KiB
Go
Raw Normal View History

package integration
import (
"bytes"
2024-06-18 23:45:54 -05:00
"fmt"
"testing"
"github.com/caddyserver/caddy/v2/caddytest"
)
func TestMap(t *testing.T) {
// arrange
2024-06-18 23:45:54 -05:00
harness := caddytest.StartHarness(t)
harness.LoadConfig(`{
2022-09-24 22:00:55 +03:00
skip_install_trust
2024-06-18 23:45:54 -05:00
admin {$TESTING_CADDY_ADMIN_BIND}
http_port {$TESTING_CADDY_PORT_ONE}
https_port {$TESTING_CADDY_PORT_TWO}
2022-09-24 22:00:55 +03:00
grace_period 1ns
}
2024-06-18 23:45:54 -05:00
localhost:{$TESTING_CADDY_PORT_ONE} {
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}"
2024-06-18 23:45:54 -05:00
}
}
`, "caddyfile")
// act and assert
2024-06-18 23:45:54 -05:00
harness.AssertGetResponse(fmt.Sprintf("http://localhost:%d/version", harness.Tester().PortOne()), 200, "hello from localhost GET-called unknown2")
harness.AssertPostResponseBody(fmt.Sprintf("http://localhost:%d/version", harness.Tester().PortOne()), []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost post-called foobar")
}
func TestMapRespondWithDefault(t *testing.T) {
// arrange
2024-06-18 23:45:54 -05:00
harness := caddytest.StartHarness(t)
harness.LoadConfig(`{
2022-09-24 22:00:55 +03:00
skip_install_trust
2024-06-18 23:45:54 -05:00
admin {$TESTING_CADDY_ADMIN_BIND}
http_port {$TESTING_CADDY_PORT_ONE}
https_port {$TESTING_CADDY_PORT_TWO}
}
2024-06-18 23:45:54 -05:00
localhost:{$TESTING_CADDY_PORT_ONE} {
map {http.request.method} {dest-name} {
default unknown
GET get-called
}
2024-06-18 23:45:54 -05:00
respond /version 200 {
body "hello from localhost {dest-name}"
2024-06-18 23:45:54 -05:00
}
}
`, "caddyfile")
// act and assert
2024-06-18 23:45:54 -05:00
harness.AssertGetResponse(fmt.Sprintf("http://localhost:%d/version", harness.Tester().PortOne()), 200, "hello from localhost get-called")
harness.AssertPostResponseBody(fmt.Sprintf("http://localhost:%d/version", harness.Tester().PortOne()), []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost unknown")
}
2022-09-01 21:40:15 -06:00
func TestMapAsJSON(t *testing.T) {
// arrange
2024-06-18 23:45:54 -05:00
harness := caddytest.StartHarness(t)
harness.LoadConfig(`
{
2022-09-24 22:00:55 +03:00
"admin": {
2024-06-18 23:45:54 -05:00
"listen": "{$TESTING_CADDY_ADMIN_BIND}"
2022-09-24 22:00:55 +03:00
},
"apps": {
2022-09-24 22:00:55 +03:00
"pki": {
"certificate_authorities" : {
"local" : {
"install_trust": false
}
}
},
"http": {
2024-06-18 23:45:54 -05:00
"http_port": {$TESTING_CADDY_PORT_ONE},
"https_port": {$TESTING_CADDY_PORT_TWO},
"servers": {
"srv0": {
"listen": [
2024-06-18 23:45:54 -05:00
":{$TESTING_CADDY_PORT_ONE}"
],
"routes": [
{
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "map",
"source": "{http.request.method}",
2022-09-01 21:40:15 -06:00
"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")
2024-06-18 23:45:54 -05:00
target := fmt.Sprintf("http://localhost:%d/version", harness.Tester().PortOne())
harness.AssertGetResponse(target, 200, "hello from localhost get-called")
harness.AssertPostResponseBody(target, []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost post-called")
}