Skip to content

Commit

Permalink
Add headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Javierlj committed Oct 25, 2019
1 parent a361600 commit 7cd0624
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.apache.http.impl.client.HttpClientBuilder
import org.slf4j.LoggerFactory


case class OrionSinkObject(content: String, url: String, contentType: ContentType.Value, method: HTTPMethod.Value)
case class OrionSinkObject(content: String, url: String, contentType: ContentType.Value, method: HTTPMethod.Value, headers: Map[String,String]= (Map[String,String]()))



Expand Down Expand Up @@ -77,6 +77,9 @@ object OrionSink {
def createHttpMsg(msg: OrionSinkObject) : HttpEntityEnclosingRequestBase= {
val httpEntity = getMethod(msg.method, msg.url)
httpEntity.setHeader("Content-type", msg.contentType.toString)
if(msg.headers.nonEmpty){
msg.headers.foreach({case(k,v) => httpEntity.setHeader(k,v)})
}
httpEntity.setEntity(new StringEntity(msg.content))
httpEntity
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ object Utils {
final val Demo = "demo"
final val Test = "/test"
final val BadContent = "BAD CONTENT"
final val All= "*/*"
final val Headers= Map(Utils.Accept->Utils.All)

final val OtherUrl = "http://localhost:9102"

Expand Down Expand Up @@ -105,11 +107,12 @@ class OrionConnectorTest extends BaseTest{
}

@Test def buildHttpPostSinkEntity : Unit = {
val os = new OrionSinkObject(Utils.Content, Utils.OrionAddress, ContentType.Plain, HTTPMethod.POST)
val os = new OrionSinkObject(Utils.Content, Utils.OrionAddress, ContentType.Plain, HTTPMethod.POST, Utils.Headers)
val httpMsg = OrionSink.createHttpMsg(os)
val content = scala.io.Source.fromInputStream(httpMsg.getEntity.getContent).mkString

Assert.assertEquals(httpMsg.getHeaders(Utils.ContentType)(0).getValue, ContentType.Plain.toString())
Assert.assertEquals(httpMsg.getHeaders(Utils.Accept)(0).getValue,Utils.All)
Assert.assertEquals(httpMsg.getMethod(), "POST")
Assert.assertEquals(content, Utils.Content)
}
Expand Down

0 comments on commit 7cd0624

Please sign in to comment.