accessing the same ELFileBody twice

Hi there,
we decided to secure out communication using HMAC-authorization. in out unsecured version we use something like the following

http(“do something”).post(url).body(ELFileBody(“foo/baz.ssp”)).asJSON

.headers(Headers.default)

.check(status.is(201))

and it works fine

Now will calculate the hmac-code for the authorization-header element as a function of the body:

val body = …

http(“do something”).post(url).body(body).asJSON

.headers(calculateHeaders(body)

.check(status.is(201))

So my question as a scala beginner, using Gatling-2.00M3 nightly build from April 2014 is what is the best way to access the body sting in both, the body element of a post-object and in the header calculation

Thanks in advance

-ulrich

Signature calculator was introduced in master on may, 22th:
https://github.com/excilys/gatling/pull/1886

https://github.com/excilys/gatling/blob/master/gatling-http/src/main/scala/io/gatling/http/request/builder/RequestBuilder.scala#L106-L109

You’ll first have to upgrade.

Then, ping if you need help using it (it’s an AsyncHttpClient API).

Hi Stéphane,
since i’m not an experienced Scala user, i do not understand how the usage of the signature calculator will solve my problem, which is the following one:
we use a function, which calculates the header map in dependence (among others) of the bodies JSON string, some constants and the name of the http method.
The constants are stored in the session object and can be accessed via ${name} expressions. However, to add the body to the http-request and to calculate the header map
we have to call

  • ELFileBody(“foo/baz.ssp”) in body and
  • ELFileBody(“foo/baz.ssp”).toString in the headers(calculateHeaders(…)),
    wich wastes ressources.

So we are looking for a way to evaluate the ELFileBody directive only once and to store the result somewhere to access it later.

–Ulrich

Why access it later?
SignatureCalculator let you generate your header at the last moment, after the body and the other headers have been resolved.
I only have my phone today so I can’t post code.
SignatureCalculator is a Java API from AsyncHttpClient, not a Gatling Scala one

Hi Stéphane,
you are completely right, using the SignatureCalculator is an elegant solution, but an example of usage would be very helpful.

BTW, my solution with ELFileBody(“foo/baz.ssp”).toString() doesn’t work because it returns ‘StringBody()’ and not the string.

How can one access the string in a StringBody-object?

– Ulrich

In the SG request parameter, look for getStringBody or something.

Hi Stephane,
please could you a short explanation or a link where i could find how to use the Signature calculator.

Please read the javadoc: https://github.com/AsyncHttpClient/async-http-client/blob/1.8.x/src/main/java/com/ning/http/client/SignatureCalculator.java

And here’s an example (a OAuth implementation): https://github.com/AsyncHttpClient/async-http-client/blob/1.8.x/src/main/java/com/ning/http/client/oauth/OAuthSignatureCalculator.java

Hi Stéphane,
my implementation of the HmacSignatureCalculator is ready (in java). However, it is still unclear, how to incorporate it in my scenarios

Thanks in advance

–Ulrich

I really don’t get how you find this difficult?!

import foo.bar.HmacSignatureCalculator

http(“do something”)
.post(url)
.body(ELFileBody(“foo/baz.ssp”))
.asJSON
.headers(Headers.default)
**.signatureCalculator(**new HmacSignatureCalculator)
.check(status.is(201))

Hi Stéphane,

  1. the signatureCalculator-directive was unknown to me

  2. if i use it, I get in the eclipse ide the following error: “value signatureCalculator is not a member of io.gatling.http.request.builder.HttpRequestWithParamsBuilder” I updated to the nightly Build from yesterday. If i use the code in the stand-alone-version of gatling, HmacSignatureCalculator is never called.

  3. I implemented the HmacSignatureCalculator similar to the OAuthSignatureCalculator.java-example as public class, but i get the warning " imported `HmacSignatureCalculator’ is permanently hidden by definition of object HmacSignatureCalculator in package …"

Hi Stéphane,

  • the signatureCalculator-directive was unknown to me

It should have been, as I sent a link to this method in my first answer in this thread. :slight_smile:

  • if i use it, I get in the eclipse ide the following error: “value signatureCalculator is not a member of io.gatling.http.request.builder.HttpRequestWithParamsBuilder” I updated to the nightly Build from yesterday.
    Your local snapshots weren’t actually updated, as we have (actually sbt has) an issue with the maven snapshot metadatas.
    https://github.com/excilys/gatling/issues/1821#issuecomment-43605084
  • If i use the code in the stand-alone-version of gatling, HmacSignatureCalculator is never called.

Where do you get your bundle?
Did you download it here? https://oss.sonatype.org/content/repositories/snapshots/io/gatling/highcharts/gatling-charts-highcharts/2.0.0-SNAPSHOT/

  • I implemented the HmacSignatureCalculator similar to the OAuthSignatureCalculator.java-example as public class, but i get the warning " imported `HmacSignatureCalculator’ is permanently hidden by definition of object HmacSignatureCalculator in package …"
    Are your HmacSignatureCalculator class and your Simulation in the same package? If so, you don’t have to import. Another possibility could be that you changed the package and didn’t clean. Just guessing.

Hi Stéphane

After reimplementing HmacSignatureCalculator in scala all tests run in gatling standalone

Great!

So if the problem still persists with eclipse/m2e, it means that your gatling artifacts are not updated.
As explained in the link I previously provided, the ONLY workaround is to manually remove them from your local repository and maybe your enterprise maven proxy if you have one. Don’t try to be smarter and run maven -U to “force” update, it won’t work.

Thanks for the hint,
Eclipse works fine now