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 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package caddyhttp |
| 16 | |
| 17 | import ( |
| 18 | "net/http" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | func TestResponseMatcher(t *testing.T) { |
| 23 | for i, tc := range []struct { |
| 24 | require ResponseMatcher |
| 25 | status int |
| 26 | hdr http.Header // make sure these are canonical cased (std lib will do that in a real request) |
| 27 | expect bool |
| 28 | }{ |
| 29 | { |
| 30 | require: ResponseMatcher{}, |
| 31 | status: 200, |
| 32 | expect: true, |
| 33 | }, |
| 34 | { |
| 35 | require: ResponseMatcher{ |
| 36 | StatusCode: []int{200}, |
| 37 | }, |
| 38 | status: 200, |
| 39 | expect: true, |
| 40 | }, |
| 41 | { |
| 42 | require: ResponseMatcher{ |
| 43 | StatusCode: []int{2}, |
| 44 | }, |
| 45 | status: 200, |
| 46 | expect: true, |
| 47 | }, |
| 48 | { |
| 49 | require: ResponseMatcher{ |
| 50 | StatusCode: []int{201}, |
| 51 | }, |
| 52 | status: 200, |
| 53 | expect: false, |
| 54 | }, |
| 55 | { |
| 56 | require: ResponseMatcher{ |
| 57 | StatusCode: []int{2}, |
| 58 | }, |
| 59 | status: 301, |
| 60 | expect: false, |
| 61 | }, |
| 62 | { |
| 63 | require: ResponseMatcher{ |
| 64 | StatusCode: []int{3}, |
| 65 | }, |
| 66 | status: 301, |
| 67 | expect: true, |
| 68 | }, |
| 69 | { |
| 70 | require: ResponseMatcher{ |
| 71 | StatusCode: []int{3}, |
| 72 | }, |
| 73 | status: 399, |
| 74 | expect: true, |
| 75 | }, |
| 76 | { |
| 77 | require: ResponseMatcher{ |
| 78 | StatusCode: []int{3}, |
| 79 | }, |
| 80 | status: 400, |
| 81 | expect: false, |
| 82 | }, |
| 83 | { |
| 84 | require: ResponseMatcher{ |
| 85 | StatusCode: []int{3, 4}, |
| 86 | }, |
| 87 | status: 400, |
| 88 | expect: true, |
| 89 | }, |
| 90 | { |
| 91 | require: ResponseMatcher{ |
| 92 | StatusCode: []int{3, 401}, |
| 93 | }, |
| 94 | status: 401, |
| 95 | expect: true, |
| 96 | }, |
| 97 | { |
| 98 | require: ResponseMatcher{ |
| 99 | Headers: http.Header{ |
| 100 | "Foo": []string{"bar"}, |
| 101 | }, |
| 102 | }, |
| 103 | hdr: http.Header{"Foo": []string{"bar"}}, |
| 104 | expect: true, |
| 105 | }, |
| 106 | { |
| 107 | require: ResponseMatcher{ |
| 108 | Headers: http.Header{ |
| 109 | "Foo2": []string{"bar"}, |
| 110 | }, |
| 111 | }, |
| 112 | hdr: http.Header{"Foo": []string{"bar"}}, |
| 113 | expect: false, |
| 114 | }, |
| 115 | { |
| 116 | require: ResponseMatcher{ |
| 117 | Headers: http.Header{ |
| 118 | "Foo": []string{"bar", "baz"}, |
| 119 | }, |
| 120 | }, |
| 121 | hdr: http.Header{"Foo": []string{"baz"}}, |
| 122 | expect: true, |
| 123 | }, |
| 124 | { |
| 125 | require: ResponseMatcher{ |
| 126 | Headers: http.Header{ |
| 127 | "Foo": []string{"bar"}, |
| 128 | "Foo2": []string{"baz"}, |
| 129 | }, |
| 130 | }, |
| 131 | hdr: http.Header{"Foo": []string{"baz"}}, |
| 132 | expect: false, |
| 133 | }, |
| 134 | { |
| 135 | require: ResponseMatcher{ |
| 136 | Headers: http.Header{ |
| 137 | "Foo": []string{"bar"}, |
| 138 | "Foo2": []string{"baz"}, |
| 139 | }, |
| 140 | }, |
| 141 | hdr: http.Header{"Foo": []string{"bar"}, "Foo2": []string{"baz"}}, |
| 142 | expect: true, |
| 143 | }, |
| 144 | { |
| 145 | require: ResponseMatcher{ |
| 146 | Headers: http.Header{ |
| 147 | "Foo": []string{"foo*"}, |
| 148 | }, |
| 149 | }, |
| 150 | hdr: http.Header{"Foo": []string{"foobar"}}, |
| 151 | expect: true, |
| 152 | }, |
| 153 | { |
| 154 | require: ResponseMatcher{ |
| 155 | Headers: http.Header{ |
| 156 | "Foo": []string{"foo*"}, |
| 157 | }, |
| 158 | }, |
| 159 | hdr: http.Header{"Foo": []string{"foobar"}}, |
| 160 | expect: true, |
| 161 | }, |
| 162 | } { |
| 163 | actual := tc.require.Match(tc.status, tc.hdr) |
| 164 | if actual != tc.expect { |
| 165 | t.Errorf("Test %d %v: Expected %t, got %t for HTTP %d %v", i, tc.require, tc.expect, actual, tc.status, tc.hdr) |
| 166 | continue |
| 167 | } |
| 168 | } |
| 169 | } |