(t *testing.T)
| 107 | } |
| 108 | |
| 109 | func TestServer_AllowedHosts(t *testing.T) { |
| 110 | cases := []struct { |
| 111 | name string |
| 112 | allowedHosts []string |
| 113 | hostHeader string |
| 114 | expectedStatusCode int |
| 115 | expectedErrorMsg string |
| 116 | validationErrorMsg string |
| 117 | }{ |
| 118 | { |
| 119 | name: "wildcard hosts - any host allowed", |
| 120 | allowedHosts: []string{"*"}, |
| 121 | hostHeader: "example.com", |
| 122 | expectedStatusCode: http.StatusOK, |
| 123 | }, |
| 124 | { |
| 125 | name: "wildcard hosts - another host allowed", |
| 126 | allowedHosts: []string{"*"}, |
| 127 | hostHeader: "malicious.com", |
| 128 | expectedStatusCode: http.StatusOK, |
| 129 | }, |
| 130 | { |
| 131 | name: "specific hosts - valid host allowed", |
| 132 | allowedHosts: []string{"localhost", "app.example.com"}, |
| 133 | hostHeader: "localhost:3000", |
| 134 | expectedStatusCode: http.StatusOK, |
| 135 | }, |
| 136 | { |
| 137 | name: "specific hosts - another valid host allowed", |
| 138 | allowedHosts: []string{"localhost", "app.example.com"}, |
| 139 | hostHeader: "app.example.com", |
| 140 | expectedStatusCode: http.StatusOK, |
| 141 | }, |
| 142 | { |
| 143 | name: "specific hosts - invalid host rejected", |
| 144 | allowedHosts: []string{"localhost", "app.example.com"}, |
| 145 | hostHeader: "malicious.com", |
| 146 | expectedStatusCode: http.StatusBadRequest, |
| 147 | expectedErrorMsg: "Invalid host header. Allowed hosts: localhost, app.example.com", |
| 148 | }, |
| 149 | { |
| 150 | name: "ipv6 bracketed configured allowed - with port", |
| 151 | allowedHosts: []string{"[2001:db8::1]"}, |
| 152 | hostHeader: "[2001:db8::1]:80", |
| 153 | expectedStatusCode: http.StatusOK, |
| 154 | }, |
| 155 | { |
| 156 | name: "ipv6 literal invalid host rejected", |
| 157 | allowedHosts: []string{"[2001:db8::1]"}, |
| 158 | hostHeader: "[2001:db8::2]", |
| 159 | expectedStatusCode: http.StatusBadRequest, |
| 160 | expectedErrorMsg: "Invalid host header. Allowed hosts: 2001:db8::1", |
| 161 | }, |
| 162 | { |
| 163 | name: "allowed hosts must not be empty", |
| 164 | allowedHosts: []string{}, |
| 165 | validationErrorMsg: "the list must not be empty", |
| 166 | }, |
nothing calls this directly
no test coverage detected