logo
down
shadow

How to add query string parameter to FakeRequest in Play Framework 2.3?


How to add query string parameter to FakeRequest in Play Framework 2.3?

By : user2948095
Date : November 14 2020, 04:48 PM
I hope this helps . Query string is parsed from the URI, that you provide to your FakeRequest, so you can create FakeRequest like this:
code :
FakeRequest(GET, "/my-resources?sort=name,description&name=MyName&description=Blablabla")


Share : facebook icon twitter icon
Routing based on query parameter in Play framework

Routing based on query parameter in Play framework


By : you
Date : March 29 2020, 07:55 AM
I wish did fix the issue. The single dispatcher action is probably the way to go, and you don't need to specify all of your optional parameters in the route. If action is always there then that's the only one you really need.
code :
GET  /someRoute      controller.dispatcher(action: String)
Play Framework Testing using MultipartFormData in a FakeRequest

Play Framework Testing using MultipartFormData in a FakeRequest


By : Camila Sanhueza Nasa
Date : March 29 2020, 07:55 AM
help you fix your problem Rather than testing in a FakeApplication which is slow and (in my experience) can be error-prone when tests are running in parallel, I've been unit testing my Multipart form upload handlers like this:
code :
def handleUpload = Action(parse.multipartFormData) { implicit request =>
  doUpload(request)
}

def doUpload(request:Request[MultipartFormData[TemporaryFile]]) = {
  ...
}
val request= mock[Request[MultipartFormData[TemporaryFile]]]
val tempFile = TemporaryFile("do_upload","spec")
val fileName = "testFile.txt"
val part = FilePart("key: String", fileName, None, tempFile)
val files = Seq[FilePart[TemporaryFile]](part)
val multipartBody = MultipartFormData(Map[String, Seq[String]](), files, Seq[BadPart](), Seq[MissingFilePart]())
request.body returns multipartBody 
val result = controller.doUpload(request)
status(result) must beEqualTo(201)
Create fakeRequest for MultipartFormData java Play Framework 2

Create fakeRequest for MultipartFormData java Play Framework 2


By : Celeste Martin
Date : March 29 2020, 07:55 AM
wish of those help I have solved this issue after trying some approaches.
The solution:
How to send json string body for FakeRequest without using withJsonBody in Play framework v2.x?

How to send json string body for FakeRequest without using withJsonBody in Play framework v2.x?


By : Harish
Date : March 29 2020, 07:55 AM
To fix this issue By default, the Content-Type header gets overridden by the framework. A workaround is given in the blog
In your case, this should work
code :
route(FakeRequest(POST, "/bob", FakeHeaders(Seq(CONTENT_TYPE->Seq("application/json"))), jsonStr))(Writeable(_.getBytes, None)).get
implicit val wString: Writeable[String] = Writeable(_.getBytes, None)
Difference between query string and path parameter in play framework?

Difference between query string and path parameter in play framework?


By : N.Daphine
Date : March 29 2020, 07:55 AM
To fix this issue It's described in the documentation:
https://www.playframework.com/documentation/2.5.x/ScalaRouting https://www.playframework.com/documentation/2.5.x/ScalaRequestBinders
code :
# Client, like /clients/125
GET   /clients/:id          controllers.Clients.show(id: Long)
# Pagination links, like /clients?page=3
GET   /clients              controllers.Clients.list(page: Int ?= 1)
# Client, like /clients/125
GET   /clients/:id          controllers.Clients.show(client: Client)
implicit def pathBinder(implicit intBinder: PathBindable[Int]) = new PathBindable[Client] {
  override def bind(key: String, value: String): Either[String, Client] = {
    for {
      id <- intBinder.bind(key, value).right
      client <- Client.findById(id).toRight("Client not found").right
    } yield client
  }
  override def unbind(key: String, client: Client): String = {
    client.id.toString
  }
}
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org