|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2022-2024, FusionAuth, All Rights Reserved |
| 2 | + * Copyright (c) 2022-2025, FusionAuth, All Rights Reserved |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -104,6 +104,36 @@ public void getBaseURL() {
|
104 | 104 | "X-Forwarded-Proto", "http");
|
105 | 105 | }
|
106 | 106 |
|
| 107 | + @Test |
| 108 | + public void getHost() { |
| 109 | + // Single host |
| 110 | + assertGetHost("acme.com", "acme.com"); |
| 111 | + |
| 112 | + // Single host with port |
| 113 | + assertGetHost("acme.com:42", "acme.com"); |
| 114 | + |
| 115 | + // Multiple hosts |
| 116 | + assertGetHost("acme.com, example.com", "acme.com"); |
| 117 | + |
| 118 | + // Multiple hosts with spacing |
| 119 | + assertGetHost("acme.com , example.com ", "acme.com"); |
| 120 | + |
| 121 | + // Multiple hosts with spacing and ports |
| 122 | + assertGetHost("acme.com:41 , example.com:42 ", "acme.com"); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void getIPAddress() { |
| 127 | + // Single IP |
| 128 | + assertGetIPAddress("192.168.1.1", "192.168.1.1"); |
| 129 | + |
| 130 | + // Multiple IPs |
| 131 | + assertGetIPAddress("192.168.1.1, 192.168.1.2", "192.168.1.1"); |
| 132 | + |
| 133 | + // Multiple IPs with spacing |
| 134 | + assertGetIPAddress("192.168.1.1 , 192.168.1.2 ", "192.168.1.1"); |
| 135 | + } |
| 136 | + |
107 | 137 | @Test
|
108 | 138 | public void hostHeaderPortHandling() {
|
109 | 139 | // positive cases
|
@@ -203,6 +233,18 @@ private void assertBaseURL(String expected, String... headers) {
|
203 | 233 | assertBaseURL(expected, null, headers);
|
204 | 234 | }
|
205 | 235 |
|
| 236 | + private void assertGetHost(String header, @SuppressWarnings("SameParameterValue") String expected) { |
| 237 | + HTTPRequest request = new HTTPRequest(); |
| 238 | + request.setHeader(Headers.XForwardedHost, header); |
| 239 | + assertEquals(request.getHost(), expected); |
| 240 | + } |
| 241 | + |
| 242 | + private void assertGetIPAddress(String header, @SuppressWarnings("SameParameterValue") String expected) { |
| 243 | + HTTPRequest request = new HTTPRequest(); |
| 244 | + request.setHeader(Headers.XForwardedFor, header); |
| 245 | + assertEquals(request.getIPAddress(), expected); |
| 246 | + } |
| 247 | + |
206 | 248 | private void assertURLs(String scheme, String source, String host, int port, String baseURL) {
|
207 | 249 | HTTPRequest request = new HTTPRequest();
|
208 | 250 | request.setScheme(scheme);
|
|
0 commit comments