本文整理汇总了Scala中play.api.test.FakeRequest类的典型用法代码示例。如果您正苦于以下问题:Scala FakeRequest类的具体用法?Scala FakeRequest怎么用?Scala FakeRequest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FakeRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: SignOutControllerISpec
//设置package包名称以及导入依赖的类
package uk.gov.hmrc.agentmappingfrontend.controllers
import play.api.test.FakeRequest
import play.api.test.Helpers.redirectLocation
import play.api.test.Helpers._
class SignOutControllerISpec extends BaseControllerISpec{
private lazy val controller: SignedOutController = app.injector.instanceOf[SignedOutController]
private val fakeRequest = FakeRequest()
"context signed out" should {
"redirect to gov.uk page" in {
val result = await(controller.signOut(fakeRequest))
status(result) shouldBe 303
redirectLocation(result).head should include("www.gov.uk")
}
}
}
示例2: CountriesDestroySpec
//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._
import play.api.libs.json._
import test.helpers.{DatabaseCleaner, DatabaseInserter}
import play.api.Play
import utils.TimeUtil.now
class CountriesDestroySpec extends PlaySpec with BeforeAndAfterAll {
val app = new GuiceApplicationBuilder().build
override def beforeAll() {
Play.maybeApplication match {
case Some(app) => ()
case _ => Play.start(app)
}
DatabaseCleaner.clean(List("Countries"))
DatabaseInserter.insert("Users", 12, List("[email protected]", "John", "Doe", "password", "token", now.toString, "User", "1", "999999", "2016-01-01"))
DatabaseInserter.insert("Users", 13, List("[email protected]", "Johnny", "Doe", "password", "admin_token", now.toString, "Admin", "1", "999999", "2016-01-01"))
DatabaseInserter.insert("Countries", 11, List("Denmark", "DEN", "1", "2016-10-10"))
}
override def afterAll() {
DatabaseCleaner.clean(List("Countries"))
}
"deleting countries" should {
"returns 404 if no such record" in {
val request = FakeRequest(DELETE, "/countries/200").withHeaders("Authorization" -> "admin_token")
val destroy = route(app, request).get
status(destroy) mustBe NOT_FOUND
}
"returns 204 if successfully deleted and user is Admin" in {
val request = FakeRequest(DELETE, "/countries/11").withHeaders("Authorization" -> "admin_token")
val destroy = route(app, request).get
status(destroy) mustBe NO_CONTENT
}
"returns 403 if user is not Admin" in {
val request = FakeRequest(DELETE, "/countries/11").withHeaders("Authorization" -> "token")
val destroy = route(app, request).get
status(destroy) mustBe FORBIDDEN
}
}
}
示例3: HomePageControllerSpec
//设置package包名称以及导入依赖的类
package controllers
import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
import play.api.test.FakeRequest
import play.api.test.Helpers._
class HomePageControllerSpec extends PlaySpec with OneAppPerSuite {
//TODO: Need to define TimeGreetingService
object TestHomeControllerTest extends HomeController {
override def greeter = ???
}
val controller = TestHomeControllerTest
"HomeController" should {
"not return 404" when {
"I go to the route /landing" in {
val result = route(app, FakeRequest(GET, "/landing"))
status(result.get) must not be NOT_FOUND
}
}
"render the correct page with the expected text" when {
"I navigate to the homepage" in {
val result = controller.landing("Good morning Helen").apply(FakeRequest(GET, "/landing"))
status(result) mustBe OK
contentAsString(result) must include ("Good morning Helen")
//arrange
//action
//assert
}
"I go to the homepage after lunch" in {
val result = controller.landing("Good afternoon Helen").apply(FakeRequest(GET, "/landing"))
contentAsString(result) must include ("Good afternoon Helen")
}
}
}
}
示例4: BreakfastAppControllerSpec
//设置package包名称以及导入依赖的类
package controllers
import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
import play.api.mvc.Result
import play.api.test.FakeRequest
import play.api.test.Helpers._
import scala.concurrent.Future
class BreakfastAppControllerSpec extends PlaySpec with OneAppPerSuite {
"BreakfastAppController" should {
"not return 404" when {
"we try to hit the route /home" in {
route(app, FakeRequest(GET, "/home")).map(status(_)) must not be Some(NOT_FOUND)
}
}
"reneder a page" when {
"we try to hit the route /home" in {
var result: Option[Future[Result]] = route(app, FakeRequest(GET, "/home"))
result.map(status(_)) mustBe Some(OK)
val text: String = result.map(contentAsString(_)).get
text must include ("Welcome to Play")
}
}
}
}
示例5: RegistrationSpec
//设置package包名称以及导入依赖的类
import controllers.AbstractRegistration
import models.rest.{UserId, UserRest}
import org.scalatestplus.play._
import play.api.libs.json.Json
import play.api.mvc._
import play.api.test.FakeRequest
import play.api.test.Helpers._
import services.RegistrationService
import scala.concurrent.Future
class RegistrationSpec extends PlaySpec with Results {
"Registration#registerUser" should {
"return Created with a body containing a uuid when a user is registered" in {
val controller = new TestableRegistration()
val request = FakeRequest("POST", "/user").withJsonBody(Json.parse(
s"""{
| "firstName": "John",
| "lastName": "Smith"
|}""".stripMargin))
val apiResult = call(controller.registerUser, request)
status(apiResult) mustEqual CREATED
val resultBody = contentAsJson(apiResult)
resultBody mustEqual Json.parse(
s"""{
| "uuid": "TEST UUID"
|}""".stripMargin)
}
}
}
// A quick version to show testing, in reality this would probably be done in a more flexible way, test more cases
// and assert that the correct values were sent
class TestableRegistration extends RegistrationService with AbstractRegistration {
def insertUser(userData: UserRest): Future[UserId] = Future.successful(UserId("TEST UUID"))
def findUser(uuid: String): Future[Option[UserRest]] = Future.successful(Some(UserRest("John", "Smith")))
}
示例6: HomeControllerSpec
//设置package包名称以及导入依赖的类
package controllers
import models.UsersRepository
import org.specs2.execute.{AsResult, Result}
import org.specs2.mutable.Around
import org.specs2.specification.Scope
import play.api.Application
import play.api.mvc._
import play.api.test.{FakeRequest, PlaySpecification}
class HomeControllerSpec extends PlaySpecification with Results {
abstract class WithTestApplication extends Around with Scope with TestEnvironment {
lazy val app: Application = fakeApp
lazy val controller = new HomeController(knolxControllerComponent)
override def around[T: AsResult](t: => T): Result = {
TestHelpers.running(app)(AsResult.effectively(t))
}
}
"HomeController" should {
"redirect index page to sessions page" in new WithTestApplication {
val result = controller.index(FakeRequest())
status(result) must be equalTo SEE_OTHER
}
}
}
示例7: LoginAPITestextends
//设置package包名称以及导入依赖的类
package integration.api
import com.github.simplyscala.MongodProps
import org.specs2.mutable.Before
import scala.language.existentials
import play.api.libs.json.Json
import play.api.test.{FakeRequest, PlaySpecification, WithApplication}
class LoginAPITestextends extends PlaySpecification {
"respond to the no login data Action" in new WithApplication {
val Some(result) = route(app, FakeRequest(POST, "/login"))
status(result) must equalTo(400)
}
"response to the with loign data but no authentication" in new WithApplication {
val Some(result) = route(app, FakeRequest(POST, "/login").withJsonBody(Json.parse("""{"username":"no_data", "password":"abc"}""") ))
status(result) must equalTo(401)
contentType(result) must beSome("application/json")
}
"response to the with login data and authentication" in new WithApplication {
val Some(result) = route(app, FakeRequest(POST, "/login").withJsonBody(Json.parse("""{"username":"mubeen", "password":"mubeen"}""")))
status(result) must equalTo(200)
contentType(result) must beSome("application/json")
}
}
示例8: SessionCreateSpec
//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._
import play.api.libs.json._
import test.helpers.{DatabaseCleaner, DatabaseInserter}
import play.api.Play
import com.github.t3hnar.bcrypt._
class SessionCreateSpec extends PlaySpec with BeforeAndAfterAll {
val app = new GuiceApplicationBuilder().build
override def beforeAll() {
Play.maybeApplication match {
case Some(app) => ()
case _ => Play.start(app)
}
DatabaseCleaner.clean(List("Users"))
val password = "password".bcrypt
DatabaseInserter.insert("Users", 12, List("[email protected]", "John", "Doe", password, "token", "2016-01-01", "User", "1", "999999", "2016-01-01"))
}
override def afterAll() {
DatabaseCleaner.clean(List("Users"))
}
"creates session if data is ok" in {
val request = FakeRequest(POST, "/session").withJsonBody(Json.parse("""{ "email":"[email protected]", "password":"password" }"""))
val create = route(app, request).get
status(create) mustBe CREATED
contentType(create) mustBe Some("application/json")
contentAsString(create) must include("user")
contentAsString(create) must include("authToken")
}
"responses with error if data is not ok" in {
val request = FakeRequest(POST, "/session").withJsonBody(Json.parse("""{ "email":"[email protected]", "password":"bad_password" }"""))
val create = route(app, request).get
status(create) mustBe BAD_REQUEST
contentType(create) mustBe Some("application/json")
contentAsString(create) must include("error")
}
}
示例9: CountriesShowSpec
//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._
import play.api.libs.json._
import test.helpers.{DatabaseCleaner, DatabaseInserter}
import play.api.Play
import utils.TimeUtil.now
class CountriesShowSpec extends PlaySpec with BeforeAndAfterAll {
val app = new GuiceApplicationBuilder().build
override def beforeAll() {
Play.maybeApplication match {
case Some(app) => ()
case _ => Play.start(app)
}
DatabaseCleaner.clean(List("Countries"))
DatabaseInserter.insert("Users", 12, List("[email protected]", "John", "Doe", "password", "token", now.toString, "User", "1", "999999", "2016-01-01"))
DatabaseInserter.insert("Countries", 88, List("Austria", "AU", "0", "2015-01-01"))
}
override def afterAll() {
DatabaseCleaner.clean(List("Countries"))
}
"renders error" in {
val request = FakeRequest(GET, "/countries/20").withHeaders("Authorization" -> "token")
val show = route(app, request).get
status(show) mustBe NOT_FOUND
contentType(show) mustBe Some("application/json")
contentAsString(show) must include("error")
contentAsString(show) must include("not found")
}
"renders country" in {
val request = FakeRequest(GET, "/countries/88").withHeaders("Authorization" -> "token")
val show = route(app, request).get
status(show) mustBe OK
contentType(show) mustBe Some("application/json")
contentAsString(show) must include("country")
}
}
示例10: CountriesIndexSpec
//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._
import play.api.libs.json._
import test.helpers.{DatabaseCleaner, DatabaseInserter}
import play.api.Play
import utils.TimeUtil.now
class CountriesIndexSpec extends PlaySpec with BeforeAndAfterAll {
val app = new GuiceApplicationBuilder().build
override def beforeAll() {
Play.maybeApplication match {
case Some(app) => ()
case _ => Play.start(app)
}
DatabaseCleaner.clean(List("Countries"))
DatabaseInserter.insert("Users", 12, List("[email protected]", "John", "Doe", "password", "token", now.toString, "User", "1", "999999", "2016-01-01"))
}
override def afterAll() {
DatabaseCleaner.clean(List("Countries"))
}
"renders the forbidden for unauthorized response" in {
val request = FakeRequest(GET, "/countries")
val index = route(app, request).get
status(index) mustBe FORBIDDEN
contentType(index) mustBe Some("application/json")
contentAsString(index) must include("errors")
}
"renders countries list for authorized user response" in {
val request = FakeRequest(GET, "/countries").withHeaders("Authorization" -> "token")
val index = route(app, request).get
status(index) mustBe OK
contentType(index) mustBe Some("application/json")
contentAsString(index) must include("countries")
}
}
示例11: CountriesCreateSpec
//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._
import play.api.libs.json._
import test.helpers.{DatabaseCleaner, DatabaseInserter}
import play.api.Play
import utils.TimeUtil.now
class CountriesCreateSpec extends PlaySpec with BeforeAndAfterAll {
val app = new GuiceApplicationBuilder().build
override def beforeAll() {
Play.maybeApplication match {
case Some(app) => ()
case _ => Play.start(app)
}
DatabaseInserter.insert("Users", 12, List("[email protected]", "John", "Doe", "password", "token", now.toString, "Moderator", "1", "999999", "2016-01-01"))
DatabaseCleaner.clean(List("Countries"))
}
override def afterAll() {
DatabaseCleaner.clean(List("Countries"))
}
"saves the record on create" in {
val request = FakeRequest(POST, "/countries").withJsonBody(Json.parse("""{ "country": {"title":"Germany", "abbreviation":"GER"} }""")).withHeaders("Authorization" -> "token")
val create = route(app, request).get
status(create) mustBe CREATED
contentType(create) mustBe Some("application/json")
contentAsString(create) must include("country")
}
}
示例12: UsersIndexSpec
//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._
import play.api.libs.json._
import test.helpers.{DatabaseCleaner, DatabaseInserter}
import play.api.Play
import utils.TimeUtil.now
class UsersIndexSpec extends PlaySpec with BeforeAndAfterAll {
val app = new GuiceApplicationBuilder().build
override def beforeAll() {
Play.maybeApplication match {
case Some(app) => ()
case _ => Play.start(app)
}
DatabaseCleaner.clean(List("Users"))
DatabaseInserter.insert("Users", 12, List("[email protected]", "John", "Doe", "password", "token", now.toString, "User", "1", "999999", "2016-01-01"))
}
override def afterAll() {
DatabaseCleaner.clean(List("Users"))
}
"renders the index response" in {
val index = route(app, FakeRequest(GET, "/users").withHeaders("Authorization" -> "token")).get
status(index) mustBe OK
contentType(index) mustBe Some("application/json")
contentAsString(index) must include("users")
contentAsString(index) mustNot include("authToken")
}
}
示例13: UsersShowSpec
//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._
import play.api.libs.json._
import test.helpers.{DatabaseCleaner, DatabaseInserter}
import play.api.Play
import utils.TimeUtil.now
class UsersShowSpec extends PlaySpec with BeforeAndAfterAll {
val app = new GuiceApplicationBuilder().build
override def beforeAll() {
Play.maybeApplication match {
case Some(app) => ()
case _ => Play.start(app)
}
DatabaseCleaner.clean(List("Users"))
DatabaseInserter.insert("Users", 10, List("[email protected]", "John", "Doe", "password", "token", now.toString, "User", "1", "999999", "2016-01-01"))
}
override def afterAll() {
DatabaseCleaner.clean(List("Users"))
}
"show response" should {
"responses 404 if there is no such user" in {
val show = route(app, FakeRequest(GET, "/users/1").withHeaders("Authorization" -> "token")).get
status(show) mustBe NOT_FOUND
contentType(show) mustBe Some("application/json")
contentAsString(show) mustNot include("user")
}
"responses 200 if record exists" in {
val show = route(app, FakeRequest(GET, "/users/10").withHeaders("Authorization" -> "token")).get
status(show) mustBe OK
contentAsString(show) must include("user")
contentAsString(show) mustNot include("authToken")
}
}
}
示例14: UsersCreateSpec
//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._
import play.api.libs.json._
import test.helpers.DatabaseCleaner
import play.api.Play
class UsersCreateSpec extends PlaySpec with BeforeAndAfterAll {
val app = new GuiceApplicationBuilder().build
override def beforeAll() {
Play.maybeApplication match {
case Some(app) => ()
case _ => Play.start(app)
}
DatabaseCleaner.clean(List("Users"))
}
override def afterAll() {
DatabaseCleaner.clean(List("Users"))
}
"creating" should {
"returns 400 if bad request" in {
val request = FakeRequest(POST, "/users").withJsonBody(Json.parse("""{ "user": {"password":"pass"} }"""))
val create = route(app, request).get
status(create) mustBe BAD_REQUEST
contentType(create) mustBe Some("application/json")
contentAsString(create) must include("errors")
contentAsString(create) must include("\"email\":[\"must not be empty\"]")
}
"returns 201 if request is acceptable" in {
DatabaseCleaner.clean(List("Users"))
val request = FakeRequest(POST, "/users").withHeaders("Authorization" -> "token").withJsonBody(Json.parse("""{ "user": {"email":"[email protected]", "password":"pass"} }"""))
val create = route(app, request).get
status(create) mustBe CREATED
contentType(create) mustBe Some("application/json")
contentAsString(create) must include("user")
contentAsString(create) must include("authToken")
}
}
}
示例15: PersonControllerSpec
//设置package包名称以及导入依赖的类
package controllers
import org.scalatest.mock.MockitoSugar
import org.scalatest.{BeforeAndAfter, FunSpec}
import play.api.mvc.Results
import play.api.test.FakeRequest
import play.api.test.Helpers._
class PersonControllerSpec extends FunSpec with BeforeAndAfter with Results with MockitoSugar {
var controller: PersonController = _
before {
controller = new PersonController
}
describe("#index") {
it("should return OK with 200") {
val request = FakeRequest("GET", "/person/1")
val response = call(controller.get(1), request)
assert(status(response) === OK)
}
}
}