I have a service which requires authentication through headers. I have an existing java client which generates the values for me. I am trying to apply the headers to a ws request using a WSRequestHeaderFilter.
The code seems to run fine when I breakpoint and I can see the headers are applied. But in my Test Server (using the play SIRD router) the headers to not seem to be applied?
How can I make my required headers appear in the request using a filter or such method?
See the code below:
Filter:
class AuthenticatingFilter @Inject() (authHeaderGenerator: AuthHeaderGenerator) extends WSRequestFilter {
def apply(executor: WSRequestExecutor): WSRequestExecutor = {
new WSRequestExecutor {
override def execute(request: WSRequest): Future[WSResponse] = {
val headers = authHeaderGenerator.generateRequestHeaders(request.method, request.uri.toString, null).asScala.toList
executor.execute(request.withHeaders(headers:_*))
}
}
}
}
Usage in client:
//code omitted for brevity
def getStuff() = ws.url(s"${baseUrl}/authenticatedEndpoint").withRequestFilter(filter).get()
Test:
// code omitted for brevity
Server.withRouter() {
case GET(p"/authenticatedEndpoint") => Action {
request =>
request.headers.get(authHeader) match {
case Some(authHeaderValue) => Results.Ok(expectedResult)
case _ => Results.Forbidden
}
}
} {
implicit port =>
implicit val materializer = Play.current.materializer
WsTestClient.withClient {
client =>
val authenticatedClient: AuthenticatedClient = new AuthenticatedClient(client,filter)
val result: String = Await.result(authenticatedClient.getStuff(), Duration.Inf)
result must beEqualTo(expectedResult)
}
}
}
Thanks,
Ben
As it turned out this was a bug in play. I patched this and the change was merged into the master branch (https://github.com/playframework/playframework/pull/6077) so if you are having similar problems this should be fixed by upgrading to play 2.5.3 (when available)