+doc #19429 initial merge of docs-dev and docs
This commit is contained in:
parent
be0c8af4c0
commit
5a18d43435
501 changed files with 9876 additions and 3681 deletions
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.http.scaladsl
|
||||
|
||||
import akka.stream.testkit.AkkaSpec
|
||||
|
||||
class MarshalSpec extends AkkaSpec {
|
||||
|
||||
"use marshal" in {
|
||||
import scala.concurrent.Await
|
||||
import scala.concurrent.duration._
|
||||
import akka.http.scaladsl.marshalling.Marshal
|
||||
import akka.http.scaladsl.model._
|
||||
|
||||
import system.dispatcher // ExecutionContext
|
||||
|
||||
val string = "Yeah"
|
||||
val entityFuture = Marshal(string).to[MessageEntity]
|
||||
val entity = Await.result(entityFuture, 1.second) // don't block in non-test code!
|
||||
entity.contentType shouldEqual ContentTypes.`text/plain(UTF-8)`
|
||||
|
||||
val errorMsg = "Easy, pal!"
|
||||
val responseFuture = Marshal(420 -> errorMsg).to[HttpResponse]
|
||||
val response = Await.result(responseFuture, 1.second) // don't block in non-test code!
|
||||
response.status shouldEqual StatusCodes.EnhanceYourCalm
|
||||
response.entity.contentType shouldEqual ContentTypes.`text/plain(UTF-8)`
|
||||
|
||||
val request = HttpRequest(headers = List(headers.Accept(MediaTypes.`application/json`)))
|
||||
val responseText = "Plaintext"
|
||||
val respFuture = Marshal(responseText).toResponseFor(request) // with content negotiation!
|
||||
a[Marshal.UnacceptableResponseContentTypeException] should be thrownBy {
|
||||
Await.result(respFuture, 1.second) // client requested JSON, we only have text/plain!
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue