当前位置: 首页>>代码示例>>Scala>>正文


Scala FakeHeaders类代码示例

本文整理汇总了Scala中play.api.test.FakeHeaders的典型用法代码示例。如果您正苦于以下问题:Scala FakeHeaders类的具体用法?Scala FakeHeaders怎么用?Scala FakeHeaders使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了FakeHeaders类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: I019_datetime_constructor_Spec

//设置package包名称以及导入依赖的类
import org.scalatestplus.play._
import play.api.mvc.{AnyContentAsEmpty, _}
import play.api.test.{FakeHeaders, _}
import play.api.test.Helpers._
import play.api.libs.json._

class I019_datetime_constructor_Spec extends PlaySpec with OneAppPerSuite {

  val exptectedContentType = "application/json"

  val headers = FakeHeaders(Seq("Accept" -> exptectedContentType))

  "I09" should {

    "be able to construct datetime objects from string" in {
      val stringRequest =
        """{
          |  "complete_before": "2016-07-25T13:15:53.208Z",
          |  "complete_after": "2016-07-25T13:15:53.208Z"
          |}""".stripMargin

      val fromString = route(app, FakeRequest(POST, "/019", headers, Json.parse(stringRequest))).get
      status(fromString) mustBe OK
      contentType(fromString) mustBe Some(exptectedContentType)
      contentAsString(fromString) must include("\"complete_before\":")
      contentAsString(fromString) must include("\"complete_after\":")
    }

    "be able to construct datetime objects from long" in {
      val longRequest =
        """{
          |  "complete_before": 1469452060,
          |  "complete_after": 1469452060
          |}""".stripMargin

      val fromLong = route(app, FakeRequest(POST, "/019", headers, Json.parse(longRequest))).get
      status(fromLong) mustBe OK
      contentType(fromLong) mustBe Some(exptectedContentType)
      contentAsString(fromLong) must include("\"complete_before\":")
      contentAsString(fromLong) must include("\"complete_after\":")
    }

    "be able to construct datetime objects from string in header and path" in {
      val request = "2016-07-25T13:15:53.208Z"
      val withHeader = FakeHeaders(("header", request) +: headers.data)
      val fromString = route(app, FakeRequest(GET, s"/019/$request", withHeader, AnyContentAsEmpty)).get
      contentAsString(fromString) must include("\"complete_before\":")
      contentAsString(fromString) must include("\"complete_after\":")
      status(fromString) mustBe OK
      contentType(fromString) mustBe Some(exptectedContentType)
    }

    "NOT be able to construct datetime objects from long in header and path" in {
      val request = 1469452060
      val withHeader = FakeHeaders(("header", request.toString) +: headers.data)
      val fromLong = route(app, FakeRequest(GET, s"/019/$request", withHeader, AnyContentAsEmpty)).get
      status(fromLong) mustBe BAD_REQUEST
    }
  }
} 
开发者ID:LappleApple,项目名称:api-first-hand,代码行数:61,代码来源:I019_datetime_constructor_Spec.scala

示例2: writes

//设置package包名称以及导入依赖的类
package helpers

import models.Person
import org.joda.time.DateTime
import org.scalatestplus.play.{ OneAppPerTest, PlaySpec }
import play.api.libs.functional.syntax._
import play.api.libs.json.{ JsPath, Json, Reads, Writes }
import play.api.mvc.Result
import play.api.test.Helpers._
import play.api.test.{ FakeHeaders, FakeRequest }

import scala.concurrent.Future

trait PersonTestHelper extends PlaySpec with OneAppPerTest {

  private val PostHeaders = FakeHeaders(Seq("Content-type" -> "application/json"))

  private implicit val PersonWrites = new Writes[Person] {
    def writes(person: Person) = Json.obj(
      "id" -> person.id,
      "name" -> person.name,
      "age" -> person.age,
      "lastUpdate" -> person.lastUpdate
    )
  }

  private implicit val PersonReads: Reads[Person] = (
    (JsPath \ "id").read[Long] and
    (JsPath \ "name").read[String] and
    (JsPath \ "age").read[Int] and
    (JsPath \ "lastUpdate").read[DateTime]
  )(Person.apply _)

  def getPeople: Future[Result] = {
    val postRequest = FakeRequest(GET, controllers.routes.PersonController.returnPeople().url)
    route(app, postRequest).get
  }

  def postPerson(person: Person): Future[Result] = {
    val json = Json.toJson(person)
    val request = FakeRequest(POST, controllers.routes.PersonController.createPerson().url, PostHeaders, json)
    route(app, request).get
  }

  def persons(response: Future[Result]): Seq[Person] = {
    Json.fromJson[Seq[Person]](Json.parse(contentAsString(response))).get
  }
} 
开发者ID:marciogualtieri,项目名称:PersonService,代码行数:49,代码来源:PersonTestHelper.scala

示例3: TestBuilder

//设置package包名称以及导入依赖的类
package models

import play.api.Application
import play.api.mvc.{AnyContentAsEmpty, Result}
import play.api.test.{FakeHeaders, FakeRequest, Helpers}
import play.mvc.Call

import scala.concurrent.Future

object TestBuilder {

  object anUser {

    import play.api.test.Helpers._
    def GET(call: Call)(implicit app: Application): Option[Future[Result]] =
      route(app, fakeUnauthenticatedRequest(Helpers.GET, call.url(), FakeHeaders(), AnyContentAsEmpty))

    def POST[A](call: Call)(body: A)(implicit w: play.api.http.Writeable[A], app: Application): Option[Future[Result]] =
      route(app, fakeUnauthenticatedRequest(Helpers.POST, call.url(), jsonHeaders, body))

    private def fakeUnauthenticatedRequest[A](method: String, uri: String, headers: FakeHeaders, body: A): FakeRequest[A] = FakeRequest(
      method,
      uri,
      withAjaxHeader(headers),
      body
    )

    private def jsonHeaders = FakeHeaders(
      Seq("Content-Type" -> "application/json")
    )

    private def withAjaxHeader(headers: FakeHeaders): FakeHeaders = headers.copy(data = headers.data :+ ("X-Requested-With" -> "XMLHttpRequest"))
  }

} 
开发者ID:fagossa,项目名称:play-reactive-couchbase,代码行数:36,代码来源:TestBuilder.scala

示例4: AdvertiserControllerSpec

//设置package包名称以及导入依赖的类
package scala.controllers.advert

import models.advert.{Advertiser, AdvertiserBusinessUpdate}
import models.social.{M8User, M8UserList, M8UserPosition}
import play.api.http.Status
import play.api.libs.json.Json
import play.api.test.Helpers._
import play.api.test.{FakeHeaders, FakeRequest, Helpers}
import utils._

class AdvertiserControllerSpec extends UnitSpec {
  "AdvertiserController" must {
    "get the advert by id" in {
      val vladResult = registerVlad.get
      val advertiserResult = registerWeiZhengAsAdvertiser.get

      M8User.updatePosition(vladResult.userId, M8UserPosition(-25.274398, 133.775126))
      Advertiser.confirmEmail(advertiserResult.advertiserId, advertiserResult.emailConfirmToken)
      activeAdvertiser(advertiserResult.advertiserId)

      Advertiser.updateBusiness(advertiserResult.advertiserId,
        AdvertiserBusinessUpdate("Crazydog Apps", Some("012345678"),
          Some("www.crazydog.com.au"), "4/15 Robinson Street", -25.274398, 133.775136, "We are a software company"))

      val result = M8User.listAdvertsNearby(vladResult.userId, M8UserList(None, None, 0, 10))
      val fakeRequest = FakeRequest(Helpers.GET,
        controllers.advert.routes.AdvertiserController.getAdvert(result.get(0).advertId).url,
        FakeHeaders(Seq(MobileApiTokenHeader -> Seq(mobileApiToken),
          MobileAccessTokenHeader -> Seq(vladResult.accessToken))),
        Json.obj()
      )

      verifyHttpResult(route(fakeRequest).get, Status.OK, "Crazydog Apps")
    }
  }
} 
开发者ID:pro-zw,项目名称:m8chat,代码行数:37,代码来源:AdvertiserControllerSpec.scala

示例5: AgentEpayeRegistrationControllerISpec

//设置package包名称以及导入依赖的类
package uk.gov.hmrc.agentepayeregistration.controllers

import play.api.libs.json.{JsObject, JsString, JsValue, Json}
import play.api.test.Helpers._
import play.api.test.{FakeHeaders, FakeRequest}
import uk.gov.hmrc.agentepayeregistration.repository.AgentEpayeRegistrationRepository

class AgentEpayeRegistrationControllerISpec extends BaseControllerISpec {
  private lazy val controller: AgentEpayeRegistrationController = app.injector.instanceOf[AgentEpayeRegistrationController]
  private lazy val repo = app.injector.instanceOf[AgentEpayeRegistrationRepository]

  val validPostData: JsObject = Json.obj(
    "agentName" -> "Jim Jiminy",
    "contactName" -> "John Johnson",
    "address" -> Json.obj(
      "addressLine1" -> "Line 1",
      "addressLine2" -> "Line 2",
      "postCode" -> "AB111AA"
    )
  )

  "submitting valid details to /registrations" should {
    "respond with HTTP 200 with a the new unique PAYE code in the response body" in {
      val request = FakeRequest(POST, "/registrations", FakeHeaders(), validPostData)
      val result = await(controller.register(request))

      status(result) shouldBe 200
      contentAsJson(result) shouldBe Json.obj("payeAgentReference" -> "HX2000")
    }
  }

  "submitting invalid details to /registrations" should {
    "respond with HTTP 400 Bad Request" when {
      "no details are given" in {
        val request = FakeRequest(POST, "/registrations", FakeHeaders(), Json.obj())
        val result = await(controller.register(request))

        status(result) shouldBe 400
      }

      "some mandatory field was missing" in {
        val postDataMissingField = validPostData - "agentName"

        val request = FakeRequest(POST, "/registrations", FakeHeaders(), postDataMissingField)
        val result = await(controller.register(request))

        status(result) shouldBe 400
      }

      "some field was invalid" in {
        val postDataInvalidField = validPostData + ("agentName", JsString("Invalid#Name"))

        val request = FakeRequest(POST, "/registrations", FakeHeaders(), postDataInvalidField)
        val result = await(controller.register(request))

        status(result) shouldBe 400
      }
    }
  }
} 
开发者ID:hmrc,项目名称:agent-epaye-registration,代码行数:61,代码来源:AgentEpayeRegistrationControllerISpec.scala

示例6:

//设置package包名称以及导入依赖的类
package toguru.play

import play.api.http.HeaderNames
import play.api.mvc.{Cookie, Cookies}
import play.api.test.{FakeHeaders, FakeRequest}
import toguru.api.ClientInfo

trait RequestHelpers {
  val client: PlayClientProvider = { implicit request =>
    import PlaySupport._
    ClientInfo(uuidFromCookieValue("myVisitor"), forcedToggle)
      .withAttribute(fromCookie("culture"))
      .withAttribute(fromHeader(UserAgent))
  }

  val UserAgent = HeaderNames.USER_AGENT

  val fakeHeaders = FakeHeaders(
    Seq(
      UserAgent -> "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
      "X-toguru" -> "feature1-Forced-By-Header=true|feature2-Forced-By-Header=true",
      HeaderNames.COOKIE ->
        Cookies.encodeCookieHeader(Seq(
          Cookie("culture", "de-DE"),
          Cookie("myVisitor", "a5f409eb-2fdd-4499-b65b-b22bd7e51aa2"),
          Cookie(
            "toguru",
            "feature1-Forced-By-Cookie=true|feature2-Forced-By-Cookie=true")
        ))
    ))

  val request = FakeRequest.apply(
    "GET",
    "http://priceestimation.autoscout24.de?toguru=feature-forced-by-query-param%3Dtrue",
    fakeHeaders,
    "body")
  val requestWithToggleIdUpppercased = FakeRequest.apply(
    "GET",
    "http://priceestimation.autoscout24.de?toguru=feature-FORCED-by-query-param%3Dtrue",
    fakeHeaders,
    "body")
  val requestWithTwoTogglesInQueryString = FakeRequest.apply(
    "GET",
    "http://priceestimation.autoscout24.de?toguru=feature1-forced-by-query-param%3Dtrue%7Cfeature2-forced-by-query-param%3Dfalse",
    fakeHeaders,
    "body"
  )
} 
开发者ID:AutoScout24,项目名称:toguru-scala-client,代码行数:49,代码来源:RequestHelpers.scala


注:本文中的play.api.test.FakeHeaders类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。