Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SLING-7508: implemented integration test for endless loop
- Loading branch information
Dirk Rudolph
committed
Feb 21, 2018
1 parent
24d1588
commit db174f7
Showing
5 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/org/apache/sling/models/it/rtpickerrequest/Query.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.apache.sling.models.it.rtpickerrequest; | ||
|
||
public interface Query { | ||
|
||
String getParameterFoo(); | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/apache/sling/models/it/rtpickerrequest/QueryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.apache.sling.models.it.rtpickerrequest; | ||
|
||
import org.apache.sling.api.SlingHttpServletRequest; | ||
import org.apache.sling.api.request.RequestParameter; | ||
import org.apache.sling.models.annotations.Model; | ||
import org.apache.sling.models.annotations.injectorspecific.Self; | ||
|
||
@Model(adaptables = SlingHttpServletRequest.class, adapters = { QueryImpl.class, Query.class }) | ||
public class QueryImpl implements Query { | ||
|
||
@Self | ||
private SlingHttpServletRequest req; | ||
|
||
@Override | ||
public String getParameterFoo() { | ||
RequestParameter param = req.getRequestParameterMap().getValue("foo"); | ||
if (param != null) { | ||
return param.getString(); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/org/apache/sling/models/it/rtpickerrequest/TestServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.apache.sling.models.it.rtpickerrequest; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.apache.felix.scr.annotations.sling.SlingServlet; | ||
import org.apache.sling.api.SlingHttpServletRequest; | ||
import org.apache.sling.api.SlingHttpServletResponse; | ||
import org.apache.sling.api.servlets.SlingSafeMethodsServlet; | ||
|
||
@SlingServlet(paths = "/apps/rtpickerrequest") | ||
public class TestServlet extends SlingSafeMethodsServlet { | ||
|
||
@Override | ||
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) | ||
throws ServletException, IOException { | ||
Query q = request.adaptTo(Query.class); | ||
|
||
if (q == null || q.getParameterFoo() == null) { | ||
response.sendError(HttpServletResponse.SC_BAD_REQUEST); | ||
} else { | ||
response.sendError(HttpServletResponse.SC_OK); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/test/java/org/apache/sling/models/testing/AdaptFromServletResourceRequestIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.apache.sling.models.testing; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.osgi.services.HttpClientBuilderFactory; | ||
import org.apache.sling.junit.Activator; | ||
import org.apache.sling.junit.rules.TeleporterRule; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.framework.ServiceReference; | ||
|
||
public class AdaptFromServletResourceRequestIT { | ||
|
||
@Rule | ||
public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "SM_Teleporter"); | ||
|
||
private String httpEndpoint; | ||
private HttpClientBuilderFactory httpBuilderFactory; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
BundleContext bundleContext = Activator.getBundleContext(); | ||
ServiceReference ref = bundleContext.getServiceReference("org.osgi.service.http.HttpService"); | ||
httpEndpoint = ((String[]) ref.getProperty("osgi.http.endpoint"))[0]; | ||
httpBuilderFactory = teleporter.getService(HttpClientBuilderFactory.class); | ||
} | ||
|
||
@Test | ||
public void testRequestWithParameter() throws IOException { | ||
requestAndAssertStatus(httpEndpoint + "apps/rtpickerrequest?foo=bar", 200); | ||
} | ||
|
||
@Test | ||
public void testRequestWithout() throws IOException { | ||
requestAndAssertStatus(httpEndpoint + "apps/rtpickerrequest", 400); | ||
} | ||
|
||
private void requestAndAssertStatus(String url, int status) throws IOException { | ||
HttpGet get = new HttpGet(url); | ||
CloseableHttpClient client = httpBuilderFactory.newBuilder().build(); | ||
CloseableHttpResponse response = client.execute(get); | ||
assertEquals(status, response.getStatusLine().getStatusCode()); | ||
response.close(); | ||
client.close(); | ||
} | ||
} |