Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 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 TestMapInvalidDestination(t *testing.T) { |
| 69 | // see https://github.com/caddyserver/caddy/issues/8073 |
| 70 | failureCases := []struct { |
| 71 | name string |
| 72 | destination string |
| 73 | expectedError string |
| 74 | }{ |
| 75 | { |
| 76 | name: "trailing text", |
| 77 | destination: "{dest-name}suffix", |
| 78 | expectedError: "destination 0 must be a placeholder and only a placeholder, but got '{dest-name}suffix'", |
| 79 | }, |
| 80 | { |
| 81 | name: "missing closing brace", |
| 82 | destination: "{dest-name", |
| 83 | expectedError: "destination 0 must be a placeholder and only a placeholder, but got '{dest-name'", |
| 84 | }, |
| 85 | { |
| 86 | name: "double closing brace", |
| 87 | destination: "{dest-name}}", |
| 88 | expectedError: "destination 0 must be a placeholder and only a placeholder, but got '{dest-name}}'", |
| 89 | }, |
| 90 | } |
| 91 | |
| 92 | for _, tc := range failureCases { |
| 93 | t.Run(tc.name, func(t *testing.T) { |
| 94 | caddytest.AssertLoadError(t, ` |
| 95 | { |
| 96 | skip_install_trust |
| 97 | admin localhost:2999 |
| 98 | http_port 9080 |
| 99 | https_port 9443 |
| 100 | } |
| 101 | |
| 102 | localhost:9080 { |
| 103 | map {http.request.uri.path} `+tc.destination+` { |
| 104 | /foo mapped |
| 105 | } |
| 106 | respond "{result}" |
| 107 | } |
| 108 | `, "caddyfile", tc.expectedError) |
| 109 | }) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func TestMapAsJSON(t *testing.T) { |
| 114 | // arrange |
| 115 | tester := caddytest.NewTester(t) |
| 116 | tester.InitServer(` |
| 117 | { |
| 118 | "admin": { |
| 119 | "listen": "localhost:2999" |
| 120 | }, |
| 121 | "apps": { |
| 122 | "pki": { |
| 123 | "certificate_authorities" : { |
| 124 | "local" : { |
| 125 | "install_trust": false |
| 126 | } |
| 127 | } |
| 128 | }, |
| 129 | "http": { |
| 130 | "http_port": 9080, |
| 131 | "https_port": 9443, |
| 132 | "servers": { |
| 133 | "srv0": { |
| 134 | "listen": [ |
| 135 | ":9080" |
| 136 | ], |
| 137 | "routes": [ |
| 138 | { |
| 139 | "handle": [ |
| 140 | { |
| 141 | "handler": "subroute", |
| 142 | "routes": [ |
| 143 | { |
| 144 | "handle": [ |
| 145 | { |
| 146 | "handler": "map", |
| 147 | "source": "{http.request.method}", |
| 148 | "destinations": ["{dest-name}"], |
| 149 | "defaults": ["unknown"], |
| 150 | "mappings": [ |
| 151 | { |
| 152 | "input": "GET", |
| 153 | "outputs": ["get-called"] |
| 154 | }, |
| 155 | { |
| 156 | "input": "POST", |
| 157 | "outputs": ["post-called"] |
| 158 | } |
| 159 | ] |
| 160 | } |
| 161 | ] |
| 162 | }, |
| 163 | { |
| 164 | "handle": [ |
| 165 | { |
| 166 | "body": "hello from localhost {dest-name}", |
| 167 | "handler": "static_response", |
| 168 | "status_code": 200 |
| 169 | } |
| 170 | ], |
| 171 | "match": [ |
| 172 | { |
| 173 | "path": ["/version"] |
| 174 | } |
| 175 | ] |
| 176 | } |
| 177 | ] |
| 178 | } |
| 179 | ], |
| 180 | "match": [ |
| 181 | { |
| 182 | "host": ["localhost"] |
| 183 | } |
| 184 | ], |
| 185 | "terminal": true |
| 186 | } |
| 187 | ] |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | }`, "json") |
| 193 | |
| 194 | tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost get-called") |
| 195 | tester.AssertPostResponseBody("http://localhost:9080/version", []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost post-called") |
| 196 | } |