Created
September 23, 2017 22:47
-
-
Save ayushmishra2005/46068d96af10ef4911aba8c9fbb1bc63 to your computer and use it in GitHub Desktop.
Akka HTTP: Examples of HttpRequest and HttpResponse model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tutorial.sample | |
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport | |
import akka.http.scaladsl.model.HttpCharsets._ | |
import akka.http.scaladsl.model.HttpMethods.{POST, PUT} | |
import akka.http.scaladsl.model.HttpProtocols.`HTTP/1.0` | |
import akka.http.scaladsl.model.MediaTypes.`text/plain` | |
import akka.http.scaladsl.model.headers.BasicHttpCredentials | |
import akka.http.scaladsl.model.{StatusCodes, headers => mHeaders, _} | |
import akka.http.scaladsl.server.Directives._ | |
import akka.http.scaladsl.server.Route | |
import akka.http.scaladsl.server.directives.Credentials | |
import akka.http.scaladsl.server.directives.MethodDirectives.{get, post} | |
import akka.http.scaladsl.server.directives.RouteDirectives.complete | |
import akka.http.scaladsl.testkit.ScalatestRouteTest | |
import akka.util.ByteString | |
import org.scalatest.{Matchers, WordSpec} | |
import spray.json.DefaultJsonProtocol | |
trait HttpRequestResponseSampleRoutes extends SprayJsonSupport { | |
import DefaultJsonProtocol._ | |
var users: List[String] = Nil | |
lazy val userRoutes: Route = | |
pathPrefix("users") { | |
pathEnd { | |
concat( | |
get { | |
complete(users) | |
}, | |
post { | |
entity(as[String]) { user => | |
users ::= user | |
complete((StatusCodes.Created, user)) | |
} | |
}, | |
put { | |
authenticateBasic(realm = "secure site", myUserPassAuthenticator) { userName => | |
entity(as[String]) { user => | |
users ::= user | |
complete((StatusCodes.Created, user)) | |
} | |
} | |
} | |
) | |
} | |
} | |
def myUserPassAuthenticator(credentials: Credentials): Option[String] = | |
credentials match { | |
case p@Credentials.Provided(id) if p.verify("pass") => Some(id) | |
case _ => None | |
} | |
} | |
class HttpRequestResponseSampleRoutesSpec extends WordSpec with Matchers with ScalatestRouteTest | |
with HttpRequestResponseSampleRoutes { | |
lazy val routes = userRoutes | |
"UserRoutes" should { | |
"return no users if no present (GET /users)" in { | |
val request = HttpRequest(uri = "/users") | |
request ~> routes ~> check { | |
response should ===(HttpResponse(StatusCodes.OK,List(),HttpEntity(ContentType(MediaTypes.`application/json`), """[]"""),HttpProtocols.`HTTP/1.1`)) | |
status should ===(StatusCodes.OK) | |
entityAs[String] should ===("""[]""") | |
} | |
} | |
"be able to add users (POST /users)" in { | |
val request = HttpRequest(POST, uri = "/users", entity = ByteString("User1")) | |
request ~> routes ~> check { | |
status should ===(StatusCodes.Created) | |
entityAs[String] should ===("""User1""") | |
} | |
} | |
"be able to add users (PUT /users) with authentication" in { | |
val request = HttpRequest( | |
PUT, | |
uri = "/users", | |
entity = HttpEntity(`text/plain` withCharset `UTF-8`, ByteString("User2")), | |
headers = List(mHeaders.Allow(POST, PUT), mHeaders.Authorization(BasicHttpCredentials("user", "pass"))), | |
protocol = `HTTP/1.0`) | |
request ~> routes ~> check { | |
status should ===(StatusCodes.Created) | |
entityAs[String] should ===("""User2""") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful and relates to chapter 4 of your book, which is enjoyable and useful.
Note that your line 9 renaming of
import headers
tomHeaders
makes it a little difficult to track in the book without this gist. That's why I had to come here!