<commit_before>package spray

import akka.actor.{Actor, ActorSystem, Props}
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import org.specs2.mutable._
import spray.can.Http
import spray.http.MediaTypes
import spray.json._
import spray.routing._
import spray.testkit.Specs2RouteTest

import scala.concurrent.duration._

case class Message(text: String)

trait RestService extends HttpService with DefaultJsonProtocol {
  implicit val messageFormat = jsonFormat1(Message)

  val restServiceRoute = {
    import spray.httpx.SprayJsonSupport._
    path("") {
      get {
        respondWithMediaType(MediaTypes.`application/json`) {
          complete(Message("get rest message"))
        }
      } ~
      post {
        respondWithMediaType(MediaTypes.`application/json`) {
          complete(Message("post rest message"))
        }
      } ~
      put {
        respondWithMediaType(MediaTypes.`application/json`) {
          complete(Message("put rest message"))
        }
      } ~
      delete {
        respondWithMediaType(MediaTypes.`application/json`) {
          complete(Message("delete rest message"))
        }
      }
    }
  }
}

class RestServiceActor extends RestService with Actor {
  def actorRefFactory = context
  def receive = runRoute(restServiceRoute)
}

class RestServiceRunner {
  implicit val timeout = Timeout(3.seconds)
  implicit val system = ActorSystem("system")
  val restServiceActor = system.actorOf(Props[RestServiceActor], "rest-service-actor")
  IO(Http) ? Http.Bind(restServiceActor, interface = "localhost", port = 9999)
}

class SprayTest extends Specification with Specs2RouteTest with RestService {
  def restServiceRunner = new RestServiceRunner
  def actorRefFactory = restServiceRunner.system
  import spray.httpx.SprayJsonSupport._

  "RestService" should {
    "handle Get, Post, Put and Delete json Messages." in {
      Get() ~> restServiceRoute ~> check {
        responseAs[Message] === Message("get rest message")
      }
      Post() ~> restServiceRoute ~> check {
        responseAs[Message] === Message("post rest message")
      }
      Put() ~> restServiceRoute ~> check {
        responseAs[Message] === Message("put rest message")
      }
      Delete() ~> restServiceRoute ~> check {
        responseAs[Message] === Message("delete rest message")
      }
    }
  }<commit_msg>added media type test<commit_after>package spray

import akka.actor.{Actor, ActorSystem, Props}
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import org.specs2.mutable._
import spray.can.Http
import spray.http.MediaTypes._
import spray.json._
import spray.routing._
import spray.testkit.Specs2RouteTest

import scala.concurrent.duration._

case class Message(text: String)

trait RestService extends HttpService with DefaultJsonProtocol {
  implicit val messageFormat = jsonFormat1(Message)

  val restServiceRoute = {
    import spray.httpx.SprayJsonSupport._
    path("") {
      get {
        respondWithMediaType(`application/json`) {
          complete(Message("get rest message"))
        }
      } ~
      post {
        respondWithMediaType(`application/json`) {
          complete(Message("post rest message"))
        }
      } ~
      put {
        respondWithMediaType(`application/json`) {
          complete(Message("put rest message"))
        }
      } ~
      delete {
        respondWithMediaType(`application/json`) {
          complete(Message("delete rest message"))
        }
      }
    }
  }
}

class RestServiceActor extends RestService with Actor {
  def actorRefFactory = context
  def receive = runRoute(restServiceRoute)
}

class RestServiceRunner {
  implicit val timeout = Timeout(3.seconds)
  implicit val system = ActorSystem("system")
  val restServiceActor = system.actorOf(Props[RestServiceActor], "rest-service-actor")
  IO(Http) ? Http.Bind(restServiceActor, interface = "localhost", port = 9999)
}

class SprayTest extends Specification with Specs2RouteTest with RestService {
  def restServiceRunner = new RestServiceRunner
  def actorRefFactory = restServiceRunner.system
  import spray.httpx.SprayJsonSupport._

  "RestService" should {
    "handle Get, Post, Put and Delete json Messages." in {
      Get() ~> restServiceRoute ~> check {
        mediaType === `application/json`
        responseAs[Message] === Message("get rest message")
      }
      Post() ~> restServiceRoute ~> check {
        mediaType === `application/json`
        responseAs[Message] === Message("post rest message")
      }
      Put() ~> restServiceRoute ~> check {
        mediaType === `application/json`
        responseAs[Message] === Message("put rest message")
      }
      Delete() ~> restServiceRoute ~> check {
        mediaType === `application/json`
        responseAs[Message] === Message("delete rest message")
      }
    }
  }
{'fraction_non_alphanumeric': 0.1052087639520463, 'fraction_numerical': 0.0037205456800330715, 'mean_word_length': 3.196877710320902, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6067253', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1341, 'n_words': 317}