I have this function which uses Play WS services:
def executeRequest(urls: List[Url]): List[Future[String]] =
urls.map(url => {
WS.url(url.url).get().map(x => url.url + ": " + x.statusText )
})
Where Url is a case class so defined:
case class Url(id: Long, url: String)
I want my application to be resilient and sometimes the url value triggers an IllegalArgumentException if the url is not well formed. What I tried was this:
def executeRequest2(urls: List[Url]): List[Future[String]] =
urls.map(url => {
WS.url(url.url).get().map(x => url.url + ": " + x.statusText )
.recover({
case e: IllegalArgumentException => url.url + " is invalid"
})
})
This doesn't work, most probably because the exception is thrown inside the .get() and not inside the future execution (it's just a guess anyway).
What I ended up doing is this:
def executeRequest(urls: List[Url]): List[Future[String]] =
urls.map(url => {
try {
WS.url(url.url).get().map(x => x.statusText)
}
catch {
case e: IllegalArgumentException => future {
url.url + " is illegal."
}
}
})
This works, but I would like some nicer and more idiomatic way to handle this exception. Note that here I haven't added the recover to handle eventual problems inside the future execution which will make this piece of code even more illegible.
Try is your friend here.
import scala.util.Try
urls.map(url =>
Try(WS.url(url.url).get().map(x => x.statusText))
.getOrElse(Future.successful(url.url + " is illegal."))
)
This will replace all exceptions with the error message in the getOrElse. We can be a little more fine grained than that though. Perhaps we could keep the invalid URLs as successful Future[String]s, but convert all others to failed Futures.
urls.map(url =>
Try(WS.url(url.url).get().map(x => x.statusText))
.recover{case e: IllegalArgumentException => Future.successful(url.url + " is illegal.")}
.recover{case t: Throwable => Future.failed(t)}
.get
)
Also note that the use of future{ .. } is deprecated.
Related
The code I'm trying to test:
const utils = require('../utils/utils');
let imageBuffer;
try {
imageBuffer = await utils.retrieveImageFromURI(params)
console.log(imageBuffer) // comes back as undefined when I mock the utils.retreieveImageFromURI
if (!imageBuffer || imageBuffer.length < 1024) {
throw new Error(`Retrieve from uri (${params.camera.ingest.uri}) was less than 1kb in size - indicating an error`)
}
console.log(`${params.camera.camId} - Successful Ingestion from URI`);
} catch (err) {
reject({ 'Task': `Attempting to pull image from camera (${params.camera.camId}) at ${params.camera.ingest.uri}`, 'Error': err.message, 'Stack': err.stack })
return;
}
Specifically, I'm trying to mock the utils.retrieveImageFromURI function - which has API calls and other things in it.
When I try to mock the function using spyOn I am trying it like so:
describe("FUNCTION: ingestAndSave", () => {
let fakeImageBuffer = Array(1200).fill('a').join('b'); // just get a long string
console.log(fakeImageBuffer.length) //2399
let retrieveImageFromURISpy
beforeAll(() => {
retrieveImageFromURISpy = jest.spyOn(utils, 'retrieveImageFromURI').mockReturnValue(fakeImageBuffer)
})
test("Will call retrieveImageFromURI", async () => {
await ingest.ingestAndSave({camera:TEST_CONSTANTS.validCameraObject, sourceQueueURL:"httpexamplecom", receiptHandle: "1234abcd"})
expect(retrieveImageFromURISpy).toHaveBeenCalledTimes(1)
})
afterEach(() => {
jest.resetAllMocks()
})
afterAll(() => {
jest.restoreAllMocks()
})
})
When I do this, I get a console log that imageBuffer (which is supposed to be the return of the mocked function) is undefined and that, in turn, triggers the thrown Error that "Retrieve from uri ...." ... which causes my test to fail. I know I could wrap the test call in a try/catch but the very next test will be a "does not throw error" test... so this needs to be solved.
It's not clear to me why the mockReturnValue isn't getting returned.
Other steps:
I've gone to the REAL retrieveImageFromURI function and added a console log - it is not running.
I've changed mockReturnValue to mockImplementation like so:
retrieveImageFromURISpy = jest.spyOn(utils, 'retrieveImageFromURI').mockImplementation(() => {
console.log("Here")
return fakeImageBuffer
})
And it does NOT console log 'here'. I'm unsure why not.
I have also tried to return it as a resolved Promise, like so:
retrieveImageFromURISpy = jest.spyOn(utils, 'retrieveImageFromURI').mockImplementation(() => {
console.log("Here")
return Promise.resolve(fakeImageBuffer)
})
Note, this also doesn't console log.
I've also tried to return the promise directly with a mockReturnValue:
`retrieveImageFromURISpy = jest.spyOn(utils, 'retrieveImageFromURI').mockReturnValue(Promise.resolve(fakeImageBuffer)`)
I have an Angular/Ionic app that communicates with a Django backend. I am using this.http.get() to communicate with this server (on Heroku) and the Django server should be sending the text "OK". Instead, I am either (dependent on specific usage of this.http.get()) getting an error where the statusText is the text I want, or something like Object { _isScalar: false, source: {…}, operator: {…} }
My Django code is simple:
def make(request, otherParams):
...
return HttpResponse("OK")
I know that the get() has made it to the server, because the server runs certain things when the corresponding function is called.
How do I, from the Angular frontend, detect if the Django script has sent the "OK" or not?
(The error is not due to any of various CORS policies, I have installed django-cors-headers)
EDIT:
if it's relevant, I'm on a Windows PC, testing on localhost/Firefox Nightly with Ionic 5 and Angular 9.
Here is my frontend code, cutting the irrelevant bits. The way I've made my GET request is not consistent, having tried many. This one is suggested in the below post, and still fails.
import { Component, OnInit } from '#angular/core';
import { AlertController } from '#ionic/angular';
import { HttpClient } from '#angular/common/http';
#Component({
selector: 'app-submit',
templateUrl: './submit.page.html',
styleUrls: ['./submit.page.scss'],
})
export class SubmitPage implements OnInit {
constructor(public alertController: AlertController, private http: HttpClient) { }
ngOnInit() {
}
//irrelevant variable-getting
save() {
console.log(this.list);
if (this.title == null || this.title == "") {
this.presentAlert("Uncompleted fields", "Please complete the Title field!");
}
else if (this.sub == null || this.sub == "") {
this.presentAlert("Uncompleted fields", "Please complete the Subtitle field!");
}
else if (this.content == null || this.content == "") {
this.presentAlert("Uncompleted fields", "Please complete the Content field!");
} else {
try {
if (this.list.length == 0) {
console.log(this.list);
throw "empty list";
}
//more irrelevance
}
catch{ this.presentAlert("Uncompleted fields", "Please complete the list!"); }
if (temp2) {
this.makePost();
}
}
}
makePost() {
var temp = (<root url> + encodeURIComponent(this.title) + `/` + (this.posterID).toString() + '/' + encodeURIComponent(this.sub) + '/' + encodeURIComponent(this.content) + '/' + this.happy.toString() + '/' + this.angry.toString() + `/` + this.stressy.toString() + `/` + this.energy.toString() + '/' + this.worry.toString());
console.log(temp);
this.http.get(temp).toPromise()
.then(r => console.log('response', r)).catch(error => console.error(error));
}
}
Assuming you are using the HttpClient to invoke your GET request, you need to actually do something with this.http.get().
Try doing something like this instead:
If you can use async/await
const response = await this.http.get(<url>);
If you cannot use async/await
this.http.get(<url>).then(r => console.log('response', r) ).catch( error => console.error(error) );
If you just do:
const response = this.http.get(<url>);
console.log(response);
You are effectively logging the Promise and not the resolved Promise that holds the data you're after.
If you can show more code from your Angular app, it would help determine if this is your problem or not. For basic troubleshooting, I would first validate that your GET request (in your Python app) works by itself. Using Postman, you can test this (along with methods). If you GET request works fine, then the issue is more than likely something in you angular app which I described how to fix above.
It turned out that my Angular script was trying to interpret the response as JSON, not the plaintext I wanted. Using the code from the answer by mwilson and adding { responseType: 'text' } into the get() parameters, the console now logs the response successfully.
My The code snippet now looks like this: this.http.get(url, { responseType:'text'}).toPromise().then(r => console.log(r)).catch(error => console.error(error));
BTW feel free to point out any improvements/optimizations to the above code if you feel it needs it.
I have a route in akka-http app which is integrated with a 3rd party service via Http().cachedHostConnectionPoolHttps. I want to test it in a right way. But not sure how it should be :(
Here is how this route looks like:
val routes: Route = pathPrefix("access-tokens") {
pathPrefix(Segment) { userId =>
parameters('refreshToken) { refreshToken =>
onSuccess(accessTokenActor ? GetAccessToken(userId, refreshToken)) {
case token: AccessToken => complete(ok(token.toJson))
case AccessTokenError => complete(internalServerError("There was problems while retriving the access token"))
}
}
}
}
Behind this route hides accessTokenActor where all logic happens, here it is:
class AccessTokenActor extends Actor with ActorLogging with APIConfig {
implicit val actorSystem = context.system
import context.dispatcher
implicit val materializer = ActorMaterializer()
import AccessTokenActor._
val connectionFlow = Http().cachedHostConnectionPoolHttps[String]("www.service.token.provider.com")
override def receive: Receive = {
case get: GetAccessToken => {
val senderActor = sender()
Source.fromFuture(Future.successful(
HttpRequest(
HttpMethods.GET,
"/oauth2/token",
Nil,
FormData(Map(
"clientId" -> youtubeClientId,"clientSecret" -> youtubeSecret,"refreshToken" -> get.refreshToken))
.toEntity(HttpCharsets.`UTF-8`)) -> get.channelId
)
)
.via(connectionFlow)
.map {
case (Success(resp), id) => resp.status match {
case StatusCodes.OK => Unmarshal(resp.entity).to[AccessTokenModel]
.map(senderActor ! AccessToken(_.access_token))
case _ => senderActor ! AccessTokenError
}
case _ => senderActor ! AccessTokenError
}
}.runWith(Sink.head)
case _ => log.info("Unknown message")
}
}
So the question is how it's better to test this route, keeping in mind that the actor with the stream also exist under its hood.
Composition
One difficulty with testing your route logic, as currently organized, is that it is hard to isolate functionality. It is impossible to test your Route logic without an Actor, and it is hard to test your Actor querying without a Route.
I think you would be better served with function composition, that way you can isolate what it is you're trying to test.
First abstract away the Actor querying (ask):
sealed trait TokenResponse
case class AccessToken() extends TokenResponse {...}
case object AccessTokenError extends TokenResponse
val queryActorForToken : (ActorRef) => (GetAccessToken) => Future[TokenResponse] =
(ref) => (getAccessToken) => (ref ? getAccessToken).mapTo[TokenResponse]
Now convert your routes value into a higher-order method which takes in the query function as a parameter:
val actorRef : ActorRef = ??? //not shown in question
type TokenQuery = GetAccessToken => Future[TokenResponse]
val actorTokenQuery : TokenQuery = queryActorForToken(actorRef)
val errorMsg = "There was problems while retriving the access token"
def createRoute(getToken : TokenQuery = actorTokenQuery) : Route =
pathPrefix("access-tokens") {
pathPrefix(Segment) { userId =>
parameters('refreshToken) { refreshToken =>
onSuccess(getToken(GetAccessToken(userId, refreshToken))) {
case token: AccessToken => complete(ok(token.toJson))
case AccessTokenError => complete(internalServerError(errorMsg))
}
}
}
}
//original routes
val routes = createRoute()
Testing
Now you can test queryActorForToken without needing a Route and you can test the createRoute method without needing an actor!
You can test createRoute with an injected function that always returns a pre-defined token:
val testToken : AccessToken = ???
val alwaysSuccceedsRoute = createRoute(_ => Success(testToken))
Get("/access-tokens/fooUser?refreshToken=bar" ~> alwaysSucceedsRoute ~> check {
status shouldEqual StatusCodes.Ok
responseAs[String] shouldEqual testToken.toJson
}
Or, you can test createRoute with an injected function that never returns a token:
val alwaysFailsRoute = createRoute(_ => Success(AccessTokenError))
Get("/access-tokens/fooUser?refreshToken=bar" ~> alwaysFailsRoute ~> check {
status shouldEqual StatusCodes.InternalServerError
responseAs[String] shouldEqual errorMsg
}
I want to test a controller action using Action composition.
Here's an example of the composed action and its test code.
The Secured trait:
trait Secured {
def username(request: RequestHeader) = request.session.get(Security.username)
def onUnauthorized(request: RequestHeader) = Results.Redirect(routes.Auth.login)
def withAuth(f: => String => Request[AnyContent] => Result) = {
Security.Authenticated(username, onUnauthorized) { user =>
Action(request => f(user)(request))
}
}
The controller:
MyController extends Contrller with Secured {
def simple = Action { Ok("ok") }
def simpleWithauth = withAuth { implicit username => implicit request=> Ok("ok") }
}
The test code:
// This work fine
val result1 = controller.simple()(FakeRequest())
// This wont compile
val result2 = controller.simpleWithAuth()(FakeRequest())
The latter would require a Request[Action[AnyContent], AnyContent]
but FakeRequest returns a Request[AnyContent]
Any pointers on how to create a fake request of the appropriate type?
This is what I had to do to test secured action
def wrappedActionResult[A](wrapped: Action[(Action[A], A)], request: Request[A]): Result = wrapped.parser(request).run.await.get match {
case Left(errorResult) => errorResult
case Right((innerAction, _)) => innerAction(request)
}
and the test
running(app) {
val result = wrappedActionResult(FakeController().securedAction, invalidRequest)
status(result) must_== UNAUTHORIZED
contentAsString(result) must_== "must be authenticated"
}
That doesn't work with Play 2.1 though, types have been changed...
UPDATE:
in Play 2.1 it's even easier
I've added some sugar
implicit class ActionExecutor(action: EssentialAction) {
def process[A](request: Request[A]): Result = concurrent.Await.result(action(request).run, Duration(1, "sec"))
}
and the test now looks like this
running(app) {
val result = FakeController().securedAction process invalidRequest
status(result) must_== UNAUTHORIZED
contentAsString(result) must_== "must be authenticated"
}
UPDATE: here is a related blog post i wrote sometime ago
http://www.daodecode.com/blog/2013/03/08/testing-security-dot-authenticated-in-play-2-dot-0-and-2-dot-1/
Have you tried using the routeAndCall approach?
val Some(result) = routeAndCall(FakeRequest(POST, "/someRoute"))
You can add whatever parameters you need in your request with the asFormUrlEncodedBody method and/or adding things to the session with the withSession method.
how to test such a method:
public function add() {
if (!empty($this->request->data)) {
$this->Contest->create();
if ($this->Contest->saveAll($this->request->data)) {
$contestStage['name'] = 'First - ' . $this->request->data['Contest']['name'];
$contestStage['contest_id'] = $this->Contest->id;
if ($this->Contest->ContestStage->save($contestStage)) {
$this->setMessage(__ADD_OK, 'Konkurs');
$this->redirect(array(
'action' => 'view',
$this->Contest->id
));
} else {
$this->setMessage(__ADD_ERROR, 'Konkurs');
}
} else {
$this->setMessage(__ADD_ERROR, 'Konkurs');
}
}
}
my test method:
public function testAdd() {
$this->generateWithAuth(self::ADMIN); // genereting controller here
$url = $this->getUrl('add');
$options2 = array(
'method' => 'post',
'data' => array(
'Contest' => array(
'id' => 3,
'owner_id' => 1,
'name' => 'Testing',
'created' => '2012-11-16 12:02:33.946',
),
),
);
$this->testAction($url, $options2);
$this->assertArrayHasKey('Location', $this->headers, 'No redirection');
$this->assertEquals($this->Contest->hasAny(array('Contest.name' => 'Testing')), true);
$messages = Set::extract('{flash}.message', CakeSession::read('Message'));
}
what i receive is
PDOEXCEPTION
SQLSTATE[23505]: Unique violation: 7 BŁĄD: double key value violates a constraint
uniqueness "contest_stages_pkey" DETAIL: Key (id)=(1) alredy exists.
Because it's true i have a contestStage with id=1
why its not using next one ;<
Its kinda strange that Cakephp does not document how to test that well.
The problem is that your inserting the Contest id twice. You should make sure that the db that your using has a test prefix (or whatever you like) and clear the test tables.
As an alternative, you could use fixtures instead. The data produces a much better test case as it pre populates the data for you so that you know whats in the db at any time. Still make sure to use a prefix, I made the mistake once of not doing that and it blew away my entire db every time
Good luck