Relative URL when using Http Request?

29 views
Skip to first unread message

todd....@gmail.com

unread,
Apr 24, 2017, 11:47:01 PM4/24/17
to Akka User List
Hi,

I'm using akka-http to build a restful api. In one route of my app, the route need to access one of my other routes in the same app, like the following:

  • `/foo`, which access `/bar` and returns slightly modified content from the latter as response
  • `/bar`
In the implementation of `/foo`, I used absolute path: `127.0.0.1:8080/bar`, and request by `Http().singleRequest(...)`. This works fine initially, but as I start to write the test cases with akka test kit(ScalatestRouteTest), the port 8080 seems cannot be accessed in my test: 

```
Get("/foo") ~> route ~> check {...}
```

and it shows the error:

```
akka.stream.StreamTcpException: Tcp command [Connect(127.0.0.1:8080,None,List(),Some(10 seconds),true)] failed
```

Thus I'm looking for a way to make request to `/bar`, with relative URL, hoping it solve this problem. Any idea is appreciated.

---
BR,
Todd Leo

Konrad Malawski

unread,
Apr 24, 2017, 11:50:52 PM4/24/17
to Akka User List, todd....@gmail.com
Because the route test does not actually start a real server. That's a feature. It allows testing all routes without involving actual parsing and starting servers so it is much much faster than full integration testing.

If you want to bind a real server in your test simply do so same as in normal apps.

Instead of doing requests to yourself I would recommend sharing code or methods inside the implementation.

-- 
Konrad Malawski
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Todd Leo

unread,
Apr 25, 2017, 12:21:07 AM4/25/17
to Konrad Malawski, Akka User List

Just need to access routes that makes requests to the other routes, this doesn’t work, due to my absolute url:

val myRoute = {
  path("/foo") {
    val url = "http://127.0.0.1:8080/bar"
    val response = Http().singleRequest(HttpRequest(uri = url))
    onComplete(response) {
      case Success(r) => complete(r)
      case Failure(_) =>
    }
  } ~
  path ("/bar") {
    complete("Hello World")
  }
}

in my test case:

Get("/foo") ~> route ~> check {...}

Sorry in my prior email I forgot to render markdown. Hope this clarifies a bit.

Reply all
Reply to author
Forward
0 new messages