spray

How to cut a long ScalaTest spec to pieces

我们两清 提交于 2019-12-02 02:57:51
I'm testing a REST API, and the code goes like this: Setting up stuff, populating a database using PUSH calls Testing API a Testing API b ... The code is currently in one rather huge FlatSpec : class RestAPITest extends FlatSpec with Matchers with ScalatestRouteTest with SprayJsonSupport I would like to chop the "Testing API a/b/..." parts out, to have the code more manageable. Trying to do that seems like a no-no: what's the type of it - how to pass that on, etc. etc. So, what's the recommended way to go about such stuff. The a/b/... tests could be run in parallel, once the basic setup has

Spray's `detach` Directive

◇◆丶佛笑我妖孽 提交于 2019-12-01 12:52:28
Given the following Spray code: object Main extends App with SimpleRoutingApp { implicit val system = ActorSystem("my-system") val pipeline: HttpRequest => Future[String] = sendReceive ~> unmarshal[String] startServer(interface = "localhost", port = 8080) { path("go") { get { detach() { complete { val req = Post("http://www.google.com") ~> addHeader("Foo", "bar") pipeline(req).recoverWith[String]{ case _ => Future { "error!" } } } } } } } } I put the complete function within the detach directive. The docs explain that detach will: execute the inner route inside a future. What's the

Akka consolidate concurrent database requests

纵然是瞬间 提交于 2019-12-01 11:12:04
I want to be able to make concurrent requests to multiple data repositories and consolidate the results. I am trying to understand if my approach is at all valid or if there is a better way to approach this problem. I am definitely new to Akka / Spray / Scala and really want to get a better understanding of how to properly build these components. Any suggestions / Tips would be greatly appreciated. Trying to wrap my head around the use of actors and futures for this type of implementation. Spray Service: trait DemoService extends HttpService with Actor with ActorLogging { implicit val timeout

Spray's `detach` Directive

大憨熊 提交于 2019-12-01 10:25:09
问题 Given the following Spray code: object Main extends App with SimpleRoutingApp { implicit val system = ActorSystem("my-system") val pipeline: HttpRequest => Future[String] = sendReceive ~> unmarshal[String] startServer(interface = "localhost", port = 8080) { path("go") { get { detach() { complete { val req = Post("http://www.google.com") ~> addHeader("Foo", "bar") pipeline(req).recoverWith[String]{ case _ => Future { "error!" } } } } } } } } I put the complete function within the detach

How to unlimit spray jsonFormat

北城以北 提交于 2019-12-01 06:23:09
I'm implementing some rest API that use spray and akka The API should expose some kind of user CRUD. I'll use only create user in this question... case class User(id:String, name:String) case class Register(user:User, registrationId:String) trait DefaultJsonFormats extends DefaultJsonProtocol with SprayJsonSupport with MetaMarshallers {} class RegistrationService(registration: ActorRef) (implicit executionContext: ExecutionContext) extends Directives with DefaultJsonFormats { implicit val timeout = Timeout(2.seconds) implicit val userFormat = jsonFormat3(User) implicit val registerFormat =

How to unlimit spray jsonFormat

元气小坏坏 提交于 2019-12-01 05:40:17
问题 I'm implementing some rest API that use spray and akka The API should expose some kind of user CRUD. I'll use only create user in this question... case class User(id:String, name:String) case class Register(user:User, registrationId:String) trait DefaultJsonFormats extends DefaultJsonProtocol with SprayJsonSupport with MetaMarshallers {} class RegistrationService(registration: ActorRef) (implicit executionContext: ExecutionContext) extends Directives with DefaultJsonFormats { implicit val

How do i specify spray Content-Type response header?

倖福魔咒の 提交于 2019-12-01 03:56:22
I understand that spray does that for me, but I still want to override it with my header, how can I override the header in the response? My response looks like this: case HttpRequest(GET, Uri.Path("/something"), _, _, _) => sender ! HttpResponse(entity = """{ "key": "value" }""" // here i want to specify also response header i would like to explicitly set it and not get it implicitly 4lex1v If you still want to use spray can, then you have two options, based on that HttpResponse is a case class. The first is to pass a List with an explicit content type: import spray.http.HttpHeaders._ import

Spray.io: Can't compile test spec

孤街浪徒 提交于 2019-11-30 09:12:55
I have the following service: trait PingService extends MyHttpService { val pingRoutes = path("ping") { get { complete("message" -> "pong") } } } MyHttpService is a custom class that extends HttpService and only contains utility methods. This is the test spec: import akka.actor.ActorRefFactory import org.json4s.{DefaultFormats, Formats} import org.scalatest.{FreeSpec, Matchers} import spray.testkit.ScalatestRouteTest class PingServiceSpec extends FreeSpec with PingService with ScalatestRouteTest with Matchers { override implicit def actorRefFactory: ActorRefFactory = system override implicit

Is it possible to install a callback after request processing is finished in Spray?

不羁的心 提交于 2019-11-30 07:19:19
问题 I'm trying to serve large temporary files from Spray. I need to delete those files once HTTP request is complete. I could not find a way to do this so far... I'm using code similar to this or this: respondWithMediaType(`text/csv`) { path("somepath" / CsvObjectIdSegment) { id => CsvExporter.export(id) { // loan pattern to provide temp file for this request file => encodeResponse(Gzip) { getFromFile(file) } } } } So essentially it calls getFromFile which completes the route in a Future . The

Spray.io: Can't compile test spec

巧了我就是萌 提交于 2019-11-29 12:49:00
问题 I have the following service: trait PingService extends MyHttpService { val pingRoutes = path("ping") { get { complete("message" -> "pong") } } } MyHttpService is a custom class that extends HttpService and only contains utility methods. This is the test spec: import akka.actor.ActorRefFactory import org.json4s.{DefaultFormats, Formats} import org.scalatest.{FreeSpec, Matchers} import spray.testkit.ScalatestRouteTest class PingServiceSpec extends FreeSpec with PingService with