Coder Social home page Coder Social logo

zio-test-akka-http's Introduction

zio-test-akka-http

CI Release Issues Snapshot
Build Status Release Artifacts Average time to resolve an issue Snapshot Artifacts

Akka-HTTP route TestKit for zio-test

libraryDependencies += "info.senia" %% "zio-test-akka-http" % "x.x.x"

Usage

The basic structure of a test built with the testkit is this (expression placeholder in all-caps):

(REQUEST ~> ROUTE).map { res =>
  assertTrue(res.some.path == value)
}

See RouteZIOSpecDefaultSpec.scala for usage examples.

Imports

Import zio.test.akkahttp.assertions._ and provide RouteTestEnvironment to use this testkit:

import akka.http.scaladsl.server.Directives.complete
import akka.http.scaladsl.model.HttpResponse
import zio.test.Assertion._
import zio.test._
import zio.test.akkahttp.RouteTestEnvironment
import zio.test.akkahttp.assertions._

object MySpec extends ZIOSpecDefault {
  def spec =
    suite("MySpec")(
      test("my test") {
        (Get() ~> complete(HttpResponse())).map { res =>
          assertTrue(res.handled.get.response == HttpResponse())
        }
      }
    ).provideShared(RouteTestEnvironment.environment)
}

Alternatively you can use DefaultAkkaRunnableSpec without importing assertions._ and providing additional environment:

import akka.http.scaladsl.server.Directives.complete
import akka.http.scaladsl.model.HttpResponse
import zio.test.Assertion._
import zio.test._
import zio.test.akkahttp.AkkaZIOSpecDefault

object MySpec extends AkkaZIOSpecDefault {
  def spec =
    suite("MySpec")(
      test("my test") {
        (Get() ~> complete(HttpResponse())).map { res =>
          assertTrue(res.handled.get.response == HttpResponse())
        }
      }
    )
}

Request creation

You can use HttpRequest or ZIO[R, E, HttpRequest] as request:

import akka.http.scaladsl.model.HttpResponse
import akka.http.scaladsl.server.Directives.complete
import zio.test.Assertion._
import zio.test._
import zio.test.akkahttp.assertions._

(Get() ~> complete(HttpResponse())).map { res =>
  assertTrue(res.handled.get.response == HttpResponse())
}

Available request builders: Get,Post,Put,Patch,Delete,Options,Head.

Request modification

You can use any function HttpRequest => HttpRequest to modify request.

There are several common request modifications provided: addHeader,mapHeaders,removeHeader,addCredentials:

Get() ~> addHeader("MyHeader", "value") ~> route

You can also add header with ~> method:

Get() ~> RawHeader("MyHeader", "value") ~> route

Assertions

There are several methods on response available:

  • handled
  • response
  • responseEntity
  • chunks
  • entityAs
  • responseAs
  • contentType
  • mediaType
  • charset
  • headers
  • header
  • status
  • closingExtension
  • trailer
  • rejected
  • isWebSocketUpgrade
  • webSocketUpgradeProtocol

There should be a method for every inspector from original Akka-HTTP Route TestKit. If you can't find a method for existing inspector please open an issue.

Note that some methods are available only on eager version of response, but not on lazy one. You can always convert lazy response to eager with toEager method.

It's sage to call get on Option inside of asertTrue macros.

Note that response is eager on response fetching by default - you can't use ~> for status code and headers if route returns an infinite body.

To avoid eager response body fetching use lazy ?~> method instead of the last ~>:

import akka.http.scaladsl.model.StatusCodes.OK
import akka.http.scaladsl.server.Directives.{complete, get, respondWithHeader}
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpResponse}
import akka.http.scaladsl.model.headers.RawHeader
import akka.http.scaladsl.server.Directives
import akka.stream.scaladsl.Source
import akka.util.ByteString
import zio.test.Assertion._
import zio.test._
import zio.test.akkahttp.RouteTestEnvironment
import zio.test.akkahttp.assertions._

val pinkHeader = RawHeader("Fancy", "pink")

val route = get {
  respondWithHeader(pinkHeader) {
    complete(HttpEntity(ContentTypes.`application/octet-stream`, Source.repeat(ByteString("abc"))))
  }
}

(Get() ?~> route).map { res =>
  assertTrue(
    res.handled.get.status == OK,
    res.handled.get.header("Fancy").get == pinkHeader,
  )
}

zio-test-akka-http's People

Contributors

senia-psm avatar scala-steward avatar domdorn avatar googley42 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.