question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BasicAuthFactory in dropwizard-testing.

See original GitHub issue

I’m trying to get the BasicAuthFactory in dropwizard testing to work. As stated in the documentation, I’m using GrizzlyTestContainerFactory as test container factory. BasicAuthFactory still isn’t working because @Context private HttpServletRequest request; isn’t injected. I also tried GrizzlyWebTestContainerFactory, this throws an exception:

java.lang.IllegalArgumentException: The deployment context must be an instance of ServletDeploymentContext.
    at org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory.create(GrizzlyWebTestContainerFactory.java:85)
    at org.glassfish.jersey.test.JerseyTest.createTestContainer(JerseyTest.java:277)
    at org.glassfish.jersey.test.JerseyTest.setUp(JerseyTest.java:609)
    at io.dropwizard.testing.junit.ResourceTestRule$1.evaluate(ResourceTestRule.java:157)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

How to test basic auth protected resources?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ferdy-lwcommented, Mar 27, 2015

After a couple of hours hacking around with this, there is a solution, albeit inelegant. As per Carlo’s solution (see his branch), if you configure ResourceTestRule to use GrizzleyWebTestContainerFactory and set the DeploymentContext to a ServletDeploymentContext you still run into the issue of the ResourceConfig being reflectively created.

I’d really like to be able to test my resources using the resource rule; however, all my resources require @Auth.

Instead of using DI you can simply hold onto a static reference. In the ResourceTestRule class add a static class of the DropwizardResourceConfig, and construct it with the rule when building the ServletDeploymentContext. When Grizzly constructs the ResourceConfig the static reference to the rule will already be there and can be used to register providers again.

public static class ResourceTestResourceConfig extends DropwizardResourceConfig {

    public static ResourceTestRule msResourceTestRule = null;  // Keep a reference to the RULE

    public ResourceTestResourceConfig(ResourceTestRule rule) {
        msResourceTestRule = rule;
    }

    public ResourceTestResourceConfig() {
        super(true, new MetricRegistry());

        ResourceTestRule resourceTestRule = msResourceTestRule;

        for (Class<?> provider : resourceTestRule.providers) {
            register(provider);
        }
        for (Map.Entry<String, Object> property : resourceTestRule.properties.entrySet()) {
            property(property.getKey(), property.getValue());
        }
        register(new JacksonMessageBodyProvider(resourceTestRule.mapper, resourceTestRule.validator));
        for (Object singleton : resourceTestRule.singletons) {
            register(singleton);
        }
    }
}


@Override
public Statement apply(final Statement base, Description description) {
    final ResourceTestRule rule = this;
    return new Statement() {
        @Override
        public void evaluate() throws Throwable {
            try {

                test = new JerseyTest() {
                    @Override
                    protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
                        return testContainerFactory;
                    }

                    @Override
                    protected DeploymentContext configureDeployment() {
                        return ServletDeploymentContext.builder(new ResourceTestResourceConfig(rule))
                                    .initParam(ServletProperties.JAXRS_APPLICATION_CLASS, ResourceTestResourceConfig.class.getName())
                                    .build();
                    }
                };
                test.setUp();
                base.evaluate();
            } finally {
                if (test != null) {
                    test.tearDown();
                }
            }
        }
    };
}
0reactions
carlo-rtrcommented, Mar 31, 2015

@ferdy-lw thanks for work around. I’ve posted the PR, let’s see what others think.

I’m not sure it’s the most elegant solution, but it works and this is limited to the test code base.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dropwizard Authentication
This authenticator takes basic auth credentials and if the client-provided password is secret , authenticates the client as a User with the client-provided ......
Read more >
Dropwizard - BasicAuth Security Example - HowToDoInJava
Authenticator class is responsible for verifying username/password credentials included in Basic Auth header. In enterprise application, you may ...
Read more >
Getting Started with Dropwizard: Authentication, Configuration ...
Basic Authentication is the simplest way to secure access to a resource. ... we'll see how to test password-protected sub-resource methods.
Read more >
Uses of Class io.dropwizard.auth.AuthFactory - javadoc.io
class, BasicAuthFactory<T>. A Jersey provider for Basic HTTP authentication.
Read more >
DropWizard Auth by Example - Stack Overflow
For Dropwizard 0.8.x, the configuration of Basic Auth has changed a bit. You can see more here. A simple example would be SimpleAuthenticator ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found