Trait/Object

org.iainhull.resttest

Dsl

Related Docs: object Dsl | package resttest

Permalink

trait Dsl extends Api with Extractors

Provides a DSL for simplifying REST system tests. This is meant to be used with ScalaTest or similar testing framework.

For example to post a json document to a REST endpoint and check the statusCode:

val personJson = """{ "name": "fred" }"""
POST url "http://api.rest.org/person" body personJson asserting (statusCode is Status.Created)

Or to get a json document from a REST endpoint and convert the json array to a List of Person objects:

val people = GET url "http://api.rest.org/person" returning (jsonBodyAsList[Person])

Finally a more complete example that using a ScalaTest Spec to verify a simple REST API.

class DslSpec extends FlatSpec with Dsl {
  "An empty api" should "support adding and deleting a single object" {
    using (_ url "http://api.rest.org/person") { implicit rb =>
      GET asserting (statusCode is Status.OK, jsonBodyAsList[Person] is EmptyList)
      val id = POST body personJson asserting (statusCode is Status.Created) returning (header("X-Person-Id"))
      GET / id asserting (statusCode is Status.OK, jsonBodyAs[Person] is Jason)
      GET asserting (statusCode is Status.OK, jsonBodyAsList[Person] is Seq(Jason))
      DELETE / id asserting (statusCode is Status.OK)
      GET / id asserting (statusCode is Status.NotFound)
      GET asserting (statusCode is Status.OK, jsonBodyAsList[Person] is EmptyList)
    }
  }
}

Configuring a Request

The DSL centers around the Api.RequestBuilder, which specifies the properties of the request. Most expressions begin with the HTTP Api.Method followed by a call to RichRequestBuilder, this converts the Method to a Api.RequestBuilder. The resulting RequestBuilder contains both the Method and secondary property. For example:

GET url "http://api.rest.org/person"

is the same as

RequestBuilder().withMethod(GET).withUrl("http://api.rest.org/person")

The RequestBuilder DSL also supports default values passed implicitly into expressions, for example:

implicit val defaults = RequestBuilder() addHeader ("Accept", "application/json")
GET url "http://api.rest.org/person"

creates a RequestBuilder with a method, url and accept header set. The default values are normal expressed the with the using expression.

Executing a Request

There are three ways to execute a request: RichRequestBuilder.execute, RichResponse.returning, RichRequestBuilder.asserting, these can all be applied to RequestBuilder instances.

The execute method executes the request with the implicit Api.HttpClient and returns the Response.

val response: Response = GET url "http://api.rest.org/person" execute ()

The returning method executes the request like the execute method, except it applies one or more Extractors to the Response to return only the extracted information.

val code1 = GET url "http://api.rest.org/person" returning (StatusCode)
val (code2, people) = GET url "http://api.rest.org/person" returning (StatusCode, jsonBodyAsList[Person])

The asserting method executes the request like the execute method, except it verifies the specified value of one or more Response values. asserting is normally used with extractors, see [RichExtractor] for more information. asserting and returning methods can be used in the same expression.

GET url "http://api.rest.org/person" asserting (statusCode is Status.OK)
val people = GET url "http://api.rest.org/person" asserting (statusCode is Status.OK) returning (jsonBodyAsList[Person])

Working with Extractors

Extractors are simply functions that take a Api.Response are extract or convert part of its contents. Extracts are written to assume that the data they require is in the response, if it is not they throw an Exception (failing the test). See Extractors for more information on the available default Extractors And how to implement your own.

Linear Supertypes
Extractors, Api, AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Dsl
  2. Extractors
  3. Api
  4. AnyRef
  5. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. type Assertion = (Response) ⇒ Option[String]

    Permalink
  2. type Extractor[+A] = Extractors.Extractor[A]

    Permalink
    Definition Classes
    Extractors
  3. type ExtractorFailedException = Extractors.ExtractorFailedException

    Permalink
    Definition Classes
    Extractors
  4. type ExtractorLike[+A] = Extractors.ExtractorLike[A]

    Permalink
    Definition Classes
    Extractors
  5. type Header = Extractors.Header

    Permalink
    Definition Classes
    Extractors
  6. type HttpClient = (Api.Request) ⇒ Api.Response

    Permalink

    HttpClient

    HttpClient

    Definition Classes
    Api
  7. type Method = Api.Method

    Permalink

    The HTTP Methods used to make a request

    The HTTP Methods used to make a request

    Definition Classes
    Api
  8. type Request = Api.Request

    Permalink

    The HTTP Request

    The HTTP Request

    Definition Classes
    Api
  9. type RequestBuilder = Api.RequestBuilder

    Permalink

    The HTTP RequestBuilder

    The HTTP RequestBuilder

    Definition Classes
    Api
  10. type Response = Api.Response

    Permalink

    The HTTP Response

    The HTTP Response

    Definition Classes
    Api
  11. implicit class RichExtractor[A] extends AnyRef

    Permalink

    Add operator support to Extractors these are used to generate an Assertion using the extracted value.

    Add operator support to Extractors these are used to generate an Assertion using the extracted value.

    GET url "http://api.rest.org/person" assert (StatusCode === Status.Ok)

    Operations

    The following operations are added to all Extractors

    $ - extractor === expected - the extracted value is equal to the expected value. $ - extractor !== expected - the extracted value is not equal to the expected value. $ - extractor in (expected1, expected2, ...) - the extracted value is in the list of expected values. $ - extractor notIn (expected1, expected2, ...) - the extracted value is in the list of expected values.

    The following operations are added to Extractors that support scala.math.Ordering. More precisely these operations are added to Extractor[T] if there exists an implicit Ordering[T] for any type T.

    $ - extractor < expected - the extracted value is less than the expected value. $ - extractor <= expected - the extracted value is less than or equal to the expected value. $ - extractor > expected - the extracted value is greater than the expected value. $ - extractor <= expected - the extracted value is greater than or equal to the expected value.

  12. implicit class RichRequestBuilder extends AnyRef

    Permalink
  13. implicit class RichResponse extends AnyRef

    Permalink

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. val &: Extractors.&.type

    Permalink
    Definition Classes
    Extractors
  4. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  5. val Body: Extractors.Extractor[Option[String]]

    Permalink
    Definition Classes
    Extractors
  6. val BodyText: Extractors.Extractor[String]

    Permalink
    Definition Classes
    Extractors
  7. val DELETE: Api.DELETE.type

    Permalink
    Definition Classes
    Api
  8. val Extractor: Extractors.Extractor.type

    Permalink
    Definition Classes
    Extractors
  9. val GET: Api.GET.type

    Permalink
    Definition Classes
    Api
  10. val HEAD: Api.HEAD.type

    Permalink
    Definition Classes
    Api
  11. val Header: Extractors.Header.type

    Permalink
    Definition Classes
    Extractors
  12. val PATCH: Api.PATCH.type

    Permalink
    Definition Classes
    Api
  13. val POST: Api.POST.type

    Permalink
    Definition Classes
    Api
  14. val PUT: Api.PUT.type

    Permalink
    Definition Classes
    Api
  15. val Request: Api.Request.type

    Permalink
    Definition Classes
    Api
  16. val RequestBuilder: Api.RequestBuilder.type

    Permalink
    Definition Classes
    Api
  17. val Response: Api.Response.type

    Permalink
    Definition Classes
    Api
  18. val Status: Api.Status.type

    Permalink
    Definition Classes
    Api
  19. val StatusCode: Extractors.Extractor[Int]

    Permalink
    Definition Classes
    Extractors
  20. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  21. def assertionFailed(assertionResults: Seq[String]): Throwable

    Permalink
  22. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  23. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  24. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  25. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  26. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  27. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  28. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  29. implicit def methodToRequestBuilder(method: Method)(implicit builder: RequestBuilder): RequestBuilder

    Permalink
  30. implicit def methodToRichRequestBuilder(method: Method)(implicit builder: RequestBuilder): RichRequestBuilder

    Permalink
  31. implicit def methodToRichResponse(method: Method)(implicit builder: RequestBuilder, client: HttpClient): RichResponse

    Permalink
  32. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  33. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  34. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  35. implicit def requestBuilderToRichResponse(builder: RequestBuilder)(implicit client: HttpClient): RichResponse

    Permalink
  36. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  37. def toHeaders(hs: (String, String)*): Map[String, List[String]]

    Permalink
    Definition Classes
    Api
  38. def toQueryString(qs: (String, String)*): String

    Permalink
    Definition Classes
    Api
  39. implicit def toRequest(builder: RequestBuilder): Request

    Permalink
  40. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  41. def using(config: (RequestBuilder) ⇒ RequestBuilder)(process: (RequestBuilder) ⇒ Unit)(implicit builder: RequestBuilder): Unit

    Permalink

    Extend the default request's configuration so that partially configured requests to be reused.

    Extend the default request's configuration so that partially configured requests to be reused. Foe example:

    using(_ url "http://api.rest.org/person") { implicit rb =>
      GET asserting (StatusCode === Status.OK, jsonBodyAsList[Person] === EmptyList)
      val id = POST body personJson asserting (StatusCode === Status.Created) returning (Header("X-Person-Id"))
      GET / id asserting (StatusCode === Status.OK, jsonBodyAs[Person] === Jason)
    }
    config

    a function to configure the default request

    builder

    the current default request, implicitly resolved, defaults to the empty request

  42. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  43. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  44. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from Extractors

Inherited from Api

Inherited from AnyRef

Inherited from Any

Ungrouped