@@ -9,6 +9,7 @@ def setup
9
9
end
10
10
11
11
def test_basic_rewrites
12
+ log_tail = LogTail . new ( "nginx/current" )
12
13
override_config ( {
13
14
"hosts" => [
14
15
{
@@ -24,6 +25,8 @@ def test_basic_rewrites
24
25
} ,
25
26
] ,
26
27
} , "--router" ) do
28
+ refute_nginx_duplicate_server_name_warnings ( log_tail )
29
+
27
30
# Basic rewrite
28
31
response = Typhoeus . get ( "https://127.0.0.1:9081/#{ unique_test_id } /hello/rewrite?foo=bar" , http_options )
29
32
assert_response_code ( 301 , response )
@@ -37,6 +40,7 @@ def test_basic_rewrites
37
40
end
38
41
39
42
def test_default_host
43
+ log_tail = LogTail . new ( "nginx/current" )
40
44
override_config ( {
41
45
"hosts" => [
42
46
{
@@ -51,6 +55,8 @@ def test_default_host
51
55
} ,
52
56
] ,
53
57
} , "--router" ) do
58
+ refute_nginx_duplicate_server_name_warnings ( log_tail )
59
+
54
60
# Known host without rewrites
55
61
response = Typhoeus . get ( "https://127.0.0.1:9081/#{ unique_test_id } /hello/rewrite?foo=bar" , http_options . deep_merge ( {
56
62
:headers => { "Host" => "default.foo" } ,
@@ -74,6 +80,7 @@ def test_default_host
74
80
end
75
81
76
82
def test_no_default_host
83
+ log_tail = LogTail . new ( "nginx/current" )
77
84
override_config ( {
78
85
"hosts" => [
79
86
{
@@ -84,6 +91,8 @@ def test_no_default_host
84
91
} ,
85
92
] ,
86
93
} , "--router" ) do
94
+ refute_nginx_duplicate_server_name_warnings ( log_tail )
95
+
87
96
# Known host without rewrites
88
97
response = Typhoeus . get ( "https://127.0.0.1:9081/#{ unique_test_id } /hello/rewrite?foo=bar" , http_options . deep_merge ( {
89
98
:headers => { "Host" => "default.foo" } ,
@@ -99,7 +108,138 @@ def test_no_default_host
99
108
end
100
109
end
101
110
111
+ # When nginx has no default_server defined, then the first server defined
112
+ # becomes the default one. So test to see how this ordering also interacts
113
+ # with a wildcard host.
114
+ def test_no_default_host_wildcard_first
115
+ log_tail = LogTail . new ( "nginx/current" )
116
+ override_config ( {
117
+ "hosts" => [
118
+ {
119
+ "hostname" => "*" ,
120
+ "rewrites" => [
121
+ "^/#{ unique_test_id } /hello/rewrite https://example.com/something/ permanent" ,
122
+ ] ,
123
+ } ,
124
+ {
125
+ "hostname" => "known.foo" ,
126
+ } ,
127
+ ] ,
128
+ } , "--router" ) do
129
+ refute_nginx_duplicate_server_name_warnings ( log_tail )
130
+
131
+ # Known host without rewrites
132
+ response = Typhoeus . get ( "https://127.0.0.1:9081/#{ unique_test_id } /hello/rewrite?foo=bar" , http_options . deep_merge ( {
133
+ :headers => { "Host" => "known.foo" } ,
134
+ } ) )
135
+ assert_response_code ( 404 , response )
136
+
137
+ # Unknown host
138
+ response = Typhoeus . get ( "https://127.0.0.1:9081/#{ unique_test_id } /hello/rewrite?foo=bar" , http_options . deep_merge ( {
139
+ :headers => { "Host" => "unknown.foo" } ,
140
+ } ) )
141
+ assert_response_code ( 404 , response )
142
+ end
143
+ end
144
+
145
+ def test_no_default_host_wildcard_last
146
+ log_tail = LogTail . new ( "nginx/current" )
147
+ override_config ( {
148
+ "hosts" => [
149
+ {
150
+ "hostname" => "known.foo" ,
151
+ } ,
152
+ {
153
+ "hostname" => "*" ,
154
+ "rewrites" => [
155
+ "^/#{ unique_test_id } /hello/rewrite https://example.com/something/ permanent" ,
156
+ ] ,
157
+ } ,
158
+ ] ,
159
+ } , "--router" ) do
160
+ refute_nginx_duplicate_server_name_warnings ( log_tail )
161
+
162
+ # Known host without rewrites
163
+ response = Typhoeus . get ( "https://127.0.0.1:9081/#{ unique_test_id } /hello/rewrite?foo=bar" , http_options . deep_merge ( {
164
+ :headers => { "Host" => "known.foo" } ,
165
+ } ) )
166
+ assert_response_code ( 404 , response )
167
+
168
+ # Unknown host
169
+ response = Typhoeus . get ( "https://127.0.0.1:9081/#{ unique_test_id } /hello/rewrite?foo=bar" , http_options . deep_merge ( {
170
+ :headers => { "Host" => "unknown.foo" } ,
171
+ } ) )
172
+ assert_response_code ( 404 , response )
173
+ end
174
+ end
175
+
176
+ def test_wildcard_is_default
177
+ log_tail = LogTail . new ( "nginx/current" )
178
+ override_config ( {
179
+ "hosts" => [
180
+ {
181
+ "hostname" => "*" ,
182
+ "default" => true ,
183
+ "rewrites" => [
184
+ "^/#{ unique_test_id } /hello/rewrite https://example.com/something/ permanent" ,
185
+ ] ,
186
+ } ,
187
+ {
188
+ "hostname" => "known.foo" ,
189
+ } ,
190
+ ] ,
191
+ } , "--router" ) do
192
+ refute_nginx_duplicate_server_name_warnings ( log_tail )
193
+
194
+ # Known host without rewrites
195
+ response = Typhoeus . get ( "https://127.0.0.1:9081/#{ unique_test_id } /hello/rewrite?foo=bar" , http_options . deep_merge ( {
196
+ :headers => { "Host" => "known.foo" } ,
197
+ } ) )
198
+ assert_response_code ( 404 , response )
199
+
200
+ # Unknown host
201
+ response = Typhoeus . get ( "https://127.0.0.1:9081/#{ unique_test_id } /hello/rewrite?foo=bar" , http_options . deep_merge ( {
202
+ :headers => { "Host" => "unknown.foo" } ,
203
+ } ) )
204
+ assert_response_code ( 301 , response )
205
+ assert_equal ( "https://example.com/something/?foo=bar" , response . headers [ "location" ] )
206
+ end
207
+ end
208
+
209
+ def test_wildcard_is_not_default
210
+ log_tail = LogTail . new ( "nginx/current" )
211
+ override_config ( {
212
+ "hosts" => [
213
+ {
214
+ "hostname" => "*" ,
215
+ "rewrites" => [
216
+ "^/#{ unique_test_id } /hello/rewrite https://example.com/something/ permanent" ,
217
+ ] ,
218
+ } ,
219
+ {
220
+ "hostname" => "known.foo" ,
221
+ "default" => true ,
222
+ } ,
223
+ ] ,
224
+ } , "--router" ) do
225
+ refute_nginx_duplicate_server_name_warnings ( log_tail )
226
+
227
+ # Known host without rewrites
228
+ response = Typhoeus . get ( "https://127.0.0.1:9081/#{ unique_test_id } /hello/rewrite?foo=bar" , http_options . deep_merge ( {
229
+ :headers => { "Host" => "known.foo" } ,
230
+ } ) )
231
+ assert_response_code ( 404 , response )
232
+
233
+ # Unknown host
234
+ response = Typhoeus . get ( "https://127.0.0.1:9081/#{ unique_test_id } /hello/rewrite?foo=bar" , http_options . deep_merge ( {
235
+ :headers => { "Host" => "unknown.foo" } ,
236
+ } ) )
237
+ assert_response_code ( 404 , response )
238
+ end
239
+ end
240
+
102
241
def test_precedence
242
+ log_tail = LogTail . new ( "nginx/current" )
103
243
override_config ( {
104
244
"apis" => [
105
245
{
@@ -129,6 +269,8 @@ def test_precedence
129
269
} ,
130
270
] ,
131
271
} , "--router" ) do
272
+ refute_nginx_duplicate_server_name_warnings ( log_tail )
273
+
132
274
http_opts = http_options . deep_merge ( {
133
275
:headers => { "Host" => "with-apis-and-website.foo" } ,
134
276
} )
@@ -166,4 +308,13 @@ def test_precedence
166
308
assert_match ( "<center>API Umbrella</center>" , response . body )
167
309
end
168
310
end
311
+
312
+ private
313
+
314
+ def refute_nginx_duplicate_server_name_warnings ( log_tail )
315
+ log_output = log_tail . read
316
+ assert_match ( /\[ notice\] .*: reconfiguring/ , log_output )
317
+ refute_match ( "conflicting server name" , log_output )
318
+ refute_match ( "warn" , log_output )
319
+ end
169
320
end
0 commit comments