|
13 | 13 |
|
14 | 14 | import org.jsoup.Jsoup; |
15 | 15 | import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.params.ParameterizedTest; |
| 17 | +import org.junit.jupiter.params.provider.ValueSource; |
16 | 18 |
|
17 | 19 | import edu.ucla.library.iiif.auth.AccessTokenError; |
18 | 20 | import edu.ucla.library.iiif.auth.Config; |
@@ -51,26 +53,42 @@ public final class AccessTokenHandlerIT extends AbstractAccessTokenHandlerIT { |
51 | 53 | /** |
52 | 54 | * Tests that a browser client can use a valid access cookie to obtain an access token. |
53 | 55 | * |
| 56 | + * @param aReverseProxyDeployment Whether or not to simulate app deployment behind a reverse proxy |
54 | 57 | * @param aVertx A Vert.x instance |
55 | 58 | * @param aContext A test context |
56 | 59 | */ |
57 | | - @Test |
58 | | - public void testGetTokenBrowser(final Vertx aVertx, final VertxTestContext aContext) { |
| 60 | + @ParameterizedTest |
| 61 | + @ValueSource(booleans = { true, false }) |
| 62 | + public void testGetTokenBrowser(final boolean aReverseProxyDeployment, final Vertx aVertx, |
| 63 | + final VertxTestContext aContext) { |
59 | 64 | final String getCookieRequestURI = |
60 | 65 | StringUtils.format(GET_COOKIE_PATH, URLEncoder.encode(TEST_ORIGIN, StandardCharsets.UTF_8)); |
61 | 66 | final HttpRequest<?> getCookie = myWebClient.get(myPort, TestConstants.INADDR_ANY, getCookieRequestURI); |
62 | 67 |
|
| 68 | + if (aReverseProxyDeployment) { |
| 69 | + getCookie.putHeader(X_FORWARDED_FOR, FORWARDED_IP_ADDRESSES); |
| 70 | + } |
| 71 | + |
63 | 72 | getCookie.send().compose(result -> { |
64 | 73 | final String cookieHeader = result.cookies().get(0); |
65 | 74 | final String cookieValue = cookieHeader.split(EQUALS)[1]; |
66 | | - final String clientIpAddress = |
67 | | - Jsoup.parse(result.bodyAsString()).getElementById(myClientIpAddressID).text(); |
| 75 | + final String clientIpAddress; |
| 76 | + |
| 77 | + if (aReverseProxyDeployment) { |
| 78 | + clientIpAddress = FORWARDED_CLIENT_IP; |
| 79 | + } else { |
| 80 | + clientIpAddress = Jsoup.parse(result.bodyAsString()).getElementById(myClientIpAddressID).text(); |
| 81 | + } |
68 | 82 |
|
69 | 83 | return myAccessCookieService.decryptCookie(cookieValue, clientIpAddress).compose(cookie -> { |
70 | 84 | final String getTokenRequestURI = StringUtils.format(GET_TOKEN_PATH, myGetTokenRequestQuery); |
71 | 85 | final HttpRequest<?> getToken = myWebClient.get(myPort, TestConstants.INADDR_ANY, getTokenRequestURI) |
72 | 86 | .putHeader(HttpHeaders.COOKIE.toString(), cookieHeader); |
73 | 87 |
|
| 88 | + if (aReverseProxyDeployment) { |
| 89 | + getToken.putHeader(X_FORWARDED_FOR, FORWARDED_IP_ADDRESSES); |
| 90 | + } |
| 91 | + |
74 | 92 | return getToken.send().onSuccess(response -> { |
75 | 93 | final JsonObject expectedAccessTokenDecoded = |
76 | 94 | new JsonObject().put(TokenJsonKeys.VERSION, myConfig.getString(Config.HAUTH_VERSION)).put( |
@@ -106,26 +124,42 @@ public void testGetTokenBrowser(final Vertx aVertx, final VertxTestContext aCont |
106 | 124 | /** |
107 | 125 | * Tests that a non-browser client can use a valid access cookie to obtain an access token. |
108 | 126 | * |
| 127 | + * @param aReverseProxyDeployment Whether or not to simulate app deployment behind a reverse proxy |
109 | 128 | * @param aVertx A Vert.x instance |
110 | 129 | * @param aContext A test context |
111 | 130 | */ |
112 | | - @Test |
113 | | - public void testGetTokenNonBrowser(final Vertx aVertx, final VertxTestContext aContext) { |
| 131 | + @ParameterizedTest |
| 132 | + @ValueSource(booleans = { true, false }) |
| 133 | + public void testGetTokenNonBrowser(final boolean aReverseProxyDeployment, final Vertx aVertx, |
| 134 | + final VertxTestContext aContext) { |
114 | 135 | final String getCookieRequestURI = |
115 | 136 | StringUtils.format(GET_COOKIE_PATH, URLEncoder.encode(TEST_ORIGIN, StandardCharsets.UTF_8)); |
116 | 137 | final HttpRequest<?> getCookie = myWebClient.get(myPort, TestConstants.INADDR_ANY, getCookieRequestURI); |
117 | 138 |
|
| 139 | + if (aReverseProxyDeployment) { |
| 140 | + getCookie.putHeader(X_FORWARDED_FOR, FORWARDED_IP_ADDRESSES); |
| 141 | + } |
| 142 | + |
118 | 143 | getCookie.send().compose(result -> { |
119 | 144 | final String cookieHeader = result.cookies().get(0); |
120 | 145 | final String cookieValue = cookieHeader.split(EQUALS)[1]; |
121 | | - final String clientIpAddress = |
122 | | - Jsoup.parse(result.bodyAsString()).getElementById(myClientIpAddressID).text(); |
| 146 | + final String clientIpAddress; |
| 147 | + |
| 148 | + if (aReverseProxyDeployment) { |
| 149 | + clientIpAddress = FORWARDED_CLIENT_IP; |
| 150 | + } else { |
| 151 | + clientIpAddress = Jsoup.parse(result.bodyAsString()).getElementById(myClientIpAddressID).text(); |
| 152 | + } |
123 | 153 |
|
124 | 154 | return myAccessCookieService.decryptCookie(cookieValue, clientIpAddress).compose(cookie -> { |
125 | 155 | final String getTokenRequestURI = StringUtils.format(GET_TOKEN_PATH, EMPTY); |
126 | 156 | final HttpRequest<?> getToken = myWebClient.get(myPort, TestConstants.INADDR_ANY, getTokenRequestURI) |
127 | 157 | .putHeader(HttpHeaders.COOKIE.toString(), cookieHeader); |
128 | 158 |
|
| 159 | + if (aReverseProxyDeployment) { |
| 160 | + getToken.putHeader(X_FORWARDED_FOR, FORWARDED_IP_ADDRESSES); |
| 161 | + } |
| 162 | + |
129 | 163 | return getToken.send().onSuccess(response -> { |
130 | 164 | final JsonObject expectedAccessTokenDecoded = |
131 | 165 | new JsonObject().put(TokenJsonKeys.VERSION, myConfig.getString(Config.HAUTH_VERSION)).put( |
|
0 commit comments