BasicAuthFactory in dropwizard-testing.
See original GitHub issueI’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:
- Created 9 years ago
- Comments:13 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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
ResourceTestRuleto useGrizzleyWebTestContainerFactoryand set theDeploymentContextto aServletDeploymentContextyou still run into the issue of theResourceConfigbeing 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
ResourceTestRuleclass add a static class of theDropwizardResourceConfig, and construct it with the rule when building theServletDeploymentContext. When Grizzly constructs the ResourceConfig the static reference to the rule will already be there and can be used to register providers again.@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.