Created
September 30, 2017 22:31
-
-
Save ayushmishra2005/6c4257333948773489736b84d7cf54e2 to your computer and use it in GitHub Desktop.
Akka HTTP: Example of URI 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.model._ | |
import akka.http.scaladsl.server.Directives._ | |
import akka.http.scaladsl.server.Route | |
import akka.http.scaladsl.server.directives.RouteDirectives.complete | |
import akka.http.scaladsl.testkit.ScalatestRouteTest | |
import org.scalatest.{Matchers, WordSpec} | |
trait AkkaHTTPUriModelExample { | |
var users: List[String] = Nil | |
lazy val userRoutes: Route = | |
path("users") { | |
parameter('redirect_uri) { redirect_uri => | |
val uri = Uri(redirect_uri) | |
try { | |
val queryParameters = uri.query(mode = Uri.ParsingMode.Strict).value | |
complete(s"Welcome $queryParameters") | |
} catch { | |
case ex: Exception => { | |
failWith(ex) | |
} | |
} | |
} | |
} | |
} | |
class AkkaHTTPUriModelExampleSpec extends WordSpec with Matchers with ScalatestRouteTest | |
with AkkaHTTPUriModelExample { | |
lazy val routes = userRoutes | |
"UserRoutes" should { | |
"return if redirect url is valid" in { | |
Get("/users?redirect_uri=http://example.com?name=John") ~> routes ~> check { | |
status should ===(StatusCodes.OK) | |
entityAs[String] should ===("""Welcome John""") | |
} | |
} | |
"not return if redirect url is invalid" in { | |
Get("/users?redirect_uri=http://example.com?^name=John") ~> routes ~> check { | |
status should ===(StatusCodes.InternalServerError) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment