| 1 | package integration |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/caddyserver/caddy/v2/caddytest" |
| 8 | ) |
| 9 | |
| 10 | func TestMap(t *testing.T) { |
| 11 | // arrange |
| 12 | tester := caddytest.NewTester(t) |
| 13 | tester.InitServer(`{ |
| 14 | skip_install_trust |
| 15 | admin localhost:2999 |
| 16 | http_port 9080 |
| 17 | https_port 9443 |
| 18 | grace_period 1ns |
| 19 | } |
| 20 | |
| 21 | localhost:9080 { |
| 22 | |
| 23 | map {http.request.method} {dest-1} {dest-2} { |
| 24 | default unknown1 unknown2 |
| 25 | ~G(.)(.) G${1}${2}-called |
| 26 | POST post-called foobar |
| 27 | } |
| 28 | |
| 29 | respond /version 200 { |
| 30 | body "hello from localhost {dest-1} {dest-2}" |
| 31 | } |
| 32 | } |
| 33 | `, "caddyfile") |
| 34 | |
| 35 | // act and assert |
| 36 | tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost GET-called unknown2") |
| 37 | tester.AssertPostResponseBody("http://localhost:9080/version", []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost post-called foobar") |
| 38 | } |
| 39 | |
| 40 | func TestMapRespondWithDefault(t *testing.T) { |
| 41 | // arrange |
| 42 | tester := caddytest.NewTester(t) |
| 43 | tester.InitServer(`{ |
| 44 | skip_install_trust |
| 45 | admin localhost:2999 |
| 46 | http_port 9080 |
| 47 | https_port 9443 |
| 48 | } |
| 49 | |
| 50 | localhost:9080 { |
| 51 | |
| 52 | map {http.request.method} {dest-name} { |
| 53 | default unknown |
| 54 | GET get-called |
| 55 | } |
| 56 | |
| 57 | respond /version 200 { |
| 58 | body "hello from localhost {dest-name}" |
| 59 | } |
| 60 | } |
| 61 | `, "caddyfile") |
| 62 | |
| 63 | // act and assert |
| 64 | tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost get-called") |
| 65 | tester.AssertPostResponseBody("http://localhost:9080/version", []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost unknown") |
| 66 | } |
| 67 | |
| 68 | func TestMapAsJSON(t *testing.T) { |
| 69 | // arrange |
| 70 | tester := caddytest.NewTester(t) |
| 71 | tester.InitServer(` |
| 72 | { |
| 73 | "admin": { |
| 74 | "listen": "localhost:2999" |
| 75 | }, |
| 76 | "apps": { |
| 77 | "pki": { |
| 78 | "certificate_authorities" : { |
| 79 | "local" : { |
| 80 | "install_trust": false |
| 81 | } |
| 82 | } |
| 83 | }, |
| 84 | "http": { |
| 85 | "http_port": 9080, |
| 86 | "https_port": 9443, |
| 87 | "servers": { |
| 88 | "srv0": { |
| 89 | "listen": [ |
| 90 | ":9080" |
| 91 | ], |
| 92 | "routes": [ |
| 93 | { |
| 94 | "handle": [ |
| 95 | { |
| 96 | "handler": "subroute", |
| 97 | "routes": [ |
| 98 | { |
| 99 | "handle": [ |
| 100 | { |
| 101 | "handler": "map", |
| 102 | "source": "{http.request.method}", |
| 103 | "destinations": ["{dest-name}"], |
| 104 | "defaults": ["unknown"], |
| 105 | "mappings": [ |
| 106 | { |
| 107 | "input": "GET", |
| 108 | "outputs": ["get-called"] |
| 109 | }, |
| 110 | { |
| 111 | "input": "POST", |
| 112 | "outputs": ["post-called"] |
| 113 | } |
| 114 | ] |
| 115 | } |
| 116 | ] |
| 117 | }, |
| 118 | { |
| 119 | "handle": [ |
| 120 | { |
| 121 | "body": "hello from localhost {dest-name}", |
| 122 | "handler": "static_response", |
| 123 | "status_code": 200 |
| 124 | } |
| 125 | ], |
| 126 | "match": [ |
| 127 | { |
| 128 | "path": ["/version"] |
| 129 | } |
| 130 | ] |
| 131 | } |
| 132 | ] |
| 133 | } |
| 134 | ], |
| 135 | "match": [ |
| 136 | { |
| 137 | "host": ["localhost"] |
| 138 | } |
| 139 | ], |
| 140 | "terminal": true |
| 141 | } |
| 142 | ] |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | }`, "json") |
| 148 | |
| 149 | tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost get-called") |
| 150 | tester.AssertPostResponseBody("http://localhost:9080/version", []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost post-called") |
| 151 | } |