|
| 1 | +/* |
| 2 | + * Elemental |
| 3 | + * Copyright (C) 2024, Evolved Binary Ltd |
| 4 | + * |
| 5 | + |
| 6 | + * https://www.evolvedbinary.com | https://www.elemental.xyz |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; version 2.1. |
| 11 | + * |
| 12 | + * This library is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + * Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public |
| 18 | + * License along with this library; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | + */ |
| 21 | +package org.exist.http.urlrewrite; |
| 22 | + |
| 23 | +import com.evolvedbinary.j8fu.tuple.Tuple2; |
| 24 | +import org.apache.http.HttpResponse; |
| 25 | +import org.apache.http.HttpStatus; |
| 26 | +import org.apache.http.client.fluent.Request; |
| 27 | +import org.apache.http.entity.ContentType; |
| 28 | +import org.exist.http.AbstractHttpTest; |
| 29 | +import org.exist.test.ExistWebServer; |
| 30 | +import org.exist.xmldb.XmldbURI; |
| 31 | +import org.junit.BeforeClass; |
| 32 | +import org.junit.ClassRule; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +import javax.annotation.Nullable; |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.function.Function; |
| 38 | + |
| 39 | +import static com.evolvedbinary.j8fu.tuple.Tuple.Tuple; |
| 40 | +import static org.exist.http.urlrewrite.XQueryURLRewrite.XQUERY_CONTROLLER_FILENAME; |
| 41 | +import static org.junit.Assert.assertEquals; |
| 42 | + |
| 43 | +/** |
| 44 | + * @author <a href="mailto:[email protected]">Adam Retter</a> |
| 45 | + */ |
| 46 | +public class RedirectTest extends AbstractHttpTest { |
| 47 | + |
| 48 | + private static final XmldbURI TEST_COLLECTION_NAME = XmldbURI.create("redirect-test"); |
| 49 | + private static final XmldbURI TEST_COLLECTION = XmldbURI.create("/db/apps").append(TEST_COLLECTION_NAME); |
| 50 | + |
| 51 | + private static final String TEST_CONTROLLER = |
| 52 | + "xquery version \"1.0\";\n" + |
| 53 | + "declare namespace exist = \"http://exist.sourceforge.net/NS/exist\";\n" + |
| 54 | + "declare namespace request = \"http://exist-db.org/xquery/request\";\n" + |
| 55 | + "let $redirect-type := request:get-parameter(\"redirect-type\", ())\n" + |
| 56 | + "return\n" + |
| 57 | + " element exist:dispatch {\n" + |
| 58 | + " element exist:redirect {\n" + |
| 59 | + " attribute url { \"http://elsewhere.dom\" },\n" + |
| 60 | + " $redirect-type ! attribute type { . }\n" + |
| 61 | + " }\n" + |
| 62 | + " }\n"; |
| 63 | + |
| 64 | + @ClassRule |
| 65 | + public static final ExistWebServer EXIST_WEB_SERVER = new ExistWebServer(true, false, true, true, false); |
| 66 | + |
| 67 | + @Test |
| 68 | + public void defaultForGetIsFound() throws IOException { |
| 69 | + testRedirect(Redirect.RedirectType.Found, null, Request::Get); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void defaultForHeadIsFound() throws IOException { |
| 74 | + testRedirect(Redirect.RedirectType.Found, null, Request::Head); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void defaultForPostIsSeeOther() throws IOException { |
| 79 | + testRedirect(Redirect.RedirectType.SeeOther, null, Request::Post); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void defaultForPatchIsSeeOther() throws IOException { |
| 84 | + testRedirect(Redirect.RedirectType.SeeOther, null, Request::Patch); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void defaultForPutIsSeeOther() throws IOException { |
| 89 | + testRedirect(Redirect.RedirectType.SeeOther, null, Request::Put); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void defaultForDeleteIsSeeOther() throws IOException { |
| 94 | + testRedirect(Redirect.RedirectType.SeeOther, null, Request::Delete); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void movedPermanently() throws IOException { |
| 99 | + testGetSendRedirect(Redirect.RedirectType.MovedPermanently); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void found() throws IOException { |
| 104 | + testGetSendRedirect(Redirect.RedirectType.Found); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void seeOther() throws IOException { |
| 109 | + testGetSendRedirect(Redirect.RedirectType.SeeOther); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void temporaryRedirect() throws IOException { |
| 114 | + testGetSendRedirect(Redirect.RedirectType.TemporaryRedirect); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void permanentRedirect() throws IOException { |
| 119 | + testGetSendRedirect(Redirect.RedirectType.TemporaryRedirect); |
| 120 | + } |
| 121 | + |
| 122 | + private void testGetSendRedirect(final Redirect.RedirectType redirectType) throws IOException { |
| 123 | + testGetRedirect(redirectType, redirectType); |
| 124 | + } |
| 125 | + |
| 126 | + private void testGetRedirect(final Redirect.RedirectType expectedRedirectTypeResponse, @Nullable final Redirect.RedirectType sendRedirectType) throws IOException { |
| 127 | + testRedirect(expectedRedirectTypeResponse, sendRedirectType, Request::Get); |
| 128 | + } |
| 129 | + |
| 130 | + private void testRedirect(final Redirect.RedirectType expectedRedirectTypeResponse, @Nullable final Redirect.RedirectType sendRedirectType, final Function<String, Request> requestFn) throws IOException { |
| 131 | + final String uri = getAppsUri(EXIST_WEB_SERVER) + "/" + TEST_COLLECTION_NAME.append("anything") + (sendRedirectType != null ? "?redirect-type=" + sendRedirectType.httpStatusCode : ""); |
| 132 | + final Request request = requestFn.apply(uri); |
| 133 | + final Tuple2<Integer, String> redirectStatusCodeAndLocation = withHttpExecutor(EXIST_WEB_SERVER, true, executor -> { |
| 134 | + final HttpResponse response = executor.execute(request).returnResponse(); |
| 135 | + return Tuple(response.getStatusLine().getStatusCode(), response.getFirstHeader("Location").getValue()); |
| 136 | + }); |
| 137 | + assertEquals(expectedRedirectTypeResponse.httpStatusCode, redirectStatusCodeAndLocation._1.intValue()); |
| 138 | + assertEquals("http://elsewhere.dom", redirectStatusCodeAndLocation._2); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + @BeforeClass |
| 143 | + public static void setup() throws IOException { |
| 144 | + final Request request = Request |
| 145 | + .Put(getRestUri(EXIST_WEB_SERVER) + TEST_COLLECTION + "/" + XQUERY_CONTROLLER_FILENAME) |
| 146 | + .bodyString(TEST_CONTROLLER, ContentType.create("application/xquery")); |
| 147 | + |
| 148 | + final int statusCode = withHttpExecutor(EXIST_WEB_SERVER, executor -> |
| 149 | + executor.execute(request).returnResponse().getStatusLine().getStatusCode() |
| 150 | + ); |
| 151 | + |
| 152 | + assertEquals(HttpStatus.SC_CREATED, statusCode); |
| 153 | + } |
| 154 | +} |
0 commit comments