简体   繁体   中英

akka http remote address

I'm using Akka HTTP 10.0.9, but struggling to get my unit tests to have a working Remote Address.

eg unit test:

Get("/").withHeaders(
    RawHeader("Remote-Address", "192.168.1.1"), RawHeader("X-Forwarded-For", "192.168.1.1")
) ~> route ~> check {
    status must_== StatusCodes.OK
}

And in the web server code:

extractClientIP{ clientAddr =>
  complete(s"$clientAddr")
}

When running the app via the command line, the client address is returned correctly. But when run via unit tests, the client address always comes back as "Unknown"

What am I doing wrong?

This is caused by akka http can't handle the RawHeader in test . You can solve it by use the Remote-Address object for set the IP for test:

import akka.http.scaladsl.model.headers.`Remote-Address`
Get().withHeaders(
  `Remote-Address`(RemoteAddress(new InetSocketAddress("192.168.1.1", 23)))
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM