Skip to content

Commit c24d3e2

Browse files
Feature/scala 472 (#522)
* Add delete recursively article * Update build.sbt * Add code * Update build.sbt * Fix * Fix version conflicts --------- Co-authored-by: Grzegorz Piwowarek <[email protected]>
1 parent cae069a commit c24d3e2

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

Diff for: build.sbt

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ lazy val scala_akka_2 = (project in file("scala-akka-2"))
191191
"com.typesafe.akka" %% "akka-stream" % "2.7.0",
192192
"com.typesafe.akka" %% "akka-http" % "10.4.0",
193193
"com.typesafe.akka" %% "akka-http-spray-json" % "10.4.0",
194+
"com.typesafe.akka" %% "akka-http-testkit" % "10.4.0",
194195
"com.lightbend.akka" %% "akka-stream-alpakka-sse" % "5.0.0",
195196
"com.typesafe.akka" %% "akka-persistence-typed" % "2.7.0",
196197
"com.typesafe.akka" %% "akka-actor-testkit-typed" % "2.7.0" % Test

Diff for: events.json

+1
Large diffs are not rendered by default.

Diff for: file.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is a file content
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.scala.akka_2.query_parameters
2+
3+
import akka.actor.typed.ActorSystem
4+
import akka.actor.typed.scaladsl.Behaviors
5+
import akka.http.scaladsl.Http
6+
import akka.http.scaladsl.model._
7+
import akka.http.scaladsl.server.Directives._
8+
9+
import scala.concurrent.ExecutionContextExecutor
10+
import scala.io.StdIn
11+
12+
object QueryParameters extends App {
13+
implicit val system: ActorSystem[Nothing] = ActorSystem(Behaviors.empty, "my-system")
14+
implicit val executionContext: ExecutionContextExecutor = system.executionContext
15+
16+
val route =
17+
pathPrefix("params") {
18+
concat(
19+
path("with-parameters") {
20+
get {
21+
parameters(Symbol("page").as[String], Symbol("size").as[String]) { (page, size) =>
22+
complete(HttpEntity(ContentTypes.`text/plain(UTF-8)`, s"parameters passed: page=$page and size=$size"))
23+
}
24+
}
25+
},
26+
path("with-extract") {
27+
get {
28+
extract(_.request.uri.query()) { params =>
29+
complete(HttpEntity(ContentTypes.`text/plain(UTF-8)`, s"parameters passed: page=${params.get("page").get} and size=${params.get("size").get}"))
30+
}
31+
}
32+
}
33+
)
34+
}
35+
36+
val bindingFuture = Http().newServerAt("localhost", 8080).bind(route)
37+
38+
println(s"Server now online. Please navigate to http://localhost:8080/params\nPress RETURN to stop...")
39+
40+
StdIn.readLine()
41+
bindingFuture
42+
.flatMap(_.unbind())
43+
.onComplete(_ => system.terminate())
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.scala.akka_2.query_parameters
2+
3+
import akka.http.scaladsl.model.{ContentTypes, HttpEntity}
4+
import akka.http.scaladsl.server.Directives._
5+
import akka.http.scaladsl.server.Route
6+
import akka.http.scaladsl.testkit.ScalatestRouteTest
7+
import org.scalatest.matchers.should.Matchers
8+
import org.scalatest.wordspec.AnyWordSpec
9+
10+
class QueryParametersTest
11+
extends AnyWordSpec
12+
with ScalatestRouteTest
13+
with Matchers {
14+
15+
val route: Route =
16+
pathPrefix("params") {
17+
concat(
18+
path("with-parameters") {
19+
get {
20+
parameters(Symbol("page").as[String], Symbol("size").as[String]) {
21+
(page, size) =>
22+
complete(
23+
HttpEntity(
24+
ContentTypes.`text/plain(UTF-8)`,
25+
s"parameters passed: page=$page and size=$size"
26+
)
27+
)
28+
}
29+
}
30+
},
31+
path("with-extract") {
32+
get {
33+
extract(_.request.uri.query()) { params =>
34+
complete(
35+
HttpEntity(
36+
ContentTypes.`text/plain(UTF-8)`,
37+
s"parameters passed: page=${params.get("page").get} and size=${params.get("size").get}"
38+
)
39+
)
40+
}
41+
}
42+
}
43+
)
44+
}
45+
46+
"The get with params" should {
47+
"return text with right query params values" in {
48+
Get("/params/with-parameters?page=0&size=23") ~> Route.seal(
49+
route
50+
) ~> check {
51+
responseAs[String] shouldEqual "parameters passed: page=0 and size=23"
52+
}
53+
}
54+
}
55+
56+
"The get with extract" should {
57+
"return text with right query params values" in {
58+
Get("/params/with-extract?page=0&size=23") ~> Route.seal(route) ~> check {
59+
responseAs[String] shouldEqual "parameters passed: page=0 and size=23"
60+
}
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)