Skip to content

Commit

Permalink
moved the url normalizing logic to JerseyEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcampanini committed Feb 1, 2016
1 parent c436e5e commit eea5774
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 36 deletions.
Expand Up @@ -489,14 +489,7 @@ protected Handler createAppServlet(Server server,
serverPush.addFilter(handler);
if (jerseyContainer != null) {
if (jerseyRootPath.isPresent()) {
String urlPattern = jerseyRootPath.get();
if (!urlPattern.endsWith("*") && !urlPattern.endsWith("/")) {
urlPattern += "/";
}
if (!urlPattern.endsWith("*")) {
urlPattern += "*";
}
jersey.setUrlPattern(urlPattern);
jersey.setUrlPattern(jerseyRootPath.get());
}
jersey.register(new JacksonMessageBodyProvider(objectMapper));
jersey.register(new HibernateValidationFeature(validator));
Expand Down
Expand Up @@ -35,49 +35,33 @@ public class AbstractServerFactoryTest {

@Before
public void before() {
when(this.environment.jersey()).thenReturn(this.jerseyEnvironment);
when(environment.jersey()).thenReturn(jerseyEnvironment);
}

@Test
public void usesYamlDefinedPattern() {
this.serverFactory.setJerseyRootPath(YAML_SET_PATTERN);
this.jerseyEnvironment.setUrlPattern(RUN_SET_PATTERN);
serverFactory.setJerseyRootPath(YAML_SET_PATTERN);
jerseyEnvironment.setUrlPattern(RUN_SET_PATTERN);

this.serverFactory.build(this.environment);
serverFactory.build(environment);

assertThat(this.jerseyEnvironment.getUrlPattern()).isEqualTo(YAML_SET_PATTERN);
assertThat(jerseyEnvironment.getUrlPattern()).isEqualTo(YAML_SET_PATTERN);
}

@Test
public void usesRunDefinedPatternWhenNoYaml() {
this.jerseyEnvironment.setUrlPattern(RUN_SET_PATTERN);
jerseyEnvironment.setUrlPattern(RUN_SET_PATTERN);

this.serverFactory.build(this.environment);
serverFactory.build(environment);

assertThat(this.jerseyEnvironment.getUrlPattern()).isEqualTo(RUN_SET_PATTERN);
assertThat(jerseyEnvironment.getUrlPattern()).isEqualTo(RUN_SET_PATTERN);
}

@Test
public void usesDefaultPatternWhenNoneSet() {
this.serverFactory.build(this.environment);
serverFactory.build(environment);

assertThat(this.jerseyEnvironment.getUrlPattern()).isEqualTo(DEFAULT_PATTERN);
}

@Test
public void yamlPatternEndsWithSlashStar() {
assertPatternEndsWithSlashStar("/missing/slash/star");
}

@Test
public void yamlPatternEndsWithStar() {
assertPatternEndsWithSlashStar("/missing/star/");
}

private void assertPatternEndsWithSlashStar(String jerseyRootPath) {
this.serverFactory.setJerseyRootPath(jerseyRootPath);
this.serverFactory.build(this.environment);
assertThat(this.jerseyEnvironment.getUrlPattern()).endsWith("/*");
assertThat(jerseyEnvironment.getUrlPattern()).isEqualTo(DEFAULT_PATTERN);
}

/**
Expand All @@ -90,7 +74,7 @@ public Server build(Environment environment) {
// mimics the current default + simple server factory build() methods
ThreadPool threadPool = createThreadPool(environment.metrics());
Server server = buildServer(environment.lifecycle(), threadPool);
this.createAppServlet(server,
createAppServlet(server,
environment.jersey(),
environment.getObjectMapper(),
environment.getValidator(),
Expand Down
Expand Up @@ -103,7 +103,14 @@ public String getUrlPattern() {
}

public void setUrlPattern(String urlPattern) {
config.setUrlPattern(urlPattern);
String normalizedUrlPattern = urlPattern;
if (!normalizedUrlPattern.endsWith("*") && !normalizedUrlPattern.endsWith("/")) {
normalizedUrlPattern += "/";
}
if (!normalizedUrlPattern.endsWith("*")) {
normalizedUrlPattern+= "*";
}
config.setUrlPattern(normalizedUrlPattern);
}

public DropwizardResourceConfig getResourceConfig() {
Expand Down
@@ -0,0 +1,36 @@
package io.dropwizard.jersey.setup;

import io.dropwizard.jersey.DropwizardResourceConfig;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public class JerseyEnvironmentTest {

private final JerseyContainerHolder holder = mock(JerseyContainerHolder.class);
private final DropwizardResourceConfig config = new DropwizardResourceConfig();
private final JerseyEnvironment jerseyEnvironment = new JerseyEnvironment(holder, config);

@Test
public void urlPatternEndsWithSlashStar() {
assertPatternEndsWithSlashStar("/missing/slash/star");
}

@Test
public void urlPatternEndsWithStar() {
assertPatternEndsWithSlashStar("/missing/star/");
}

@Test
public void urlPatternSuffixNoop() {
String slashStarPath = "/slash/star/*";
jerseyEnvironment.setUrlPattern(slashStarPath);
assertThat(jerseyEnvironment.getUrlPattern()).isEqualTo(slashStarPath);
}

private void assertPatternEndsWithSlashStar(String jerseyRootPath) {
jerseyEnvironment.setUrlPattern(jerseyRootPath);
assertThat(jerseyEnvironment.getUrlPattern()).endsWith("/*");
}
}

0 comments on commit eea5774

Please sign in to comment.