2020-06-27 09:12:37 +12:00
package integration
import (
"bytes"
2024-06-18 23:45:54 -05:00
"fmt"
2020-06-27 09:12:37 +12:00
"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 1 ns
2020-06-27 09:12:37 +12:00
}
2024-06-18 23:45:54 -05:00
localhost : { $ TESTING_CADDY_PORT_ONE } {
2020-06-27 09:12:37 +12:00
2020-10-02 14:23:40 -06:00
map { http . request . method } { dest - 1 } { dest - 2 } {
2020-10-02 15:23:52 -06:00
default unknown1 unknown2
2021-03-11 02:52:33 +05:30
~ G ( . ) ( . ) G $ { 1 } $ { 2 } - called
2020-10-02 14:23:40 -06:00
POST post - called foobar
2020-06-27 09:12:37 +12:00
}
respond / version 200 {
2020-10-02 14:23:40 -06:00
body "hello from localhost {dest-1} {dest-2}"
2024-06-18 23:45:54 -05:00
}
2020-06-27 09:12:37 +12: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" )
2020-06-27 09:12:37 +12:00
}
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 }
2020-06-27 09:12:37 +12:00
}
2024-06-18 23:45:54 -05:00
localhost : { $ TESTING_CADDY_PORT_ONE } {
2020-10-02 14:23:40 -06:00
map { http . request . method } { dest - name } {
2020-06-27 09:12:37 +12:00
default unknown
2020-10-02 14:23:40 -06:00
GET get - called
2020-06-27 09:12:37 +12:00
}
2024-06-18 23:45:54 -05:00
2020-06-27 09:12:37 +12:00
respond / version 200 {
body "hello from localhost {dest-name}"
2024-06-18 23:45:54 -05:00
}
2020-06-27 09:12:37 +12: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" )
2020-06-27 09:12:37 +12:00
}
2022-09-01 21:40:15 -06:00
func TestMapAsJSON ( t * testing . T ) {
2020-06-27 09:12:37 +12:00
// arrange
2024-06-18 23:45:54 -05:00
harness := caddytest . StartHarness ( t )
harness . LoadConfig ( `
2020-10-02 14:23:40 -06:00
{
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
} ,
2020-06-27 09:12:37 +12:00
"apps" : {
2022-09-24 22:00:55 +03:00
"pki" : {
"certificate_authorities" : {
"local" : {
"install_trust" : false
}
}
} ,
2020-06-27 09:12:37 +12:00
"http" : {
2024-06-18 23:45:54 -05:00
"http_port" : { $ TESTING_CADDY_PORT_ONE } ,
"https_port" : { $ TESTING_CADDY_PORT_TWO } ,
2020-10-02 14:23:40 -06:00
"servers" : {
"srv0" : {
"listen" : [
2024-06-18 23:45:54 -05:00
":{$TESTING_CADDY_PORT_ONE}"
2020-10-02 14:23:40 -06:00
] ,
2020-06-27 09:12:37 +12:00
"routes" : [
{
2020-10-02 14:23:40 -06:00
"handle" : [
2020-06-27 09:12:37 +12:00
{
2020-10-02 14:23:40 -06:00
"handler" : "subroute" ,
"routes" : [
{
"handle" : [
{
"handler" : "map" ,
"source" : "{http.request.method}" ,
2022-09-01 21:40:15 -06:00
"destinations" : [ "{dest-name}" ] ,
2020-10-02 14:23:40 -06: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 09:12:37 +12:00
{
2020-10-02 14:23:40 -06:00
"host" : [ "localhost" ]
2020-06-27 09:12:37 +12:00
}
2020-10-02 14:23:40 -06:00
] ,
"terminal" : true
2020-06-27 09:12:37 +12:00
}
]
}
}
}
}
2020-10-02 14:23:40 -06:00
} ` , "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" )
2020-06-27 09:12:37 +12:00
}