|
| 1 | +// Copyright (C) 2018 The Delphi Team. |
| 2 | +// See the LICENCE file distributed with this work for additional |
| 3 | +// information regarding copyright ownership. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package de.upb.cs.swt.delphi.webapi |
| 18 | + |
| 19 | +import akka.http.scaladsl.Http |
| 20 | +import akka.http.scaladsl.model.{HttpRequest, HttpResponse} |
| 21 | +import akka.http.scaladsl.unmarshalling.{Unmarshal, Unmarshaller} |
| 22 | +import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpec} |
| 23 | +import spray.json._ |
| 24 | + |
| 25 | +import scala.concurrent.duration._ |
| 26 | +import scala.concurrent.{Await, Future} |
| 27 | +import scala.util.{Failure, Success} |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Hariharan. |
| 31 | + */ |
| 32 | +class RequestLimitCheck extends WordSpec with Matchers with BeforeAndAfterAll with JsonSupport { |
| 33 | + val delphiRoutes = DelphiRoutes() |
| 34 | + val serverBinding: Future[Http.ServerBinding] = Http() |
| 35 | + .bindAndHandle(delphiRoutes, "localhost", 8085) |
| 36 | + |
| 37 | + override protected def beforeAll(): Unit = { |
| 38 | + serverBinding.onComplete { |
| 39 | + case Success(server) => |
| 40 | + println(s"Server started at http://${server.localAddress.getHostString}:${server.localAddress.getPort}/") |
| 41 | + case Failure(e) => |
| 42 | + e.printStackTrace() |
| 43 | + sys.exit(0) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + "Requests" should { |
| 49 | + "throttle when limit reached" in { |
| 50 | + def responseFuture: Future[HttpResponse] = Http() |
| 51 | + .singleRequest(HttpRequest(uri = "http://localhost:8085/version")) |
| 52 | + |
| 53 | + //Completing request limit |
| 54 | + for (i <- (1 to maxIndividualReq)) { |
| 55 | + Await.result(responseFuture, 1.second) |
| 56 | + } |
| 57 | + case class LimitMsg(msg: String) |
| 58 | + implicit val msgFormat = jsonFormat1(LimitMsg) |
| 59 | + |
| 60 | + val limitReachedFuture = responseFuture |
| 61 | + limitReachedFuture.onComplete { |
| 62 | + case Success(res) => { |
| 63 | + val msgPromise = Unmarshal(res.entity).to[LimitMsg] |
| 64 | + msgPromise.onComplete { |
| 65 | + case Success(limitMsg) => { |
| 66 | + assertResult("Request limit exceeded")(limitMsg.msg) |
| 67 | + } |
| 68 | + case Failure(exception) => { |
| 69 | + fail(exception) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + case Failure(exception) => { |
| 74 | + fail(exception) |
| 75 | + } |
| 76 | + } |
| 77 | + Await.result(system.terminate(), 5.seconds) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments