<commit_before>/**
 * Copyright 2011-2014 eBusiness Information, Groupe Excilys (www.ebusinessinformation.fr)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gatling.http.request.builder

import org.mockito.Mockito._
import org.scalatest.{ FlatSpec, Matchers }
import org.scalatest.mock.MockitoSugar

import com.ning.http.client.uri.Uri
import com.ning.http.client.{ Request, RequestBuilderBase, SignatureCalculator }

import io.gatling.core.config.GatlingConfiguration
import io.gatling.core.session.{ Session, Expression }
import io.gatling.core.validation.{ Success, Failure }
import io.gatling.http.config.HttpProtocol

class HttpRequestBuilderSpec extends FlatSpec with Matchers with MockitoSugar {

  // Default config
  GatlingConfiguration.setUpForTest()

  def mockComonAttributes() = CommonAttributes(_ => Success("attributes"), "method", Right(Uri.create("http://gatling-tool.org")))

  "request builder" should "set signature calculator object" in {
    var builder = new HttpRequestBuilder(mockComonAttributes(), HttpAttributes())
    val sigCalc = mock[Expression[SignatureCalculator]]
    builder = builder.signatureCalculator(sigCalc)

    val httpRequest = builder.build(HttpProtocol.DefaultHttpProtocol, throttled = false)
    httpRequest.signatureCalculator.get shouldBe sigCalc
  }

  // FIXME
  /* it should "set signature calculator function" in {
    var builder = new HttpRequestBuilder(mockComonAttributes(), HttpAttributes())
    val sigCalcFunc: (Request, RequestBuilderBase[_]) => Unit = mock[(Request, RequestBuilderBase[_]) => Unit]
    builder = builder.signatureCalculator(sigCalcFunc)

    val httpRequest = builder.build(HttpProtocol.DefaultHttpProtocol, throttled = false)
    val sigCalc = httpRequest.signatureCalculator.get(mock[Session])

    val mockRequest = mock[Request]
    val mockRequestBuilder = mock[RequestBuilderBase[_]]

    sigCalc match {
      case Success(sc) => sc.calculateAndAddSignature(mockRequest, mockRequestBuilder)
      case Failure(e)  => ()
    }
    verify(sigCalcFunc, times(1)).apply(mockRequest, mockRequestBuilder)
  } */
}<commit_msg>Clean up test so we don't get a SOF<commit_after>/**
 * Copyright 2011-2014 eBusiness Information, Groupe Excilys (www.ebusinessinformation.fr)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gatling.http.request.builder

import org.scalatest.{ FlatSpec, Matchers }

import com.ning.http.client.uri.Uri
import com.ning.http.client.{ Request, RequestBuilderBase, SignatureCalculator }

import io.gatling.core.config.GatlingConfiguration
import io.gatling.core.session._
import io.gatling.http.config.HttpProtocol
import io.gatling.core.test.ValidationValues

class HttpRequestBuilderSpec extends FlatSpec with Matchers with ValidationValues {

  // Default config
  GatlingConfiguration.setUpForTest()

  private def performTest(addSignatureCalculator: HttpRequestBuilder => HttpRequestBuilder): Unit = {

    val commonAttributes = CommonAttributes("requestName".expression, "GET", Right(Uri.create("http://gatling.io")))

    val builder = addSignatureCalculator(new HttpRequestBuilder(commonAttributes, HttpAttributes()))

    val httpRequestDef = builder.build(HttpProtocol.DefaultHttpProtocol, throttled = false)
    httpRequestDef.build("requestName", Session("scenarioName", "userId")).map(_.ahcRequest.getHeaders.getFirstValue("X-Token")).succeeded shouldBe "foo"
  }

  "request builder" should "set signature calculator object" in {
    val sigCalc = new SignatureCalculator {
      def calculateAndAddSignature(request: Request, rb: RequestBuilderBase[_]): Unit = rb.addHeader("X-Token", "foo")
    }

    performTest(_.signatureCalculator(sigCalc))
  }

  it should "set signature calculator function" in {
      def sigCalc(request: Request, rb: RequestBuilderBase[_]): Unit = rb.addHeader("X-Token", "foo")

    performTest(_.signatureCalculator(sigCalc _))
  }
}
{'fraction_non_alphanumeric': 0.09778859809291945, 'fraction_numerical': 0.005072022722661797, 'mean_word_length': 4.8971291866028706, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2409387', 'n_tokens_mistral': 1412, 'n_tokens_neox': 1339, 'n_words': 395}