akka.http.scaladsl.model.HttpMethods Scala Examples

The following examples show how to use akka.http.scaladsl.model.HttpMethods. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: TwitterFollowerClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.rest.followers

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities.{RatedData, UserIds, UserStringifiedIds, Users}
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterFollowerClientSpec extends ClientSpec {

  class TwitterFollowerClientSpecContext extends RestClientSpecContext with TwitterFollowerClient

  "Twitter Follower Client" should {

    "get followers ids of a specific user by id" in new TwitterFollowerClientSpecContext {
      val result: RatedData[UserIds] = when(followerIdsForUserId(2911461333L))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/followers/ids.json"
          request.uri.rawQueryString === Some("count=5000&cursor=-1&stringify_ids=false&user_id=2911461333")
        }
        .respondWithRated("/twitter/rest/followers/followers_ids.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[UserIds]("/fixtures/rest/followers/followers_ids.json")
    }

    "get followers ids of a specific user by name" in new TwitterFollowerClientSpecContext {
      val result: RatedData[UserIds] = when(followerIdsForUser("DanielaSfregola"))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/followers/ids.json"
          request.uri.rawQueryString === Some("count=5000&cursor=-1&screen_name=DanielaSfregola&stringify_ids=false")
        }
        .respondWithRated("/twitter/rest/followers/followers_ids.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[UserIds]("/fixtures/rest/followers/followers_ids.json")
    }

    "get followers stringified ids of a specific user by id" in new TwitterFollowerClientSpecContext {
      val result: RatedData[UserStringifiedIds] = when(followerStringifiedIdsForUserId(2911461333L))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/followers/ids.json"
          request.uri.rawQueryString === Some("count=5000&cursor=-1&stringify_ids=true&user_id=2911461333")
        }
        .respondWithRated("/twitter/rest/followers/followers_ids_stringified.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[UserStringifiedIds]("/fixtures/rest/followers/followers_ids_stringified.json")
    }

    "get followers stringified ids of a specific user by name" in new TwitterFollowerClientSpecContext {
      val result: RatedData[UserStringifiedIds] = when(followersStringifiedIdsForUser("DanielaSfregola"))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/followers/ids.json"
          request.uri.rawQueryString === Some("count=5000&cursor=-1&screen_name=DanielaSfregola&stringify_ids=true")
        }
        .respondWithRated("/twitter/rest/followers/followers_ids_stringified.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[UserStringifiedIds]("/fixtures/rest/followers/followers_ids_stringified.json")
    }

    "get followers of a specific user" in new TwitterFollowerClientSpecContext {
      val result: RatedData[Users] = when(followersForUser("DanielaSfregola"))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/followers/list.json"
          request.uri.rawQueryString === Some(
            "count=20&cursor=-1&include_user_entities=true&screen_name=DanielaSfregola&skip_status=false")
        }
        .respondWithRated("/twitter/rest/followers/followers.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Users]("/fixtures/rest/followers/followers.json")
    }

    "get followers of a specific user by user id" in new TwitterFollowerClientSpecContext {
      val result: RatedData[Users] = when(followersForUserId(2911461333L))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/followers/list.json"
          request.uri.rawQueryString === Some(
            "count=20&cursor=-1&include_user_entities=true&skip_status=false&user_id=2911461333")
        }
        .respondWithRated("/twitter/rest/followers/followers.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Users]("/fixtures/rest/followers/followers.json")
    }
  }
} 
Example 2
Source File: WebHandler.scala    From akka-grpc   with Apache License 2.0 5 votes vote down vote up
package akka.grpc.scaladsl

import scala.collection.immutable
import scala.concurrent.Future
import akka.actor.ClassicActorSystemProvider
import akka.annotation.ApiMayChange
import akka.http.javadsl.{ model => jmodel }
import akka.http.scaladsl.model.{ HttpMethods, HttpRequest, HttpResponse }
import akka.http.scaladsl.model.headers._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.directives.MarshallingDirectives.handleWith
import ch.megard.akka.http.cors.scaladsl.CorsDirectives.cors
import ch.megard.akka.http.cors.scaladsl.model.HttpHeaderRange
import ch.megard.akka.http.cors.scaladsl.settings.CorsSettings

@ApiMayChange
object WebHandler {

  
  def grpcWebHandler(handlers: PartialFunction[HttpRequest, Future[HttpResponse]]*)(
      implicit as: ClassicActorSystemProvider,
      corsSettings: CorsSettings = defaultCorsSettings): HttpRequest => Future[HttpResponse] = {
    implicit val system = as.classicSystem
    val servicesHandler = ServiceHandler.concat(handlers: _*)
    Route.asyncHandler(cors(corsSettings) {
      handleWith(servicesHandler)
    })
  }

} 
Example 3
Source File: GrpcRequestHelpers.scala    From akka-grpc   with Apache License 2.0 5 votes vote down vote up
package akka.grpc.internal

import akka.actor.ActorSystem
import akka.actor.ClassicActorSystemProvider
import akka.grpc.{ ProtobufSerializer, Trailers }
import akka.grpc.GrpcProtocol.GrpcProtocolWriter
import akka.http.scaladsl.model.HttpEntity.ChunkStreamPart
import akka.stream.scaladsl.Source
import akka.NotUsed
import akka.annotation.InternalApi
import akka.grpc.scaladsl.{ headers, GrpcExceptionHandler }
import akka.http.scaladsl.model.{ HttpEntity, HttpMethods, HttpRequest, Uri }
import io.grpc.Status

import scala.collection.immutable

@InternalApi
object GrpcRequestHelpers {

  def apply[T](
      uri: Uri,
      e: Source[T, NotUsed],
      eHandler: ActorSystem => PartialFunction[Throwable, Trailers] = GrpcExceptionHandler.defaultMapper)(
      implicit m: ProtobufSerializer[T],
      writer: GrpcProtocolWriter,
      system: ClassicActorSystemProvider): HttpRequest =
    request(uri, GrpcEntityHelpers(e, Source.single(GrpcEntityHelpers.trailer(Status.OK)), eHandler))

  private def request[T](uri: Uri, entity: Source[ChunkStreamPart, NotUsed])(
      implicit writer: GrpcProtocolWriter): HttpRequest = {
    HttpRequest(
      uri = uri,
      method = HttpMethods.POST,
      headers = immutable.Seq(
        headers.`Message-Encoding`(writer.messageEncoding.name),
        headers.`Message-Accept-Encoding`(Codecs.supportedCodecs.map(_.name).mkString(","))),
      entity = HttpEntity.Chunked(writer.contentType, entity))
  }

} 
Example 4
Source File: AkkaHttpServerWithMinio.scala    From c4proto   with Apache License 2.0 5 votes vote down vote up
package ee.cone.c4gate_akka_s3

import java.util.UUID

import akka.http.scaladsl.model.{HttpMethods, HttpRequest}
import akka.stream.scaladsl.StreamConverters
import com.typesafe.scalalogging.LazyLogging
import ee.cone.c4actor_s3.S3FileStorage
import ee.cone.c4di.c4
import ee.cone.c4gate_akka._

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}

@c4("AkkaMinioGatewayApp") final class AkkaMinioRequestPreHandler(
  s3FileStorage: S3FileStorage,
  akkaMat: AkkaMat,
) extends AkkaRequestPreHandler with LazyLogging {
  def handleAsync(
    income: HttpRequest
  )(
    implicit ec: ExecutionContext
  ): Future[HttpRequest] = if (income.method == HttpMethods.PUT) {
    for { mat <- akkaMat.get } yield {
      val tmpFilename: String = s"tmp/${UUID.randomUUID()}"
      logger debug s"PUT request received; Storing request body to $tmpFilename"
      val is = income.entity.dataBytes.runWith(StreamConverters.asInputStream(5.minutes))(mat) //todo check throw
      logger debug s"Bytes Stream created"
      s3FileStorage.uploadByteStream(tmpFilename, is)
      logger debug s"Uploaded bytestream to $tmpFilename"
      // ? income.headers
      income.withEntity(tmpFilename)
    }
  } else Future.successful(income)
} 
Example 5
Source File: CorsBenchmark.scala    From akka-http-cors   with Apache License 2.0 5 votes vote down vote up
package ch.megard.akka.http.cors

import java.util.concurrent.TimeUnit

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.Http.ServerBinding
import akka.http.scaladsl.model.headers.{Origin, `Access-Control-Request-Method`}
import akka.http.scaladsl.model.{HttpMethods, HttpRequest}
import akka.http.scaladsl.server.Directives
import akka.http.scaladsl.unmarshalling.Unmarshal
import ch.megard.akka.http.cors.scaladsl.CorsDirectives
import ch.megard.akka.http.cors.scaladsl.settings.CorsSettings
import com.typesafe.config.ConfigFactory
import org.openjdk.jmh.annotations._

import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext}

@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.SECONDS)
@BenchmarkMode(Array(Mode.Throughput))
class CorsBenchmark extends Directives with CorsDirectives {
  private val config = ConfigFactory.parseString("akka.loglevel = ERROR").withFallback(ConfigFactory.load())

  implicit private val system: ActorSystem  = ActorSystem("CorsBenchmark", config)
  implicit private val ec: ExecutionContext = scala.concurrent.ExecutionContext.global

  private val http         = Http()
  private val corsSettings = CorsSettings.default

  private var binding: ServerBinding        = _
  private var request: HttpRequest          = _
  private var requestCors: HttpRequest      = _
  private var requestPreflight: HttpRequest = _

  @Setup
  def setup(): Unit = {
    val route = {
      path("baseline") {
        get {
          complete("ok")
        }
      } ~ path("cors") {
        cors(corsSettings) {
          get {
            complete("ok")
          }
        }
      }
    }
    val origin = Origin("http://example.com")

    binding = Await.result(http.bindAndHandle(route, "127.0.0.1", 0), 1.second)
    val base = s"http://${binding.localAddress.getHostString}:${binding.localAddress.getPort}"

    request = HttpRequest(uri = base + "/baseline")
    requestCors = HttpRequest(
      method = HttpMethods.GET,
      uri = base + "/cors",
      headers = List(origin)
    )
    requestPreflight = HttpRequest(
      method = HttpMethods.OPTIONS,
      uri = base + "/cors",
      headers = List(origin, `Access-Control-Request-Method`(HttpMethods.GET))
    )
  }

  @TearDown
  def shutdown(): Unit = {
    val f = for {
      _ <- http.shutdownAllConnectionPools()
      _ <- binding.terminate(1.second)
      _ <- system.terminate()
    } yield ()
    Await.ready(f, 5.seconds)
  }

  @Benchmark
  def baseline(): Unit = {
    val f = http.singleRequest(request).flatMap(r => Unmarshal(r.entity).to[String])
    assert(Await.result(f, 1.second) == "ok")
  }

  @Benchmark
  def default_cors(): Unit = {
    val f = http.singleRequest(requestCors).flatMap(r => Unmarshal(r.entity).to[String])
    assert(Await.result(f, 1.second) == "ok")
  }

  @Benchmark
  def default_preflight(): Unit = {
    val f = http.singleRequest(requestPreflight).flatMap(r => Unmarshal(r.entity).to[String])
    assert(Await.result(f, 1.second) == "")
  }
} 
Example 6
Source File: AkkaHTTPClient.scala    From learn-akka   with Apache License 2.0 5 votes vote down vote up
package com.allaboutscala.learn.akka.client

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{MediaTypes, HttpEntity, HttpMethods, HttpRequest}
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import akka.util.ByteString
import com.allaboutscala.learn.akka.http.jsonsupport.{Donuts, JsonSupport}

import scala.concurrent.{Await, Future}
import scala.util.{Failure, Success}
import scala.concurrent.duration._


object AkkaHttpClient extends App with JsonSupport {

  implicit val system = ActorSystem("akka-http-donuts-client")
  implicit val materializer = ActorMaterializer()
  implicit val ec = system.dispatcher

  // HTTP GET request
  val donutsUri = "http://localhost:8080/donuts"
  val donutsHttpRequest = HttpRequest(
    uri = donutsUri,
    method = HttpMethods.GET
  )

  val donutsResponse = Http().singleRequest(donutsHttpRequest)
  donutsResponse
    .onComplete {
      case Success(donutsResponse) =>
        println(s"Raw HttpResponse = $donutsResponse")

        // You obviously should not block using Await.result(...) and use flatMap or other similar future sequencing mechanics
        val donutsF: Future[Donuts] = Unmarshal(donutsResponse).to[Donuts]
        val donuts: Donuts = Await.result(donutsF, 5.second)
        println(s"Unmarshalled HttpResponse to Case Class = $donuts")


      case Failure(e) => println(s"Failed to HTTP GET $donutsUri, error = ${e.getMessage}")
    }

  Thread.sleep(3000)



  // HTTP POST request
  val jsonDonutInput = ByteString("""{"name":"plain donut", "price":1.50}""")
  val httpPostCreateDonut = HttpRequest(
    uri = "http://localhost:8080/create-donut",
    method = HttpMethods.POST,
    entity = HttpEntity(MediaTypes.`application/json`, jsonDonutInput))

  val createDonutF = for {
    response <- Http().singleRequest(httpPostCreateDonut)
    _        = println(s"Akka HTTP request status = ${response.status}")
    if response.status.isSuccess()
    output   <- Unmarshal(response).to[String]
  } yield println(s"HTTP POST request output = $output")

  Await.result(createDonutF, 5.second)


  system.terminate()
} 
Example 7
Source File: CreateDonutTest.scala    From learn-akka   with Apache License 2.0 5 votes vote down vote up
package com.allaboutscala.learn.akka.http

import akka.http.scaladsl.model.{HttpEntity, HttpMethods, HttpRequest, MediaTypes}
import akka.http.scaladsl.testkit.ScalatestRouteTest
import akka.util.ByteString
import com.allaboutscala.learn.akka.http.routes.DonutRoutes
import org.scalatest.{Matchers, WordSpec}


class CreateDonutTest
  extends WordSpec
    with Matchers
    with ScalatestRouteTest {

  val donutRoutes = new DonutRoutes().route()

  "Donut API" should {
    "Create a valid Donut when posting JSON to /create-donut path" in {
      val jsonDonutInput = ByteString("""{"name":"plain donut", "price":1.50}""")
      val httpPostCreateDonut = HttpRequest(
        uri = "http://localhost:8080/create-donut",
        method = HttpMethods.POST,
        entity = HttpEntity(MediaTypes.`application/json`, jsonDonutInput))

      httpPostCreateDonut ~> donutRoutes ~> check {
        status.isSuccess() shouldEqual true
        status.intValue() shouldEqual 201
        status.reason shouldEqual "Created"
      }
    }
  }

} 
Example 8
Source File: TwitterOAuthClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.authentication.oauth

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities.AccessToken
import com.danielasfregola.twitter4s.entities.authentication.{OAuthAccessToken, OAuthRequestToken, RequestToken}
import com.danielasfregola.twitter4s.entities.enums.AccessType
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterOAuthClientSpec extends ClientSpec {

  class TwitterOAuthClientSpecContext extends AuthenticationClientSpecContext with TwitterOAuthClient {
    val token = RequestToken("my-oauth-token", "my-oauth-secret")
  }

  "Twitter OAuth Client" should {

    "request a oauth request token with static callback" in new TwitterOAuthClientSpecContext {
      val result: OAuthRequestToken = when(requestToken())
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/oauth/request_token"
          request.uri.rawQueryString === None
        }
        .respondWith("/twitter/authentication/request_token.txt")
        .await
      val expectedResult = OAuthRequestToken(token = RequestToken(key = "Z6eEdO8MOmk394WozF5oKyuAv855l4Mlqo7hhlSLik",
                                                                  secret = "Kd75W4OQfb2oJTV0vzGzeXftVAwgMnEK9MumzYcM"),
                                             oauth_callback_confirmed = true)
      result === expectedResult
    }

    "request a oauth request token with dynamic callback and access type" in new TwitterOAuthClientSpecContext {
      val result: OAuthRequestToken = when(requestToken(Some("http://my.callback"), Some(AccessType.Read)))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/oauth/request_token"
          request.uri.rawQueryString === Some("x_auth_access_type=read")
        }
        .respondWith("/twitter/authentication/request_token.txt")
        .await
      val expectedResult = OAuthRequestToken(token = RequestToken(key = "Z6eEdO8MOmk394WozF5oKyuAv855l4Mlqo7hhlSLik",
                                                                  secret = "Kd75W4OQfb2oJTV0vzGzeXftVAwgMnEK9MumzYcM"),
                                             oauth_callback_confirmed = true)
      result === expectedResult
    }

    "generate an authentication url" in new TwitterOAuthClientSpecContext {
      val url: String = authenticateUrl(token = token, force_login = true, screen_name = Some("DanielaSfregola"))
      val params = "oauth_token=my-oauth-token&force_login=true&screen_name=DanielaSfregola"
      url === s"https://api.twitter.com/oauth/authenticate?$params"
    }

    "generate an authorization url" in new TwitterOAuthClientSpecContext {
      val url: String = authorizeUrl(token = token, force_login = true, screen_name = Some("DanielaSfregola"))
      val params = "oauth_token=my-oauth-token&force_login=true&screen_name=DanielaSfregola"
      url === s"https://api.twitter.com/oauth/authorize?$params"
    }

    "request an access token for x_auth authentication" in new TwitterOAuthClientSpecContext {
      val result: OAuthAccessToken = when(accessToken("twitterapi", "my-secret-password"))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/oauth/access_token"
          request.uri.rawQueryString === Some(
            "x_auth_mode=client_auth&x_auth_password=my-secret-password&x_auth_username=twitterapi")
        }
        .respondWith("/twitter/authentication/access_token.txt")
        .await
      val expectedResult = OAuthAccessToken(
        accessToken = AccessToken(key = "6253282-eWudHldSbIaelX7swmsiHImEL4KinwaGloHANdrY",
                                  secret = "2EEfA6BG3ly3sR3RjE0IBSnlQu4ZrUzPiYKmrkVU"),
        user_id = 6253282,
        screen_name = "twitterapi"
      )
      result === expectedResult
    }

    "request an access token for oauth authentication" in new TwitterOAuthClientSpecContext {
      val result: OAuthAccessToken = when(accessToken(token = token, oauth_verifier = "my-token-verifier"))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/oauth/access_token"
          request.uri.rawQueryString === Some("oauth_token=my-oauth-token&oauth_verifier=my-token-verifier")
        }
        .respondWith("/twitter/authentication/access_token.txt")
        .await
      val expectedResult = OAuthAccessToken(
        accessToken = AccessToken(key = "6253282-eWudHldSbIaelX7swmsiHImEL4KinwaGloHANdrY",
                                  secret = "2EEfA6BG3ly3sR3RjE0IBSnlQu4ZrUzPiYKmrkVU"),
        user_id = 6253282,
        screen_name = "twitterapi"
      )
      result.accessToken === expectedResult.accessToken
      result.user_id === expectedResult.user_id
      result.screen_name === expectedResult.screen_name
      result === expectedResult
    }
  }
} 
Example 9
Source File: TwitterTrendClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.rest.trends

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities.{Location, LocationTrends, RatedData}
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterTrendClientSpec extends ClientSpec {

  class TwitterTrendClientSpecContext extends RestClientSpecContext with TwitterTrendClient

  "Twitter Trend Client" should {

    "get global trends" in new TwitterTrendClientSpecContext {
      val result: RatedData[Seq[LocationTrends]] = when(globalTrends())
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/trends/place.json"
          request.uri.rawQueryString === Some("id=1")
        }
        .respondWithRated("/twitter/rest/trends/trends.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Seq[LocationTrends]]("/fixtures/rest/trends/trends.json")
    }

    "get trends for a location" in new TwitterTrendClientSpecContext {
      val result: RatedData[Seq[LocationTrends]] = when(trends(1))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/trends/place.json"
          request.uri.rawQueryString === Some("id=1")
        }
        .respondWithRated("/twitter/rest/trends/trends.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Seq[LocationTrends]]("/fixtures/rest/trends/trends.json")
    }

    "get trends for a location without hashtags" in new TwitterTrendClientSpecContext {
      val result: RatedData[Seq[LocationTrends]] = when(trends(1, true))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/trends/place.json"
          request.uri.rawQueryString === Some("exclude=hashtags&id=1")
        }
        .respondWithRated("/twitter/rest/trends/trends.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Seq[LocationTrends]]("/fixtures/rest/trends/trends.json")
    }

    "get locations with available trends" in new TwitterTrendClientSpecContext {
      val result: RatedData[Seq[Location]] = when(locationTrends)
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/trends/available.json"
        }
        .respondWithRated("/twitter/rest/trends/available_locations.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Seq[Location]]("/fixtures/rest/trends/available_locations.json")
    }

    "get closest location trends" in new TwitterTrendClientSpecContext {
      val result: RatedData[Seq[Location]] = when(closestLocationTrends(37.781157, -122.400612831116))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/trends/closest.json"
        }
        .respondWithRated("/twitter/rest/trends/closest_locations.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Seq[Location]]("/fixtures/rest/trends/closest_locations.json")
    }

  }

} 
Example 10
Source File: TwitterGeoClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.rest.geo

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities.{GeoPlace, GeoSearch, RatedData}
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterGeoClientSpec extends ClientSpec {

  class TwitterGeoClientSpecContext extends RestClientSpecContext with TwitterGeoClient

  "Twitter Geo Client" should {

    "get a geo place by id" in new TwitterGeoClientSpecContext {
      val placeId = "df51dec6f4ee2b2c"
      val result: RatedData[GeoPlace] = when(geoPlace(placeId))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === s"https://api.twitter.com/1.1/geo/id/$placeId.json"
        }
        .respondWithRated("/twitter/rest/geo/geo_place.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[GeoPlace]("/fixtures/rest/geo/geo_place.json")
    }

    "perform a reverse geocode search" in new TwitterGeoClientSpecContext {
      val result: RatedData[GeoSearch] = when(reverseGeocode(-122.42284884, 37.76893497))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === s"https://api.twitter.com/1.1/geo/reverse_geocode.json"
          request.uri.rawQueryString === Some("accuracy=0m&granularity=neighborhood&lat=-122.42284884&long=37.76893497")
        }
        .respondWithRated("/twitter/rest/geo/reverse_geocode.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[GeoSearch]("/fixtures/rest/geo/reverse_geocode.json")
    }

    "search a geo place" in new TwitterGeoClientSpecContext {
      val result: RatedData[GeoSearch] = when(searchGeoPlace("Creazzo"))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === s"https://api.twitter.com/1.1/geo/search.json"
          request.uri.rawQueryString === Some("query=Creazzo")
        }
        .respondWithRated("/twitter/rest/geo/search.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[GeoSearch]("/fixtures/rest/geo/search.json")
    }

    "perform an advanced search of a geo place" in new TwitterGeoClientSpecContext {
      val result: RatedData[GeoSearch] =
        when(advancedSearchGeoPlace(query = Some("Creazzo"), street_address = Some("Via Giotto 20")))
          .expectRequest { request =>
            request.method === HttpMethods.GET
            request.uri.endpoint === s"https://api.twitter.com/1.1/geo/search.json"
            request.uri.rawQueryString === Some("attribute:street_address=Via%20Giotto%2020&query=Creazzo")
          }
          .respondWithRated("/twitter/rest/geo/advanced_search.json")
          .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[GeoSearch]("/fixtures/rest/geo/advanced_search.json")
    }

    "reject advanced search if no latitude or longitude or ip or query have been provided" in new TwitterGeoClientSpecContext {
      val msg =
        "requirement failed: please, provide at least one of the following: 'latitude', 'longitude', 'query', 'ip'"
      advancedSearchGeoPlace(street_address = Some("Via Giotto 20")) must throwA[IllegalArgumentException](msg)
    }

  }

} 
Example 11
Source File: TwitterHelpClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.rest.help

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities._
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterHelpClientSpec extends ClientSpec {

  class TwitterHelpClientSpecContext extends RestClientSpecContext with TwitterHelpClient

  "Twitter Help Client" should {

    "get twitter configuration" in new TwitterHelpClientSpecContext {
      val result: RatedData[Configuration] = when(configuration())
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/help/configuration.json"
        }
        .respondWithRated("/twitter/rest/help/configuration.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Configuration]("/fixtures/rest/help/configuration.json")
    }

    "get supported languages" in new TwitterHelpClientSpecContext {
      val result: RatedData[Seq[LanguageDetails]] = when(supportedLanguages())
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/help/languages.json"
        }
        .respondWithRated("/twitter/rest/help/languages.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Seq[LanguageDetails]]("/fixtures/rest/help/languages.json")
    }

    "get twitter privacy policy" in new TwitterHelpClientSpecContext {
      val result: RatedData[PrivacyPolicy] = when(privacyPolicy())
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/help/privacy.json"
        }
        .respondWithRated("/twitter/rest/help/privacy.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[PrivacyPolicy]("/fixtures/rest/help/privacy.json")
    }

    "get twitter terms of service" in new TwitterHelpClientSpecContext {
      val result: RatedData[TermsOfService] = when(termsOfService())
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/help/tos.json"
        }
        .respondWithRated("/twitter/rest/help/tos.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[TermsOfService]("/fixtures/rest/help/tos.json")
    }
  }
} 
Example 12
Source File: TwitterApplicationClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.rest.application

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities.enums.Resource
import com.danielasfregola.twitter4s.entities.{RateLimits, RatedData}
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterApplicationClientSpec extends ClientSpec {

  class TwitterApplicationClientSpecContext extends RestClientSpecContext with TwitterApplicationClient

  "Twitter Application Client" should {

    "get application rate limits for some resources" in new TwitterApplicationClientSpecContext {
      val result: RatedData[RateLimits] = when(rateLimits(Resource.Account, Resource.Statuses))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/application/rate_limit_status.json"
          request.uri.rawQueryString === Some("resources=account%2Cstatuses")
        }
        .respondWithRated("/twitter/rest/application/rate_limits.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[RateLimits]("/fixtures/rest/application/rate_limits.json")
    }

    "get application rate limits for all the resources" in new TwitterApplicationClientSpecContext {
      val result: RatedData[RateLimits] = when(rateLimits())
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/application/rate_limit_status.json"
          request.uri.rawQueryString === None
        }
        .respondWithRated("/twitter/rest/application/rate_limits.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[RateLimits]("/fixtures/rest/application/rate_limits.json")
    }
  }
} 
Example 13
Source File: ExampleBank.scala    From affinity   with Apache License 2.0 5 votes vote down vote up
package io.amient.affinity.example

import akka.http.scaladsl.model.HttpMethods
import akka.util.Timeout
import io.amient.affinity.avro.record.{AvroRecord, Fixed}
import io.amient.affinity.core.ack
import io.amient.affinity.core.actor.{GatewayHttp, GatewayStream, Partition, Routed}
import io.amient.affinity.core.http.RequestMatchers.{HTTP, INT, PATH, QUERY}
import io.amient.affinity.core.storage.Record
import io.amient.affinity.core.util._

import scala.concurrent.duration._
import scala.language.postfixOps

case class Account(sortcode: String, number: Int) extends AvroRecord
case class Transaction(id: Long, amount: Double, timestamp: Long) extends AvroRecord with EventTime {
  override def eventTimeUnix() = timestamp
}


class ExampleBank extends GatewayStream with GatewayHttp {

  implicit val executor = context.dispatcher
  implicit val scheduler = context.system.scheduler
  implicit val timeout = Timeout(5 seconds)

  val defaultKeyspace = keyspace("default")

  input[Account, Transaction]("input-stream") { record: Record[Account, Transaction] =>
    defaultKeyspace ?! StoreTransaction(record.key, record.value)
  }

  override def handle: Receive = {
    case HTTP(HttpMethods.GET, PATH("transactions", sortcode, INT(number)), _, response) =>
      defaultKeyspace ?! GetAccountTransactions(Account(sortcode, number)) map (handleAsJson(response, _))

    case HTTP(HttpMethods.GET, PATH("transactions", sortcode), QUERY(("before", before)), response) =>
      defaultKeyspace ?? GetBranchTransactions(sortcode, EventTime.unix(before+"T00:00:00+00:00")) map (handleAsJson(response, _))

    case HTTP(HttpMethods.GET, PATH("transactions", sortcode), _, response) =>
      defaultKeyspace ?? GetBranchTransactions(sortcode) map (handleAsJson(response, _))
  }

}


case class StoreTransaction(key: Account, t: Transaction) extends AvroRecord with Routed with Reply[Option[Transaction]]
case class StorageKey(@Fixed(8) sortcode: String, @Fixed account: Int, txn: Long) extends AvroRecord
case class GetAccountTransactions(key: Account) extends AvroRecord with Routed with Reply[Seq[Transaction]]
case class GetBranchTransactions(sortcode: String, beforeUnixTs: Long = Long.MaxValue) extends AvroRecord with ScatterIterable[Transaction]

class DefaultPartition extends Partition {

  val transactions = state[StorageKey, Transaction]("transactions")

  implicit val executor = context.dispatcher

  override def handle: Receive = {

    case request@StoreTransaction(Account(sortcode, number), transaction) => request(sender) ! {
      transactions.replace(StorageKey(sortcode, number, transaction.id), transaction)
    } map {
      _ => context.system.eventStream.publish(request) //this is only to have determinist way of testing all data was processed
    }

    case request@GetBranchTransactions(sortcode, before) =>
      request(sender) ! transactions.range(TimeRange.until(before), sortcode).values.toList

    case request@GetAccountTransactions(account) => request(sender) ! {
      transactions.range(TimeRange.UNBOUNDED, account.sortcode, account.number).values.toList
    }
  }
} 
Example 14
Source File: TwitterSavedSearchClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.rest.savedsearches

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities.{RatedData, SavedSearch}
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterSavedSearchClientSpec extends ClientSpec {

  class TwitterSavedSearchClientSpecContext extends RestClientSpecContext with TwitterSavedSearchClient

  "Twitter Saved Search Client" should {

    "get saved searches" in new TwitterSavedSearchClientSpecContext {
      val result: RatedData[Seq[SavedSearch]] = when(savedSearches)
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/saved_searches/list.json"
        }
        .respondWithRated("/twitter/rest/savedsearches/list.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Seq[SavedSearch]]("/fixtures/rest/savedsearches/list.json")
    }

    "save a search" in new TwitterSavedSearchClientSpecContext {
      val result: SavedSearch = when(saveSearch("#scala"))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/1.1/saved_searches/create.json"
          request.uri.rawQueryString === Some("query=%23scala")
        }
        .respondWith("/twitter/rest/savedsearches/create.json")
        .await
      result === loadJsonAs[SavedSearch]("/fixtures/rest/savedsearches/create.json")
    }

    "delete a saved search" in new TwitterSavedSearchClientSpecContext {
      val id = 9569704
      val result: SavedSearch = when(deleteSavedSearch(id))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === s"https://api.twitter.com/1.1/saved_searches/destroy/$id.json"
        }
        .respondWith("/twitter/rest/savedsearches/destroy.json")
        .await
      result === loadJsonAs[SavedSearch]("/fixtures/rest/savedsearches/destroy.json")
    }

    "get saved search by id" in new TwitterSavedSearchClientSpecContext {
      val id = 9569704
      val result: RatedData[SavedSearch] = when(savedSearch(id))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === s"https://api.twitter.com/1.1/saved_searches/show/$id.json"
        }
        .respondWithRated("/twitter/rest/savedsearches/show.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[SavedSearch]("/fixtures/rest/savedsearches/show.json")
    }
  }

} 
Example 15
Source File: TwitterFriendClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.rest.friends

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities.{RatedData, UserIds, UserStringifiedIds, Users}
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterFriendClientSpec extends ClientSpec {

  class TwitterFriendClientSpecContext extends RestClientSpecContext with TwitterFriendClient

  "Twitter Friend Client" should {

    "get friends ids of a specific user by id" in new TwitterFriendClientSpecContext {
      val result: RatedData[UserIds] = when(friendIdsForUserId(2911461333L))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/friends/ids.json"
          request.uri.rawQueryString === Some("count=5000&cursor=-1&stringify_ids=false&user_id=2911461333")
        }
        .respondWithRated("/twitter/rest/friends/friends_ids.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[UserIds]("/fixtures/rest/friends/friends_ids.json")
    }

    "get friends ids of a specific user by name" in new TwitterFriendClientSpecContext {
      val result: RatedData[UserIds] = when(friendIdsForUser("DanielaSfregola"))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/friends/ids.json"
          request.uri.rawQueryString === Some("count=5000&cursor=-1&screen_name=DanielaSfregola&stringify_ids=false")
        }
        .respondWithRated("/twitter/rest/friends/friends_ids.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[UserIds]("/fixtures/rest/friends/friends_ids.json")
    }

    "get friends stringified ids of a specific user by id" in new TwitterFriendClientSpecContext {
      val result: RatedData[UserStringifiedIds] = when(friendStringifiedIdsForUserId(2911461333L))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/friends/ids.json"
          request.uri.rawQueryString === Some("count=5000&cursor=-1&stringify_ids=true&user_id=2911461333")
        }
        .respondWithRated("/twitter/rest/friends/friends_ids_stringified.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[UserStringifiedIds]("/fixtures/rest/friends/friends_ids_stringified.json")
    }

    "get friends stringified ids of a specific user by name" in new TwitterFriendClientSpecContext {
      val result: RatedData[UserStringifiedIds] = when(friendStringifiedIdsForUser("DanielaSfregola"))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/friends/ids.json"
          request.uri.rawQueryString === Some("count=5000&cursor=-1&screen_name=DanielaSfregola&stringify_ids=true")
        }
        .respondWithRated("/twitter/rest/friends/friends_ids_stringified.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[UserStringifiedIds]("/fixtures/rest/friends/friends_ids_stringified.json")
    }

    "get friends of a user" in new TwitterFriendClientSpecContext {
      val result: RatedData[Users] = when(friendsForUser("DanielaSfregola", count = 10))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/friends/list.json"
          request.uri.rawQueryString === Some(
            "count=10&cursor=-1&include_user_entities=true&screen_name=DanielaSfregola&skip_status=false")
        }
        .respondWithRated("/twitter/rest/friends/users.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Users]("/fixtures/rest/friends/users.json")
    }

    "get friends of a user by user id" in new TwitterFriendClientSpecContext {
      val result: RatedData[Users] = when(friendsForUserId(2911461333L, count = 10))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/friends/list.json"
          request.uri.rawQueryString === Some(
            "count=10&cursor=-1&include_user_entities=true&skip_status=false&user_id=2911461333")
        }
        .respondWithRated("/twitter/rest/friends/users.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Users]("/fixtures/rest/friends/users.json")
    }

  }

} 
Example 16
Source File: TwitterSearchClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.rest.search

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities.{RatedData, StatusSearch}
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterSearchClientSpec extends ClientSpec {

  class TwitterSearchClientSpecContext extends RestClientSpecContext with TwitterSearchClient

  "Twitter Search Client" should {

    "search for tweets" in new TwitterSearchClientSpecContext {
      val result: RatedData[StatusSearch] = when(searchTweet("#scala"))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/search/tweets.json"
          request.uri.rawQueryString === Some("count=15&include_entities=true&q=%23scala&result_type=mixed")
        }
        .respondWithRated("/twitter/rest/search/tweets.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[StatusSearch]("/fixtures/rest/search/tweets.json")
    }

    "search for tweets with url in it" in new TwitterSearchClientSpecContext {
      val result: RatedData[StatusSearch] = when(searchTweet("https://github.com/ReactiveX/RxJava"))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/search/tweets.json"
          request.uri.rawQueryString === Some("count=15&include_entities=true" +
            "&q=https%3A%2F%2Fgithub.com%2FReactiveX%2FRxJava&result_type=mixed")
        }
        .respondWithRated("/twitter/rest/search/tweets.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[StatusSearch]("/fixtures/rest/search/tweets.json")
    }

    "search for tweets with * in it" in new TwitterSearchClientSpecContext {
      val result: RatedData[StatusSearch] = when(searchTweet("Test*Test"))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/search/tweets.json"
          request.uri.rawQueryString === Some("count=15&include_entities=true" +
            "&q=Test%2ATest&result_type=mixed")
        }
        .respondWithRated("/twitter/rest/search/tweets.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[StatusSearch]("/fixtures/rest/search/tweets.json")
    }

  }

} 
Example 17
Source File: TwitterBlockClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.rest.blocks

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities._
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterBlockClientSpec extends ClientSpec {

  class TwitterBlockClientSpecContext extends RestClientSpecContext with TwitterBlockClient

  "Twitter Block Client" should {

    "get blocked users" in new TwitterBlockClientSpecContext {
      val result: RatedData[Users] = when(blockedUsers())
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/blocks/list.json"
        }
        .respondWithRated("/twitter/rest/blocks/blocked_users.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Users]("/fixtures/rest/blocks/blocked_users.json")
    }

    "get blocked user ids" in new TwitterBlockClientSpecContext {
      val result: RatedData[UserIds] = when(blockedUserIds())
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/blocks/ids.json"
          request.uri.rawQueryString === Some("cursor=-1&stringify_ids=false")
        }
        .respondWithRated("/twitter/rest/blocks/ids.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[UserIds]("/fixtures/rest/blocks/ids.json")
    }

    "get blocked user stringified ids" in new TwitterBlockClientSpecContext {
      val result: RatedData[UserStringifiedIds] = when(blockedUserStringifiedIds())
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/blocks/ids.json"
          request.uri.rawQueryString === Some("cursor=-1&stringify_ids=true")
        }
        .respondWithRated("/twitter/rest/blocks/stringified_ids.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[UserStringifiedIds]("/fixtures/rest/blocks/stringified_ids.json")
    }

    "block user" in new TwitterBlockClientSpecContext {
      val result: User = when(blockUser("marcobonzanini"))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/1.1/blocks/create.json"
          request.uri.rawQueryString === Some("include_entities=true&screen_name=marcobonzanini&skip_status=false")
        }
        .respondWith("/twitter/rest/blocks/user.json")
        .await
      result === loadJsonAs[User]("/fixtures/rest/blocks/user.json")
    }

    "block user by user id" in new TwitterBlockClientSpecContext {
      val result: User = when(blockUserId(19018614L))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/1.1/blocks/create.json"
          request.uri.rawQueryString === Some("include_entities=true&skip_status=false&user_id=19018614")
        }
        .respondWith("/twitter/rest/blocks/user.json")
        .await
      result === loadJsonAs[User]("/fixtures/rest/blocks/user.json")
    }

    "unblock user" in new TwitterBlockClientSpecContext {
      val result: User = when(unblockUser("marcobonzanini"))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/1.1/blocks/destroy.json"
          request.uri.rawQueryString === Some("include_entities=true&screen_name=marcobonzanini&skip_status=false")
        }
        .respondWith("/twitter/rest/blocks/user.json")
        .await
      result === loadJsonAs[User]("/fixtures/rest/blocks/user.json")
    }

    "unblock user by user id" in new TwitterBlockClientSpecContext {
      val result: User = when(unblockUserId(19018614L))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/1.1/blocks/destroy.json"
          request.uri.rawQueryString === Some("include_entities=true&skip_status=false&user_id=19018614")
        }
        .respondWith("/twitter/rest/blocks/user.json")
        .await
      result === loadJsonAs[User]("/fixtures/rest/blocks/user.json")
    }
  }

} 
Example 18
Source File: TwitterMuteClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.rest.mutes

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities.{RatedData, User, UserIds, Users}
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterMuteClientSpec extends ClientSpec {

  class TwitterMuteClientSpecContext extends RestClientSpecContext with TwitterMuteClient

  "Twitter Mute Client" should {

    "mute a user" in new TwitterMuteClientSpecContext {
      val result: User = when(muteUser("marcobonzanini"))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/1.1/mutes/users/create.json"
          request.uri.rawQueryString === Some("screen_name=marcobonzanini")
        }
        .respondWith("/twitter/rest/mutes/user.json")
        .await
      result === loadJsonAs[User]("/fixtures/rest/mutes/user.json")
    }

    "mute a user by id" in new TwitterMuteClientSpecContext {
      val result: User = when(muteUserId(19018614L))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/1.1/mutes/users/create.json"
          request.uri.rawQueryString === Some("user_id=19018614")
        }
        .respondWith("/twitter/rest/mutes/user.json")
        .await
      result === loadJsonAs[User]("/fixtures/rest/mutes/user.json")
    }

    "unmute a user" in new TwitterMuteClientSpecContext {
      val result: User = when(unmuteUser("marcobonzanini"))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/1.1/mutes/users/destroy.json"
          request.uri.rawQueryString === Some("screen_name=marcobonzanini")
        }
        .respondWith("/twitter/rest/mutes/user.json")
        .await
      result === loadJsonAs[User]("/fixtures/rest/mutes/user.json")
    }

    "unmute a user by id" in new TwitterMuteClientSpecContext {
      val result: User = when(unmuteUserId(19018614L))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/1.1/mutes/users/destroy.json"
          request.uri.rawQueryString === Some("user_id=19018614")
        }
        .respondWith("/twitter/rest/mutes/user.json")
        .await
      result === loadJsonAs[User]("/fixtures/rest/mutes/user.json")
    }

    "get muted users ids" in new TwitterMuteClientSpecContext {
      val result: RatedData[UserIds] = when(mutedUserIds())
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/mutes/users/ids.json"
          request.uri.rawQueryString === Some("cursor=-1")
        }
        .respondWithRated("/twitter/rest/mutes/muted_users_ids.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[UserIds]("/fixtures/rest/mutes/muted_users_ids.json")
    }

    "get muted users" in new TwitterMuteClientSpecContext {
      val result: RatedData[Users] = when(mutedUsers())
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/mutes/users/list.json"
          request.uri.rawQueryString === Some("cursor=-1&include_entities=true&skip_status=false")
        }
        .respondWithRated("/twitter/rest/mutes/users.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Users]("/fixtures/rest/mutes/users.json")
    }
  }
} 
Example 19
Source File: TwitterSuggestionClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.rest.suggestions

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities.{Category, RatedData, Suggestions, User}
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterSuggestionClientSpec extends ClientSpec {

  class TwitterSuggestionClientSpecContext extends RestClientSpecContext with TwitterSuggestionClient

  "Twitter Suggestion Client" should {

    "get suggestions of a category" in new TwitterSuggestionClientSpecContext {
      val slug = "twitter"
      val result: RatedData[Suggestions] = when(suggestions(slug))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === s"https://api.twitter.com/1.1/users/suggestions/$slug.json"
          request.uri.rawQueryString === Some("lang=en")
        }
        .respondWithRated("/twitter/rest/suggestions/slug_suggestions.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Suggestions]("/fixtures/rest/suggestions/slug_suggestions.json")
    }

    "get suggested categories" in new TwitterSuggestionClientSpecContext {
      val result: RatedData[Seq[Category]] = when(suggestedCategories())
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/users/suggestions.json"
          request.uri.rawQueryString === Some("lang=en")
        }
        .respondWithRated("/twitter/rest/suggestions/categories.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Seq[Category]]("/fixtures/rest/suggestions/categories.json")
    }

    "get suggestions members" in new TwitterSuggestionClientSpecContext {
      val slug = "twitter"
      val result: RatedData[Seq[User]] = when(suggestionsMembers(slug))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === s"https://api.twitter.com/1.1/users/suggestions/$slug/members.json"
        }
        .respondWithRated("/twitter/rest/suggestions/suggestions_members.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Seq[User]]("/fixtures/rest/suggestions/suggestions_members.json")
    }
  }

} 
Example 20
Source File: TwitterFavoriteClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.rest.favorites

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities.{RatedData, Tweet}
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterFavoriteClientSpec extends ClientSpec {

  class TwitterFavoriteClientSpecContext extends RestClientSpecContext with TwitterFavoriteClient

  "Twitter Favorite Client" should {

    "get favorites" in new TwitterFavoriteClientSpecContext {
      val result: RatedData[Seq[Tweet]] = when(favoriteStatusesForUser("DanielaSfregola"))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/favorites/list.json"
          request.uri.rawQueryString === Some("count=20&include_entities=true&screen_name=DanielaSfregola")
        }
        .respondWithRated("/twitter/rest/favorites/favorites.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Seq[Tweet]]("/fixtures/rest/favorites/favorites.json")
    }

    "get favorites per user id" in new TwitterFavoriteClientSpecContext {
      val result: RatedData[Seq[Tweet]] = when(favoriteStatusesForUserId(19018614))
        .expectRequest { request =>
          request.method === HttpMethods.GET
          request.uri.endpoint === "https://api.twitter.com/1.1/favorites/list.json"
          request.uri.rawQueryString === Some("count=20&include_entities=true&user_id=19018614")
        }
        .respondWithRated("/twitter/rest/favorites/favorites.json")
        .await
      result.rate_limit === rateLimit
      result.data === loadJsonAs[Seq[Tweet]]("/fixtures/rest/favorites/favorites.json")
    }

    "favorite a tweet" in new TwitterFavoriteClientSpecContext {
      val result: Tweet = when(favoriteStatus(243138128959913986L))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/1.1/favorites/create.json"
          request.uri.rawQueryString === Some("id=243138128959913986&include_entities=true")
        }
        .respondWith("/twitter/rest/favorites/favorite.json")
        .await
      result === loadJsonAs[Tweet]("/fixtures/rest/favorites/favorite.json")
    }

    "unfavorite a tweet" in new TwitterFavoriteClientSpecContext {
      val result: Tweet = when(unfavoriteStatus(243138128959913986L))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://api.twitter.com/1.1/favorites/destroy.json"
          request.uri.rawQueryString === Some("id=243138128959913986&include_entities=true")
        }
        .respondWith("/twitter/rest/favorites/unfavorite.json")
        .await
      result === loadJsonAs[Tweet]("/fixtures/rest/favorites/unfavorite.json")
    }
  }

} 
Example 21
Source File: TwitterStatusClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.streaming.statuses

import akka.http.scaladsl.model.{HttpEntity, HttpMethods}
import com.danielasfregola.twitter4s.entities.enums.Language
import com.danielasfregola.twitter4s.entities.{Coordinate, GeoBoundingBox}
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterStatusClientSpec extends ClientSpec {

  class TwitterStatusClientSpecContext extends StreamingClientSpecContext with TwitterStatusClient

  "Twitter Status Streaming Client" should {

    "start a filtered status stream" in new TwitterStatusClientSpecContext {
      val locationsFilter = Seq(GeoBoundingBox(Coordinate(-122.75, 36.8), Coordinate(-121.75, 37.8)))
      val result: Unit =
        when(
          filterStatuses(follow = Seq(1L, 2L, 3L),
                         tracks = Seq("trending", "other"),
                         locations = locationsFilter,
                         languages = Seq(Language.Hungarian, Language.Bengali))(dummyProcessing))
      when(
        filterStatuses(follow = Seq(1L, 2L, 3L),
                       tracks = Seq("trending", "other"),
                       languages = Seq(Language.Hungarian, Language.Bengali))(dummyProcessing))
        .expectRequest { request =>
          request.method === HttpMethods.POST
          request.uri.endpoint === "https://stream.twitter.com/1.1/statuses/filter.json"
          request.entity === HttpEntity(
            `application/x-www-form-urlencoded`,
            "filter_level=none&follow=1%2C2%2C3&language=hu%2Cbn&locations=-122.75%2C36.8%2C-121.75%2C37.8&stall_warnings=false&track=trending%2Cother"
          )
        }
        .respondWithOk
        .await
      result.isInstanceOf[Unit] should beTrue
    }

    "start a sample status stream" in new TwitterStatusClientSpecContext {
      val result: Unit =
        when(sampleStatuses(languages = Seq(Language.English))(dummyProcessing))
          .expectRequest { request =>
            request.method === HttpMethods.GET
            request.uri.endpoint === "https://stream.twitter.com/1.1/statuses/sample.json"
            request.uri.rawQueryString === Some("filter_level=none&language=en&stall_warnings=false")
          }
          .respondWithOk
          .await
      result.isInstanceOf[Unit] should beTrue
    }

    "start a firehose status stream" in new TwitterStatusClientSpecContext {
      val result: Unit =
        when(firehoseStatuses(languages = Seq(Language.Hungarian, Language.Bengali))(dummyProcessing))
          .expectRequest { request =>
            request.method === HttpMethods.GET
            request.uri.endpoint === "https://stream.twitter.com/1.1/statuses/firehose.json"
            request.uri.rawQueryString === Some("language=hu%2Cbn&stall_warnings=false")
          }
          .respondWithOk
          .await
      result.isInstanceOf[Unit] should beTrue
    }
  }
} 
Example 22
Source File: TwitterUserClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.streaming.users

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities.enums.Language
import com.danielasfregola.twitter4s.entities.{Coordinate, GeoBoundingBox}
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterUserClientSpec extends ClientSpec {

  class TwitterUserClientSpecContext extends StreamingClientSpecContext with TwitterUserClient

  "Twitter User Streaming Client" should {

    "start a filtered user stream" in new TwitterUserClientSpecContext {
      val locationsFilter = Seq(
        GeoBoundingBox(Coordinate(-122.75, 36.8), Coordinate(-121.75, 37.8)),
        GeoBoundingBox(Coordinate(-74, 40), Coordinate(-73, 41))
      )
      val result: Unit =
        when(
          userEvents(tracks = Seq("trending"), languages = Seq(Language.English), locations = locationsFilter)(
            dummyProcessing))
          .expectRequest { request =>
            request.method === HttpMethods.GET
            request.uri.endpoint === "https://userstream.twitter.com/1.1/user.json"
            request.uri.rawQueryString === Some(
              "filter_level=none&language=en&locations=-122.75%2C36.8%2C-121.75%2C37.8%2C-74.0%2C40.0%2C-73.0%2C41.0&stall_warnings=false&stringify_friend_ids=false&track=trending&with=followings")
          }
          .respondWithOk
          .await
      result.isInstanceOf[Unit] should beTrue
    }
  }
} 
Example 23
Source File: TwitterSiteClientSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.clients.streaming.sites

import akka.http.scaladsl.model.HttpMethods
import com.danielasfregola.twitter4s.entities.enums.Language
import com.danielasfregola.twitter4s.helpers.ClientSpec

class TwitterSiteClientSpec extends ClientSpec {

  class TwitterSiteClientSpecContext extends StreamingClientSpecContext with TwitterSiteClient

  "Twitter Site Streaming Client" should {

    "start a filtered site stream" in new TwitterSiteClientSpecContext {
      val result: Unit =
        when(siteEvents(languages = Seq(Language.Italian))(dummyProcessing))
          .expectRequest { request =>
            request.method === HttpMethods.GET
            request.uri.endpoint === "https://sitestream.twitter.com/1.1/site.json"
            request.uri.rawQueryString === Some("language=it&stall_warnings=false&stringify_friend_ids=false&with=user")
          }
          .respondWithOk
          .await
      result.isInstanceOf[Unit] should beTrue
    }
  }
} 
Example 24
Source File: MessageProviderKafkaItTest.scala    From rokku   with Apache License 2.0 5 votes vote down vote up
package com.ing.wbaa.rokku.proxy.provider

import java.net.InetAddress

import akka.actor.ActorSystem
import akka.http.scaladsl.model.{HttpMethods, RemoteAddress}
import com.ing.wbaa.rokku.proxy.config.KafkaSettings
import com.ing.wbaa.rokku.proxy.data._
import com.ing.wbaa.rokku.proxy.handler.parsers.RequestParser.RequestTypeUnknown
import net.manub.embeddedkafka.{EmbeddedKafka, EmbeddedKafkaConfig}
import org.scalatest.RecoverMethods._
import org.scalatest.diagrams.Diagrams
import org.scalatest.wordspec.AnyWordSpecLike

import scala.concurrent.ExecutionContext

class MessageProviderKafkaItTest extends AnyWordSpecLike with Diagrams with EmbeddedKafka with MessageProviderKafka {

  implicit val testSystem: ActorSystem = ActorSystem("kafkaTest")

  private val testKafkaPort = 9093

  override implicit val kafkaSettings: KafkaSettings = new KafkaSettings(testSystem.settings.config) {
    override val bootstrapServers: String = s"localhost:$testKafkaPort"
  }

  override implicit val executionContext: ExecutionContext = testSystem.dispatcher

  implicit val requestId: RequestId = RequestId("test")

  val s3Request = S3Request(AwsRequestCredential(AwsAccessKey("a"), None), Some("demobucket"), Some("s3object"), Read())
    .copy(clientIPAddress = RemoteAddress(InetAddress.getByName("127.0.0.1")))

  "KafkaMessageProvider" should {
    "Send message to correct topic with Put or Post" in {
      implicit val config = EmbeddedKafkaConfig(kafkaPort = testKafkaPort)

      withRunningKafka {
        Thread.sleep(3000)
        val createEventsTopic = "create_events"
        createCustomTopic(createEventsTopic)
        emitEvent(s3Request, HttpMethods.PUT, "testUser", RequestTypeUnknown())
        val result = consumeFirstStringMessageFrom(createEventsTopic)
        assert(result.contains("s3:ObjectCreated:PUT"))
      }
    }

    "Send message to correct topic with Delete" in {
      implicit val config = EmbeddedKafkaConfig(kafkaPort = testKafkaPort)

      withRunningKafka {
        Thread.sleep(3000)
        val deleteEventsTopic = "delete_events"
        createCustomTopic(deleteEventsTopic)
        emitEvent(s3Request, HttpMethods.DELETE, "testUser", RequestTypeUnknown())
        assert(consumeFirstStringMessageFrom(deleteEventsTopic).contains("s3:ObjectRemoved:DELETE"))
      }
    }

    "fail on incomplete data" in {
      recoverToSucceededIf[Exception](emitEvent(s3Request.copy(s3Object = None), HttpMethods.PUT, "testUser", RequestTypeUnknown()))
    }
  }

} 
Example 25
Source File: HttpServiceIntegrationTest.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.http

import java.io.File
import java.nio.file.Files

import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpMethods, HttpRequest, StatusCodes, Uri}
import com.daml.http.Statement.discard
import com.daml.http.util.TestUtil.writeToFile
import org.scalacheck.Gen
import org.scalatest.{Assertion, BeforeAndAfterAll}

import scala.concurrent.Future

class HttpServiceIntegrationTest extends AbstractHttpServiceIntegrationTest with BeforeAndAfterAll {

  private val staticContent: String = "static"

  private val staticContentDir: File =
    Files.createTempDirectory("integration-test-static-content").toFile

  override def staticContentConfig: Option[StaticContentConfig] =
    Some(StaticContentConfig(prefix = staticContent, directory = staticContentDir))

  override def jdbcConfig: Option[JdbcConfig] = None

  private val expectedDummyContent: String = Gen
    .listOfN(100, Gen.identifier)
    .map(_.mkString(" "))
    .sample
    .getOrElse(throw new IllegalStateException(s"Cannot create dummy text content"))

  private val dummyFile: File =
    writeToFile(new File(staticContentDir, "dummy.txt"), expectedDummyContent).get
  require(dummyFile.exists)

  override protected def afterAll(): Unit = {
    // clean up temp directory
    discard { dummyFile.delete() }
    discard { staticContentDir.delete() }
    super.afterAll()
  }

  "should serve static content from configured directory" in withHttpService { (uri: Uri, _, _) =>
    Http()
      .singleRequest(
        HttpRequest(
          method = HttpMethods.GET,
          uri = uri.withPath(Uri.Path(s"/$staticContent/${dummyFile.getName}"))))
      .flatMap { resp =>
        discard { resp.status shouldBe StatusCodes.OK }
        val bodyF: Future[String] = getResponseDataBytes(resp, debug = false)
        bodyF.flatMap { body =>
          body shouldBe expectedDummyContent
        }
      }: Future[Assertion]
  }

  "Forwarded" - {
    import Endpoints.Forwarded
    "can 'parse' sample" in {
      Forwarded("for=192.168.0.1;proto=http;by=192.168.0.42").proto should ===(Some("http"))
    }

    "can 'parse' quoted sample" in {
      Forwarded("for=192.168.0.1;proto = \"https\" ;by=192.168.0.42").proto should ===(
        Some("https"))
    }
  }
} 
Example 26
Source File: Registry.scala    From kanadi   with MIT License 5 votes vote down vote up
package org.zalando.kanadi.api

import java.net.URI

import defaults._
import akka.http.scaladsl.HttpExt
import akka.http.scaladsl.model.headers.RawHeader
import akka.http.scaladsl.model.{ContentTypes, HttpMethods, HttpRequest, Uri}
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.Materializer
import com.typesafe.scalalogging.{Logger, LoggerTakingImplicit}
import de.heikoseeberger.akkahttpcirce.ErrorAccumulatingCirceSupport._
import org.mdedetrich.webmodels.{FlowId, OAuth2TokenProvider}
import org.mdedetrich.webmodels.RequestHeaders.`X-Flow-ID`
import org.zalando.kanadi.models._

import scala.concurrent.{ExecutionContext, Future}

case class Registry(baseUri: URI, oAuth2TokenProvider: Option[OAuth2TokenProvider] = None)(implicit
                                                                                           kanadiHttpConfig: HttpConfig,
                                                                                           http: HttpExt,
                                                                                           materializer: Materializer)
    extends RegistryInterface {
  protected val logger: LoggerTakingImplicit[FlowId] = Logger.takingImplicit[FlowId](classOf[Registry])
  private val baseUri_                               = Uri(baseUri.toString)

  
  def partitionStrategies(implicit flowId: FlowId = randomFlowId(),
                          executionContext: ExecutionContext): Future[List[PartitionStrategy]] = {
    val uri =
      baseUri_.withPath(baseUri_.path / "registry" / "partition-strategies")

    val baseHeaders = List(RawHeader(`X-Flow-ID`, flowId.value))

    for {
      headers <- oAuth2TokenProvider match {
                  case None => Future.successful(baseHeaders)
                  case Some(futureProvider) =>
                    futureProvider.value().map { oAuth2Token =>
                      toHeader(oAuth2Token) +: baseHeaders
                    }
                }
      request  = HttpRequest(HttpMethods.GET, uri, headers)
      _        = logger.debug(request.toString)
      response <- http.singleRequest(request)
      result <- {
        if (response.status.isSuccess()) {
          Unmarshal(response.entity.httpEntity.withContentType(ContentTypes.`application/json`))
            .to[List[PartitionStrategy]]
        } else
          processNotSuccessful(request, response)
      }
    } yield result
  }

} 
Example 27
Source File: HttpRequestFactory.scala    From hydra   with Apache License 2.0 5 votes vote down vote up
package hydra.ingest.http

import akka.http.scaladsl.model.{HttpMethods, HttpRequest}
import akka.http.scaladsl.server.directives.CodingDirectives
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.Materializer
import hydra.core.ingest.RequestParams._
import hydra.core.ingest._
import hydra.core.transport.{AckStrategy, ValidationStrategy}

import scala.concurrent.Future
import scala.util.Success


class HttpRequestFactory
    extends RequestFactory[HttpRequest]
    with CodingDirectives {

  override def createRequest(correlationId: String, request: HttpRequest)(
      implicit mat: Materializer
  ): Future[HydraRequest] = {
    implicit val ec = mat.executionContext

    lazy val vs = request.headers
      .find(_.lowercaseName() == HYDRA_VALIDATION_STRATEGY)
      .map(h => ValidationStrategy(h.value()))
      .getOrElse(ValidationStrategy.Strict)

    lazy val as = request.headers
      .find(_.lowercaseName() == HYDRA_ACK_STRATEGY)
      .map(h => AckStrategy(h.value()))
      .getOrElse(Success(AckStrategy.NoAck))

    lazy val clientId = request.headers
      .find(_.lowercaseName() == HydraClientId)
      .map(_.value().toLowerCase)

    Unmarshal(request.entity).to[String].flatMap { payload =>
      val dPayload = if (request.method == HttpMethods.DELETE) null else payload
      val metadata: Map[String, String] =
        request.headers.map(h => h.name.toLowerCase -> h.value).toMap
      Future
        .fromTry(as)
        .map(ack =>
          HydraRequest(correlationId, dPayload, clientId, metadata, vs, ack)
        )
    }
  }
} 
Example 28
Source File: CorsSupportSpec.scala    From hydra   with Apache License 2.0 5 votes vote down vote up
package hydra.core.http

import akka.http.scaladsl.model.HttpMethods
import ch.megard.akka.http.cors.scaladsl.model.HttpOriginMatcher
import org.scalatest.matchers.should.Matchers
import org.scalatest.funspec.AnyFunSpecLike

import scala.collection.immutable

class CorsSupportSpec extends Matchers with AnyFunSpecLike with CorsSupport {

  describe("Cors Support") {
    it("has sensible defaults") {
      settings.allowCredentials shouldBe false
      settings.exposedHeaders shouldBe immutable.Seq("Link")
      settings.allowedMethods shouldBe Seq(
        HttpMethods.GET,
        HttpMethods.POST,
        HttpMethods.HEAD,
        HttpMethods.OPTIONS
      )
      settings.allowedOrigins shouldBe HttpOriginMatcher.*
      settings.maxAge shouldBe Some(1800)
      settings.allowGenericHttpRequests shouldBe true
    }
  }

} 
Example 29
Source File: GitHubSpec.scala    From akka-api-gateway-example   with MIT License 5 votes vote down vote up
package jp.co.dzl.example.akka.api.service

import akka.actor.ActorSystem
import akka.http.scaladsl.model.headers.RawHeader
import akka.http.scaladsl.model.{ HttpMethods, HttpRequest, HttpResponse }
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{ Flow, Source }
import akka.stream.testkit.scaladsl.TestSink
import org.scalamock.scalatest.MockFactory
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{ BeforeAndAfterAll, FlatSpec, Matchers }

import scala.concurrent.Await
import scala.concurrent.duration.Duration

class GitHubSpec extends FlatSpec with Matchers with ScalaFutures with BeforeAndAfterAll with MockFactory {
  implicit val system = ActorSystem("github-spec")
  implicit val executor = system.dispatcher
  implicit val materializer = ActorMaterializer()

  override protected def afterAll: Unit = {
    Await.result(system.terminate(), Duration.Inf)
  }

  "#from" should "merge original headers to github request" in {
    val github = new GitHubImpl("127.0.0.1", 8000, 5, mock[HttpClient])
    val request = HttpRequest(HttpMethods.GET, "/")
      .addHeader(RawHeader("host", "dummy"))
      .addHeader(RawHeader("timeout-access", "dummy"))

    val result = Source.single(HttpRequest(HttpMethods.GET, "/v1/github/users/xxxxxx"))
      .via(github.from(request))
      .runWith(TestSink.probe[HttpRequest])
      .request(1)
      .expectNext()

    result.headers.filter(_.lowercaseName() == "host") shouldBe empty
    result.headers.filter(_.lowercaseName() == "timeout-access") shouldBe empty
    result.headers.filter(_.lowercaseName() == "x-forwarded-host") shouldNot be(empty)
  }

  "#send" should "connect using http client" in {
    val httpResponse = HttpResponse()
    val httpClient = mock[HttpClient]
    (httpClient.connectionHttps _).expects(*, *, *).returning(Flow[HttpRequest].map(_ => httpResponse))

    val github = new GitHubImpl("127.0.0.1", 8000, 5, httpClient)
    val result = Source.single(HttpRequest(HttpMethods.GET, "/"))
      .via(github.send)
      .runWith(TestSink.probe[HttpResponse])
      .request(1)
      .expectNext()

    result shouldBe httpResponse
  }
} 
Example 30
Source File: ClickhouseQueryBuilder.scala    From clickhouse-scala-client   with GNU Lesser General Public License v3.0 5 votes vote down vote up
package com.crobox.clickhouse.internal

import akka.http.scaladsl.model.Uri.Query
import akka.http.scaladsl.model.headers.{HttpEncodingRange, RawHeader}
import akka.http.scaladsl.model.{HttpMethods, HttpRequest, RequestEntity, Uri}
import com.crobox.clickhouse.internal.QuerySettings.ReadQueries
import com.crobox.clickhouse.internal.progress.ProgressHeadersAsEventsStage
import com.typesafe.config.Config
import com.typesafe.scalalogging.LazyLogging

import scala.collection.immutable

private[clickhouse] trait ClickhouseQueryBuilder extends LazyLogging {

  private val Headers = {
    import HttpEncodingRange.apply
    import akka.http.scaladsl.model.headers.HttpEncodings.{deflate, gzip}
    import akka.http.scaladsl.model.headers.`Accept-Encoding`
    immutable.Seq(`Accept-Encoding`(gzip, deflate))
  }
  private val MaxUriSize = 16 * 1024

  protected def toRequest(uri: Uri,
                          query: String,
                          queryIdentifier: Option[String],
                          settings: QuerySettings,
                          entity: Option[RequestEntity])(config: Config): HttpRequest = {
    val urlQuery = uri.withQuery(Query(Query("query" -> query) ++ settings.withFallback(config).asQueryParams: _*))
    entity match {
      case Some(e) =>
        logger.debug(s"Executing clickhouse query [$query] on host [${uri
          .toString()}] with entity payload of length ${e.contentLengthOption}")
        HttpRequest(
          method = HttpMethods.POST,
          uri = urlQuery,
          entity = e,
          headers = Headers ++ queryIdentifier.map(RawHeader(ProgressHeadersAsEventsStage.InternalQueryIdentifier, _))
        )
      case None
          if settings.idempotent.contains(true) && settings.readOnly == ReadQueries && urlQuery
            .toString()
            .getBytes
            .length < MaxUriSize => //max url size
        logger.debug(s"Executing clickhouse idempotent query [$query] on host [${uri.toString()}]")
        HttpRequest(
          method = HttpMethods.GET,
          uri = urlQuery.withQuery(
            urlQuery
              .query()
              .filterNot(
                _._1 == "readonly"
              ) //get requests are readonly by default, if we send the readonly flag clickhouse will fail the request
          ),
          headers = Headers ++ queryIdentifier.map(RawHeader(ProgressHeadersAsEventsStage.InternalQueryIdentifier, _))
        )
      case None =>
        logger.debug(s"Executing clickhouse query [$query] on host [${uri.toString()}]")
        HttpRequest(
          method = HttpMethods.POST,
          uri = uri.withQuery(settings.withFallback(config).asQueryParams),
          entity = query,
          headers = Headers ++ queryIdentifier.map(RawHeader(ProgressHeadersAsEventsStage.InternalQueryIdentifier, _))
        )
    }
  }

} 
Example 31
Source File: MetricsFactory.scala    From rokku   with Apache License 2.0 5 votes vote down vote up
package com.ing.wbaa.rokku.proxy.metrics

import akka.http.scaladsl.model.{ HttpMethod, HttpMethods }
import com.codahale.metrics.{ JmxReporter, MetricRegistry }

object MetricsFactory {

  val ALL_REQUEST = "requests.status.all.total"
  val SUCCESS_REQUEST = "requests.status.success.total"
  val FAILURE_REQUEST = "requests.status.failure.total"
  val UNAUTHENTICATED_REQUEST = "requests.status.unauthenticated.total"
  val REQUEST_TIME = "requests.nanoseconds.total"
  val REQUEST_TIME_HIST = "requests.time.histogram"
  val HTTP_METHOD = "{httpMethod}"
  val HTTP_DIRECTION = "{InOut}"
  val REQUEST_CONTEXT_LENGTH = s"requests.method.$HTTP_METHOD.$HTTP_DIRECTION.context.length.bytes"
  val REQUEST_CONTEXT_LENGTH_SUM = s"requests.method.$HTTP_METHOD.$HTTP_DIRECTION.context.length.bytes.total"
  val REQUEST_QUEUE_OCCUPIED = "request.queue.occupied"
  val REQUEST_USER = "{user}"
  val REQUEST_QUEUE_OCCUPIED_BY_USER = s"request.queue.occupied.by.$REQUEST_USER"
  val ERROR_REPORTED_TOTAL = "errors.reported.total"
  val OBJECTS_UPLOAD_OPERATIONS_TOTAL = s"requests.method.$HTTP_METHOD.operations.total"
  val KAFKA_SENT_NOTIFICATION_TOTAL = "requests.kafka.notification.sent.total"
  val KAFKA_SENT_NOTIFICATION_ERROR_TOTAL = "requests.kafka.notification.sent.errors.total"

  private[this] val metrics = new MetricRegistry()

  JmxReporter.forRegistry(metrics).inDomain("rokku").build.start()

  def registryMetrics(): MetricRegistry = metrics

  def countRequest(name: String, count: Long = 1, countAll: Boolean = true): Unit = {
    metrics.counter(name).inc(count)
    if (countAll) metrics.counter(ALL_REQUEST).inc()
  }

  def markRequestTime(time: Long): Unit = {
    metrics.counter(REQUEST_TIME).inc(time)
    metrics.histogram(REQUEST_TIME_HIST).update(time)
  }

  def incrementRequestQueue(name: String): Unit = {
    metrics.counter(name).inc()
    metrics.counter(REQUEST_QUEUE_OCCUPIED).inc()
  }

  def decrementRequestQueue(name: String): Unit = {
    metrics.counter(name).dec()
    metrics.counter(REQUEST_QUEUE_OCCUPIED).dec()
  }

  def countLogErrors(name: String): Unit = {
    metrics.counter(name).inc()
  }

  def incrementObjectsUploaded(requestMethodName: HttpMethod): Unit = {
    requestMethodName match {
      case HttpMethods.PUT | HttpMethods.POST =>
        metrics.counter(OBJECTS_UPLOAD_OPERATIONS_TOTAL.replace(MetricsFactory.HTTP_METHOD, requestMethodName.value)).inc()
      case _ =>
    }
  }

  def incrementKafkaNotificationsSent(requestMethodName: HttpMethod): Unit = {
    requestMethodName match {
      case HttpMethods.PUT | HttpMethods.POST => metrics.counter(KAFKA_SENT_NOTIFICATION_TOTAL).inc()
      case _                                  =>
    }
  }

  def incrementKafkaSendErrors(): Unit = {
    metrics.counter(KAFKA_SENT_NOTIFICATION_ERROR_TOTAL).inc()
  }
} 
Example 32
Source File: HttpRequestConversionSupport.scala    From rokku   with Apache License 2.0 5 votes vote down vote up
package com.ing.wbaa.rokku.proxy.persistence.serializers

import java.net.InetAddress

import akka.http.scaladsl.model.HttpHeader.ParsingResult
import akka.http.scaladsl.model.{ HttpEntity, HttpHeader, HttpMethod, HttpMethods, HttpProtocol, HttpRequest, RemoteAddress, Uri }
import com.ing.wbaa.rokku.proxy.data.{ UserAssumeRole, UserRawJson }
import spray.json.DefaultJsonProtocol

import scala.collection.immutable

trait HttpRequestConversionSupport extends DefaultJsonProtocol {

  case class SimplifiedRemoteAddress(host: String) {
    def toRemoteAddr: RemoteAddress = {
      val a = host.split(":")
      RemoteAddress(InetAddress.getByName(a(0)), Some(a(1).toInt))
    }
  }

  case class SimplifiedHttpRequest(method: String, uri: String, headers: List[String], entity: String, httpProtocol: String)

  implicit val httpRequestF = jsonFormat5(SimplifiedHttpRequest)
  implicit val userRoleF = jsonFormat1(UserAssumeRole)
  implicit val userSTSF = jsonFormat5(UserRawJson)
  implicit val remoteAddressF = jsonFormat1(SimplifiedRemoteAddress)

  private[persistence] def convertAkkaHeadersToStrings(headers: Seq[HttpHeader]): List[String] = headers.map(h => s"${h.name()}=${h.value()}").toList

  private def convertStringsToAkkaHeaders(headers: List[String]): immutable.Seq[HttpHeader] = headers.map { p =>
    val kv = p.split("=")
    HttpHeader.parse(kv(0), kv(1)) match {
      case ParsingResult.Ok(header, _) => header
      case ParsingResult.Error(error)  => throw new Exception(s"Unable to convert to HttpHeader: ${error.summary}")
    }
  }

  private def httpMethodFrom(m: String): HttpMethod = m match {
    case "GET"    => HttpMethods.GET
    case "HEAD"   => HttpMethods.HEAD
    case "PUT"    => HttpMethods.PUT
    case "POST"   => HttpMethods.POST
    case "DELETE" => HttpMethods.DELETE
  }

  private[persistence] def toAkkaHttpRequest(s: SimplifiedHttpRequest): HttpRequest =
    HttpRequest(
      httpMethodFrom(s.method),
      Uri(s.uri),
      convertStringsToAkkaHeaders(s.headers),
      HttpEntity(s.entity),
      HttpProtocol(s.httpProtocol)
    )
} 
Example 33
Source File: MessageProviderKafka.scala    From rokku   with Apache License 2.0 5 votes vote down vote up
package com.ing.wbaa.rokku.proxy.provider

import akka.Done
import akka.http.scaladsl.model.{ HttpMethod, HttpMethods, StatusCodes }
import com.ing.wbaa.rokku.proxy.data.{ AWSMessageEventJsonSupport, RequestId, S3Request }
import com.ing.wbaa.rokku.proxy.handler.parsers.RequestParser.AWSRequestType
import com.ing.wbaa.rokku.proxy.provider.aws.{ s3ObjectCreated, s3ObjectRemoved }
import com.ing.wbaa.rokku.proxy.provider.kafka.EventProducer

import scala.concurrent.Future

trait MessageProviderKafka extends EventProducer with AWSMessageEventJsonSupport {

  def emitEvent(s3Request: S3Request, method: HttpMethod, principalId: String, awsRequest: AWSRequestType)(implicit id: RequestId): Future[Done] =
    method match {
      case HttpMethods.POST | HttpMethods.PUT =>
        prepareAWSMessage(s3Request, method, principalId, s3Request.userIps, s3ObjectCreated(method.value), id, StatusCodes.OK, awsRequest)
          .map(jse =>
            sendSingleMessage(jse.toString(), kafkaSettings.createEventsTopic, Some(method)))
          .getOrElse(Future(Done))

      case HttpMethods.DELETE =>
        prepareAWSMessage(s3Request, method, principalId, s3Request.userIps, s3ObjectRemoved(method.value), id, StatusCodes.OK, awsRequest)
          .map(jse =>
            sendSingleMessage(jse.toString(), kafkaSettings.deleteEventsTopic, Some(method)))
          .getOrElse(Future(Done))

      case _ => Future(Done)
    }

} 
Example 34
Source File: AuditLogProviderItTest.scala    From rokku   with Apache License 2.0 5 votes vote down vote up
package com.ing.wbaa.rokku.proxy.provider

import java.net.InetAddress

import akka.actor.ActorSystem
import akka.http.scaladsl.model.{HttpMethods, HttpRequest, RemoteAddress, StatusCodes}
import com.ing.wbaa.rokku.proxy.config.KafkaSettings
import com.ing.wbaa.rokku.proxy.data._
import com.ing.wbaa.rokku.proxy.handler.parsers.RequestParser.RequestTypeUnknown
import net.manub.embeddedkafka.{EmbeddedKafka, EmbeddedKafkaConfig}
import org.scalatest.diagrams.Diagrams
import org.scalatest.wordspec.AnyWordSpecLike

import scala.concurrent.ExecutionContext

class AuditLogProviderItTest extends AnyWordSpecLike with Diagrams with EmbeddedKafka with AuditLogProvider {

  implicit val testSystem: ActorSystem = ActorSystem("kafkaTest")

  private val testKafkaPort = 9093

  override def auditEnabled = true

  override implicit val kafkaSettings: KafkaSettings = new KafkaSettings(testSystem.settings.config) {
    override val bootstrapServers: String = s"localhost:$testKafkaPort"
  }

  override implicit val executionContext: ExecutionContext = testSystem.dispatcher

  implicit val requestId: RequestId = RequestId("test")

  val s3Request = S3Request(AwsRequestCredential(AwsAccessKey("a"), None), Some("demobucket"), Some("s3object"), Read())
    .copy(headerIPs = HeaderIPs(Some(RemoteAddress(InetAddress.getByName("127.0.0.1"))),
      Some(Seq(RemoteAddress(InetAddress.getByName("1.1.1.1")))),
      Some(RemoteAddress(InetAddress.getByName("2.2.2.2")))))

  "AuditLogProvider" should {
    "send audit" in {
      implicit val config = EmbeddedKafkaConfig(kafkaPort = testKafkaPort)

      withRunningKafka {
        Thread.sleep(3000)
        val createEventsTopic = "audit_events"
        createCustomTopic(createEventsTopic)
        auditLog(s3Request, HttpRequest(HttpMethods.PUT, "http://localhost", Nil), "testUser", RequestTypeUnknown(), StatusCodes.Processing)
        val result = consumeFirstStringMessageFrom(createEventsTopic)
        assert(result.contains("\"eventName\":\"PUT\""))
        assert(result.contains("\"sourceIPAddress\":\"ClientIp=unknown|X-Real-IP=127.0.0.1|X-Forwarded-For=1.1.1.1|Remote-Address=2.2.2.2\""))
        assert(result.contains("\"x-amz-request-id\":\"test\""))
        assert(result.contains("\"principalId\":\"testUser\""))
      }
    }
  }

} 
Example 35
Source File: TestResponseFromUnsupportedApis.scala    From ohara   with Apache License 2.0 5 votes vote down vote up
package oharastream.ohara.configurator

import java.util.concurrent.TimeUnit

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.model.{HttpMethod, HttpMethods, HttpRequest}
import akka.http.scaladsl.unmarshalling.Unmarshal
import oharastream.ohara.client.configurator.ErrorApi
import oharastream.ohara.common.rule.OharaTest
import oharastream.ohara.common.util.{CommonUtils, Releasable}
import org.junit.{After, Test}
import org.scalatest.matchers.should.Matchers._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}

class TestResponseFromUnsupportedApis extends OharaTest {
  private[this] val configurator = Configurator.builder.fake().build()

  private[this] implicit val actorSystem: ActorSystem = ActorSystem("Executor-TestResponseFromUnsupportedApis")

  private[this] val expectedMessage = oharastream.ohara.configurator.route.apiUrl

  private[this] def result[T](f: Future[T]): T = Await.result(f, Duration(20, TimeUnit.SECONDS))

  @Test
  def testGet(): Unit = sendRequest(HttpMethods.GET, CommonUtils.randomString()).apiUrl.get shouldBe expectedMessage

  @Test
  def testPut(): Unit = sendRequest(HttpMethods.PUT, CommonUtils.randomString()).apiUrl.get shouldBe expectedMessage

  @Test
  def testDelete(): Unit =
    sendRequest(HttpMethods.DELETE, CommonUtils.randomString()).apiUrl.get shouldBe expectedMessage

  @Test
  def testPost(): Unit = sendRequest(HttpMethods.POST, CommonUtils.randomString()).apiUrl.get shouldBe expectedMessage

  private[this] def sendRequest(method: HttpMethod, postfix: String): ErrorApi.Error =
    result(
      Http()
        .singleRequest(HttpRequest(method, s"http://${configurator.hostname}:${configurator.port}/$postfix"))
        .flatMap { response =>
          if (response.status.isSuccess()) Future.failed(new AssertionError())
          else Unmarshal(response.entity).to[ErrorApi.Error]
        }
    )

  @After
  def tearDown(): Unit = {
    Releasable.close(configurator)
    result(actorSystem.terminate())
  }
} 
Example 36
Source File: S3RequestSpec.scala    From rokku   with Apache License 2.0 5 votes vote down vote up
package com.ing.wbaa.rokku.proxy.data

import akka.http.scaladsl.model.{ HttpMethods, MediaTypes, RemoteAddress, Uri }
import org.scalatest.diagrams.Diagrams
import org.scalatest.flatspec.AnyFlatSpec

class S3RequestSpec extends AnyFlatSpec with Diagrams {

  val testCred = AwsRequestCredential(AwsAccessKey("ak"), Some(AwsSessionToken("st")))

  "S3Request" should "parse an S3 request from an http Path and Method" in {
    val result = S3Request(testCred, Uri.Path("/demobucket"), HttpMethods.GET, RemoteAddress.Unknown, HeaderIPs(), MediaTypes.`text/plain`)
    assert(result == S3Request(testCred, Some("/demobucket"), None, Read("GET")))
  }

  it should "parse an S3 request from an http Path with object and Method" in {
    val result = S3Request(testCred, Uri.Path("/demobucket/demoobject"), HttpMethods.GET, RemoteAddress.Unknown, HeaderIPs(), MediaTypes.`text/plain`)
    assert(result == S3Request(testCred, Some("/demobucket/demoobject"), Some("demoobject"), Read("GET")))
  }

  it should "parse an S3 request from an http Path with subfolder and Method" in {
    val result = S3Request(testCred, Uri.Path("/demobucket/subfolder1/"), HttpMethods.GET, RemoteAddress.Unknown, HeaderIPs(), MediaTypes.`text/plain`)
    assert(result == S3Request(testCred, Some("/demobucket/subfolder1/"), None, Read("GET")))
  }

  it should "parse none for bucket if path is only root" in {
    val result = S3Request(testCred, Uri.Path("/"), HttpMethods.GET, RemoteAddress.Unknown, HeaderIPs(), MediaTypes.`text/plain`)
    assert(result == S3Request(testCred, None, None, Read("GET")))
  }

  it should "parse none for bucket if path is empty" in {
    val result = S3Request(testCred, Uri.Path(""), HttpMethods.GET, RemoteAddress.Unknown, HeaderIPs(), MediaTypes.`text/plain`)
    assert(result == S3Request(testCred, None, None, Read("GET")))
  }

  it should "set access to write for anything but GET" in {
    val result = S3Request(testCred, Uri.Path("/demobucket"), HttpMethods.POST, RemoteAddress.Unknown, HeaderIPs(), MediaTypes.`text/plain`)
    assert(result == S3Request(testCred, Some("/demobucket"), None, Post("POST")))
  }

} 
Example 37
Source File: FilterRecursiveListBucketHandlerSpec.scala    From rokku   with Apache License 2.0 5 votes vote down vote up
package com.ing.wbaa.rokku.proxy.handler

import akka.NotUsed
import akka.actor.ActorSystem
import akka.http.scaladsl.model.{ HttpMethods, MediaTypes, RemoteAddress, Uri }
import akka.stream.scaladsl.{ Sink, Source }
import akka.stream.{ ActorMaterializer, Materializer }
import akka.util.ByteString
import com.ing.wbaa.rokku.proxy.data._
import org.scalatest.diagrams.Diagrams
import org.scalatest.wordspec.AsyncWordSpec

import scala.concurrent.ExecutionContext

class FilterRecursiveListBucketHandlerSpec extends AsyncWordSpec with Diagrams with FilterRecursiveListBucketHandler {

  implicit val system: ActorSystem = ActorSystem.create("test-system")
  override implicit val executionContext: ExecutionContext = system.dispatcher
  implicit val requestId: RequestId = RequestId("test")

  implicit def materializer: Materializer = ActorMaterializer()(system)

  def isUserAuthorizedForRequest(request: S3Request, user: User)(implicit id: RequestId): Boolean = {
    user match {
      case User(userName, _, _, _, _) if userName.value == "admin" => true
      case User(userName, _, _, _, _) if userName.value == "user1" =>
        request match {
          case S3Request(_, s3BucketPath, _, _, _, _, _) =>
            if (s3BucketPath.get.startsWith("/demobucket/user/user2")) false else true
        }
      case _ => true
    }
  }

  val listBucketXmlResponse: String = scala.io.Source.fromResource("listBucket.xml").mkString.stripMargin.trim

  val adminUser = User(UserRawJson("admin", Some(Set.empty[String]), "a", "s", None))
  val user1 = User(UserRawJson("user1", Some(Set.empty[String]), "a", "s", None))
  val s3Request = S3Request(AwsRequestCredential(AwsAccessKey(""), None), Uri.Path("/demobucket/user"), HttpMethods.GET, RemoteAddress.Unknown, HeaderIPs(), MediaTypes.`text/plain`)
  val data: Source[ByteString, NotUsed] = Source.single(ByteString.fromString(listBucketXmlResponse))

  "List bucket object response" should {
    "returns all objects to admin" in {
      data.via(filterRecursiveListObjects(adminUser, s3Request)).map(_.utf8String).runWith(Sink.seq).map(x => {
        assert(x.mkString.stripMargin.equals(listBucketXmlResponse))
      })
    }

    val filteredXml: String = scala.io.Source.fromResource("filteredListBucket.xml").mkString.stripMargin.trim
    "returns filtered object for user 1" in {
      data.via(filterRecursiveListObjects(user1, s3Request)).map(_.utf8String).runWith(Sink.seq).map(x => {
        assert(x.mkString.stripMargin.replaceAll("[\n\r\\s]", "")
          .equals(filteredXml.replaceAll("[\n\r\\s]", "")))
      })
    }
  }
} 
Example 38
Source File: CacheRulesV1Spec.scala    From rokku   with Apache License 2.0 5 votes vote down vote up
package com.ing.wbaa.rokku.proxy.cache

import akka.actor.ActorSystem
import akka.http.scaladsl.model.Uri.Path
import akka.http.scaladsl.model.{ HttpMethod, HttpMethods, HttpRequest, Uri }
import com.ing.wbaa.rokku.proxy.config.StorageS3Settings
import com.ing.wbaa.rokku.proxy.data.RequestId
import com.ing.wbaa.rokku.proxy.handler.parsers.RequestParser
import org.scalatest.diagrams.Diagrams
import org.scalatest.wordspec.AnyWordSpec

class CacheRulesV1Spec extends AnyWordSpec with Diagrams with CacheRulesV1 with RequestParser {

  private implicit val id = RequestId("testRequestId")

  val system: ActorSystem = ActorSystem.create("test-system")
  override val storageS3Settings: StorageS3Settings = new StorageS3Settings(system.settings.config) {
    override val storageS3Authority: Uri.Authority = Uri.Authority(Uri.Host("1.2.3.4"), 1234)
  }

  override def getMaxEligibleCacheObjectSizeInBytes(implicit id: RequestId): Long = 5242880L

  override def getEligibleCachePaths(implicit id: RequestId): Array[String] = "/home/,/test/".trim.split(",")

  override def getHeadEnabled(implicit id: RequestId): Boolean = true

  private val uri = Uri("http", Uri.Authority(Uri.Host("1.2.3.4")), Path(""), None, None)

  private val methods = Seq(HttpMethods.GET, HttpMethods.PUT, HttpMethods.POST, HttpMethods.DELETE, HttpMethods.HEAD)

  "Cache rules v1 set isEligibleToBeCached " should {

    methods.foreach { method =>
      testIsEligibleToBeCached(method, "/home/test", HttpRequest.apply(method = method, uri = uri.copy(path = Path("/home/test"))))
      testIsEligibleToBeCached(method, "/home2/test", HttpRequest.apply(method = method, uri = uri.copy(path = Path("/home2/test"))))
      testIsEligibleToBeCached(method, "/test/abc", HttpRequest.apply(method = method, uri = uri.copy(path = Path("/test/abc"))))
      testIsEligibleToBeCached(method, "/testtest/test", HttpRequest.apply(method = method, uri = uri.copy(path = Path("/testtest/test"))))
    }
  }

  private def testIsEligibleToBeCached(method: HttpMethod, path: String, request: HttpRequest): Unit = {
    method match {
      case HttpMethods.GET | HttpMethods.HEAD if storageS3Settings.eligibleCachePaths.exists(path.startsWith) =>
        s"for method=$method and path=$path to true" in {
          assert(isEligibleToBeCached(request))
        }
      case _ =>
        s"for method=$method and path=$path to false" in {
          assert(!isEligibleToBeCached(request))
        }
    }
  }

  "Cache rules v1 set isEligibleToBeInvalidated" should {

    methods.foreach { method =>
      val request = HttpRequest.apply(method = method, uri)
      method match {
        case HttpMethods.POST | HttpMethods.PUT | HttpMethods.DELETE =>
          s"for method=$method to true" in {
            assert(isEligibleToBeInvalidated(request))
          }
        case _ =>
          s"for method=$method to false" in {
            assert(!isEligibleToBeInvalidated(request))
          }
      }
    }
  }
} 
Example 39
Source File: Config.scala    From akka-http-oauth2-client   with Apache License 2.0 5 votes vote down vote up
package com.github.dakatsuka.akka.http.oauth2.client

import java.net.URI

import akka.http.scaladsl.model.{ HttpMethod, HttpMethods }

case class Config(
    clientId: String,
    clientSecret: String,
    site: URI,
    authorizeUrl: String = "/oauth/authorize",
    tokenUrl: String = "/oauth/token",
    tokenMethod: HttpMethod = HttpMethods.POST
) extends ConfigLike {
  def getHost: String = site.getHost
  def getPort: Int = site.getScheme match {
    case "http"  => if (site.getPort == -1) 80 else site.getPort
    case "https" => if (site.getPort == -1) 443 else site.getPort
  }
} 
Example 40
Source File: YARNComponentActor.scala    From openwhisk   with Apache License 2.0 5 votes vote down vote up
package org.apache.openwhisk.core.yarn

import akka.actor.{Actor, ActorSystem}
import akka.http.scaladsl.model.{HttpMethods, StatusCodes}
import akka.stream.ActorMaterializer
import org.apache.openwhisk.common.Logging
import org.apache.openwhisk.core.entity.ExecManifest.ImageName
import org.apache.openwhisk.core.yarn.YARNComponentActor.{CreateContainerAsync, RemoveContainer}
import spray.json.{JsArray, JsNumber, JsObject, JsString}

import scala.concurrent.ExecutionContext


object YARNComponentActor {
  case object CreateContainerAsync
  case class RemoveContainer(component_instance_name: String)
}

class YARNComponentActor(actorSystem: ActorSystem,
                         logging: Logging,
                         yarnConfig: YARNConfig,
                         serviceName: String,
                         imageName: ImageName)
    extends Actor {

  implicit val as: ActorSystem = actorSystem
  implicit val materializer: ActorMaterializer = ActorMaterializer()
  implicit val ec: ExecutionContext = actorSystem.dispatcher

  //Adding a container via the YARN REST API is actually done by flexing the component's container pool to a certain size.
  // This actor must track the current containerCount in order to make the correct scale-up request.
  var containerCount: Int = 0

  def receive: PartialFunction[Any, Unit] = {
    case CreateContainerAsync =>
      sender ! createContainerAsync

    case RemoveContainer(component_instance_name) =>
      sender ! removeContainer(component_instance_name)

    case input =>
      throw new IllegalArgumentException("Unknown input: " + input)
      sender ! false
  }

  def createContainerAsync(): Unit = {
    logging.info(this, s"Using YARN to create a container with image ${imageName.name}...")

    val body = JsObject("number_of_containers" -> JsNumber(containerCount + 1)).compactPrint
    val response = YARNRESTUtil.submitRequestWithAuth(
      yarnConfig.authType,
      HttpMethods.PUT,
      s"${yarnConfig.masterUrl}/app/v1/services/$serviceName/components/${imageName.name}",
      body)
    response match {
      case httpresponse(StatusCodes.OK, content) =>
        logging.info(this, s"Added container: ${imageName.name}. Response: $content")
        containerCount += 1

      case httpresponse(_, _) => YARNRESTUtil.handleYARNRESTError(logging)
    }
  }

  def removeContainer(component_instance_name: String): Unit = {
    logging.info(this, s"Removing ${imageName.name} container: $component_instance_name ")
    if (containerCount <= 0) {
      logging.warn(this, "Already at 0 containers")
    } else {
      val body = JsObject(
        "components" -> JsArray(
          JsObject(
            "name" -> JsString(imageName.name),
            "decommissioned_instances" -> JsArray(JsString(component_instance_name))))).compactPrint
      val response = YARNRESTUtil.submitRequestWithAuth(
        yarnConfig.authType,
        HttpMethods.PUT,
        s"${yarnConfig.masterUrl}/app/v1/services/$serviceName",
        body)
      response match {
        case httpresponse(StatusCodes.OK, content) =>
          logging.info(
            this,
            s"Successfully removed ${imageName.name} container: $component_instance_name. Response: $content")
          containerCount -= 1

        case httpresponse(_, _) => YARNRESTUtil.handleYARNRESTError(logging)
      }
    }
  }
} 
Example 41
Source File: Wsk.scala    From openwhisk   with Apache License 2.0 5 votes vote down vote up
package org.apache.openwhisk.standalone

import akka.Done
import akka.actor.ActorSystem
import akka.http.scaladsl.model.Uri.Query
import akka.http.scaladsl.model.headers.{Accept, Authorization, BasicHttpCredentials}
import akka.http.scaladsl.model.{HttpHeader, HttpMethods, MediaTypes, Uri}
import org.apache.openwhisk.core.database.PutException
import org.apache.openwhisk.http.PoolingRestClient
import spray.json._

import scala.concurrent.{ExecutionContext, Future}

class Wsk(host: String, port: Int, authKey: String)(implicit system: ActorSystem) extends DefaultJsonProtocol {
  import PoolingRestClient._
  private implicit val ec: ExecutionContext = system.dispatcher
  private val client = new PoolingRestClient("http", host, port, 10)
  private val baseHeaders: List[HttpHeader] = {
    val Array(username, password) = authKey.split(':')
    List(Authorization(BasicHttpCredentials(username, password)), Accept(MediaTypes.`application/json`))
  }

  def updatePgAction(name: String, content: String): Future[Done] = {
    val js = actionJson(name, content)
    val params = Map("overwrite" -> "true")
    val uri = Uri(s"/api/v1/namespaces/_/actions/$name").withQuery(Query(params))
    client.requestJson[JsObject](mkJsonRequest(HttpMethods.PUT, uri, js, baseHeaders)).map {
      case Right(_)     => Done
      case Left(status) => throw PutException(s"Error creating action $name " + status)
    }
  }

  private def actionJson(name: String, code: String) = {
    s"""{
      |    "namespace": "_",
      |    "name": "$name",
      |    "exec": {
      |        "kind": "nodejs:default",
      |        "code": ${quote(code)}
      |    },
      |    "annotations": [{
      |        "key": "provide-api-key",
      |        "value": true
      |    }, {
      |        "key": "web-export",
      |        "value": true
      |    }, {
      |        "key": "raw-http",
      |        "value": false
      |    }, {
      |        "key": "final",
      |        "value": true
      |    }],
      |    "parameters": [{
      |        "key": "__ignore_certs",
      |        "value": true
      |    }]
      |}""".stripMargin.parseJson.asJsObject
  }

  private def quote(code: String) = {
    JsString(code).compactPrint
  }
} 
Example 42
Source File: AkkaHttpBackend.scala    From drunk   with Apache License 2.0 5 votes vote down vote up
package com.github.jarlakxen.drunk.backend

import scala.collection.immutable
import scala.concurrent.{ExecutionContext, Future}
import akka.actor.ActorSystem
import akka.http.scaladsl.{Http, HttpExt}
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpHeader, HttpMethods, HttpRequest, Uri}
import akka.stream.ActorMaterializer

class AkkaHttpBackend private[AkkaHttpBackend] (
  uri: Uri,
  headers: immutable.Seq[HttpHeader],
  httpExt: HttpExt
)(override implicit val as: ActorSystem, override implicit val mat: ActorMaterializer)
    extends AkkaBackend {

  def send(body: String): Future[(Int, String)] = {
    implicit val ec: ExecutionContext = as.dispatcher

    val req = HttpRequest(HttpMethods.POST, uri, headers, HttpEntity(ContentTypes.`application/json`, body))

    val res = httpExt.singleRequest(req)

    res.flatMap { hr =>
      val code = hr.status.intValue()

      val charsetFromHeaders = encodingFromContentType(hr.entity.contentType.toString).getOrElse("utf-8")
      val decodedResponse = decodeResponse(hr)
      val stringBody = bodyToString(decodedResponse, charsetFromHeaders)

      if (code >= 200 && code < 300) {
        stringBody.map { body =>
          hr.discardEntityBytes()
          (code, body)
        }
      } else {
        stringBody.flatMap { body =>
          hr.discardEntityBytes()
          Future.failed(new RuntimeException(s"${uri.toString} return $code with body: $body"))
        }
      }
    }
  }

}

object AkkaHttpBackend {
  val ContentTypeHeader = "Content-Type"

  def apply(
    uri: Uri,
    headers: immutable.Seq[HttpHeader] = Nil,
    httpExt: Option[HttpExt] = None
  )(implicit as: ActorSystem, mat: ActorMaterializer): AkkaHttpBackend = {

    val http = httpExt.getOrElse { Http(as) }
    new AkkaHttpBackend(uri, headers, http)
  }
} 
Example 43
Source File: SumTypedEntitiesTestSuite.scala    From endpoints4s   with MIT License 5 votes vote down vote up
package endpoints4s.algebra.server

import akka.http.scaladsl.model.{ContentTypes, HttpMethods, HttpRequest}
import endpoints4s.algebra

trait SumTypedEntitiesTestSuite[
    T <: algebra.SumTypedEntitiesTestApi
] extends ServerTestBase[T] {

  "Sum typed route" should {

    "handle `text/plain` content-type requests" in {
      serveIdentityEndpoint(serverApi.sumTypedEndpoint) { port =>
        val request =
          HttpRequest(HttpMethods.POST, s"http://localhost:$port/user-or-name")
            .withEntity(ContentTypes.`text/plain(UTF-8)`, "Alice")
        whenReady(sendAndDecodeEntityAsText(request)) {
          case (response, entity) =>
            assert(response.status.intValue() == 200)
            assert(
              response.entity.contentType == ContentTypes.`text/plain(UTF-8)`
            )
            entity shouldEqual "Alice"
        }
        ()
      }
    }

    "handle `application/json` content-type requests" in {
      serveIdentityEndpoint(serverApi.sumTypedEndpoint) { port =>
        val request =
          HttpRequest(HttpMethods.POST, s"http://localhost:$port/user-or-name")
            .withEntity(
              ContentTypes.`application/json`,
              "{\"name\":\"Alice\",\"age\":42}"
            )
        whenReady(sendAndDecodeEntityAsText(request)) {
          case (response, entity) =>
            assert(response.status.intValue() == 200)
            assert(
              response.entity.contentType == ContentTypes.`application/json`
            )
            ujson.read(entity) shouldEqual ujson.Obj(
              "name" -> ujson.Str("Alice"),
              "age" -> ujson.Num(42)
            )
        }
        ()
      }
    }

    "handle `application/json` content-type requests with malformed bodies" in {
      serveIdentityEndpoint(serverApi.sumTypedEndpoint) { port =>
        val request =
          HttpRequest(HttpMethods.POST, s"http://localhost:$port/user-or-name")
            .withEntity(
              ContentTypes.`application/json`,
              "{\"name\":\"Alice\"}"
            )
        whenReady(sendAndDecodeEntityAsText(request)) {
          case (response, entity) =>
            assert(response.status.intValue() == 400)
        }
        ()
      }

    }

    "not handle `application/x-www-form-urlencoded` content-type requests" in {
      serveIdentityEndpoint(serverApi.sumTypedEndpoint) { port =>
        val request =
          HttpRequest(HttpMethods.POST, s"http://localhost:$port/user-or-name")
            .withEntity(
              ContentTypes.`application/x-www-form-urlencoded`,
              "name=Alice&age=42"
            )
        whenReady(sendAndDecodeEntityAsText(request)) {
          case (response, _) =>
            assert(response.status.intValue() == 415)
        }
        ()
      }
    }
  }
} 
Example 44
Source File: CounterTest.scala    From endpoints4s   with MIT License 5 votes vote down vote up
package quickstart

import java.net.ServerSocket

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpMethods, HttpRequest, StatusCodes}
import akka.http.scaladsl.server.Directives._
import org.scalatest.BeforeAndAfterAll

import scala.concurrent.Await
import scala.concurrent.duration.DurationInt
import org.scalatest.freespec.AsyncFreeSpec

class CounterTest extends AsyncFreeSpec with BeforeAndAfterAll {

  implicit val actorSystem: ActorSystem = ActorSystem()
  val routes = CounterServer.routes ~ DocumentationServer.routes
  val interface = "0.0.0.0"
  val port = findOpenPort()
  val server = Http().bindAndHandle(routes, interface, port)

  override protected def afterAll(): Unit = {
    Await.result(
      Await.result(server, 10.seconds).terminate(3.seconds),
      15.seconds
    )
    Await.result(actorSystem.terminate(), 5.seconds)
    super.afterAll()
  }

  "CounterServer" - {
    "Query counter value" in {
      for {
        response <- Http().singleRequest(
          HttpRequest(uri = uri("/current-value"))
        )
        entity <- response.entity.toStrict(1.second)
      } yield {
        assert(response.status == StatusCodes.OK)
        assert(entity.contentType == ContentTypes.`application/json`)
        assert(entity.data.utf8String == "{\"value\":0}")
      }
    }
    "Increment counter value" in {
      val request =
        HttpRequest(
          method = HttpMethods.POST,
          uri = uri("/increment"),
          entity = HttpEntity(ContentTypes.`application/json`, "{\"step\":1}")
        )
      for {
        response <- Http().singleRequest(request)
      } yield {
        assert(response.status == StatusCodes.OK)
      }
    }
    "Query API documentation" in {
      for {
        response <- Http().singleRequest(
          HttpRequest(uri = uri("/documentation.json"))
        )
        entity <- response.entity.toStrict(1.second)
      } yield {
        assert(response.status == StatusCodes.OK)
        assert(entity.contentType == ContentTypes.`application/json`)
      }
    }
  }

  def findOpenPort(): Int = {
    val socket = new ServerSocket(0)
    try socket.getLocalPort
    finally if (socket != null) socket.close()
  }

  def uri(suffix: String) = s"http://$interface:$port$suffix"

} 
Example 45
Source File: UnMarshalling.scala    From introduction-to-akkahttp   with Apache License 2.0 5 votes vote down vote up
package com.shashank.akkahttp.basic.routing

import akka.actor.ActorSystem
import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.model.{HttpMethods, HttpRequest, HttpResponse, MessageEntity}
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.{ActorMaterializer, Materializer}
import akka.util.ByteString

import scala.concurrent.Await
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import spray.json._


object UnMarshalling {

  def main(args: Array[String]) {

    implicit val sys = ActorSystem("IntroductionToAkkaHttp")
    implicit val mat:Materializer = ActorMaterializer()

    //type FromStringUnmarshaller[T] = Unmarshaller[String, T]
    val intFuture = Unmarshal("42").to[Int]
    val int = Await.result(intFuture, 1.second)
    println("int unmarshalling "+int)

    //type FromStringUnmarshaller[T] = Unmarshaller[String, T]
    val boolFuture = Unmarshal("off").to[Boolean]
    val bool = Await.result(boolFuture, 1.second)
    println("off unmarshalling "+bool)

    //type ToEntityMarshaller[T] = Marshaller[T, MessageEntity]
    val string = "Yeah"
    val entityFuture = Marshal(string).to[MessageEntity]
    val entity = Await.result(entityFuture, 1.second) // don't block in non-test code!
    println(entity)

    //type ToResponseMarshaller[T] = Marshaller[T, HttpResponse]
    val errorMsg = "Not found, pal!"
    val responseFuture = Marshal(404 -> errorMsg).to[HttpResponse]
    val response = Await.result(responseFuture, 1.second)
    println(response)


    //type FromEntityUnmarshaller[T] = Unmarshaller[HttpEntity, T]
    val jsonByteString = ByteString("""{"name":"Hello"}""")
    val httpRequest = HttpRequest(HttpMethods.POST, entity = jsonByteString)
    val jsonDataUnmarshalledFuture = Unmarshal(httpRequest).to[String]
    val jsonDataUnmarshalled = Await.result(jsonDataUnmarshalledFuture, 1.second)
    println(jsonDataUnmarshalled)

    sys.terminate()

  }

}