Skip to content

Commit

Permalink
attempt at better query string handling #66
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Sep 25, 2015
1 parent 05d5dc8 commit af87236
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pact-jvm-consumer-junit/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

dependencies {
compile project(":pact-jvm-consumer_${project.scalaVersion}"), 'junit:junit:4.12',
'org.json:json:20140107', 'org.apache.commons:commons-lang3:3.3.2'
'org.json:json:20140107', 'org.apache.commons:commons-lang3:3.3.2', 'com.google.guava:guava:18.0'

testCompile 'ch.qos.logback:logback-core:1.1.2', 'ch.qos.logback:logback-classic:1.1.2',
'org.apache.commons:commons-collections4:4.0', 'com.google.code.gson:gson:2.3.1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
import org.apache.http.NameValuePair;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down
2 changes: 1 addition & 1 deletion pact-jvm-consumer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
dependencies {
compile project(":pact-jvm-model_${project.scalaVersion}"), project(":pact-jvm-matchers_${project.scalaVersion}"),
'com.googlecode.java-diff-utils:diffutils:1.3.0', 'dk.brics.automaton:automaton:1.11-8',
'org.apache.httpcomponents:httpclient:4.4'
'org.apache.httpcomponents:httpclient:4.5.1'

testCompile 'org.apache.httpcomponents:fluent-hc:4.4', 'org.codehaus.groovy:groovy-all:2.4.4:indy'
}
Expand Down
2 changes: 1 addition & 1 deletion pact-jvm-model/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dependencies {
compile 'org.json:json:20140107',
"net.databinder:unfiltered-netty-server_${project.scalaVersion}:0.8.2",
"net.databinder.dispatch:dispatch-core_${project.scalaVersion}:0.11.1",
'org.apache.httpcomponents:httpclient:4.4.1'
'org.apache.httpcomponents:httpclient:4.5.1'
compile 'com.github.zafarkhaja:java-semver:0.9.0'

testCompile 'org.specs2:specs2-mock_2.11:3.6.4'
Expand Down
14 changes: 10 additions & 4 deletions pact-jvm-model/src/main/scala/au/com/dius/pact/model/Pact.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package au.com.dius.pact.model

import java.net.URLDecoder

import org.apache.http.client.utils.URLEncodedUtils
import org.json4s._
import org.json4s.jackson.JsonMethods._
import scala.collection.JavaConversions
Expand Down Expand Up @@ -152,19 +155,22 @@ trait Optionals {
}
}

def optionalQuery(query: String): Option[Map[String, List[String]]] = {
def optionalQuery(query: String, decode: Boolean = false): Option[Map[String, List[String]]] = {
if(query == null || query == "") {
None
} else {
Some(query.split("&").map(_.split("=")).foldLeft(Map[String,List[String]]()) {
(m, a) => m + (a.head -> (m.getOrElse(a.head, List[String]()) :+ a.last))
(m, a) =>
val name = if (decode) URLDecoder.decode(a.head, "UTF-8") else a.head
val value = if (decode) URLDecoder.decode(a.last, "UTF-8") else a.last
m + (name -> (m.getOrElse(name, List[String]()) :+ value))
})
}
}

def optionalQuery(query: Option[String]): Option[Map[String, List[String]]] = {
def optionalQuery(query: Option[String], decode: Boolean): Option[Map[String, List[String]]] = {
if(query.isDefined) {
optionalQuery(query.get)
optionalQuery(query.get, decode)
} else {
None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.util.jar.JarInputStream

import com.github.zafarkhaja.semver.Version
import com.typesafe.scalalogging.StrictLogging
import org.apache.http.client.utils.URLEncodedUtils
import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization
Expand Down Expand Up @@ -116,9 +117,7 @@ object PactSerializer extends StrictLogging with au.com.dius.pact.model.Optional
}
}

def queryToMap(value: JValue) = {
optionalQuery(value.values.toString)
}
def queryToMap(value: JValue) = optionalQuery(value.values.toString, true)

def mapToQueryStr(queryMap: Option[Map[String, List[String]]]) = {
queryMap.getOrElse(Map()).flatMap(entry => entry._2.map(value => entry._1 + "=" + value)).mkString("&")
Expand Down
40 changes: 40 additions & 0 deletions pact-jvm-model/src/test/resources/test_pact_encoded_query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"provider" : {
"name" : "test_provider"
},
"consumer" : {
"name" : "test_consumer"
},
"interactions" : [ {
"providerState" : "test state",
"description" : "test interaction",
"request" : {
"method" : "GET",
"path" : "/",
"headers" : {
"testreqheader" : "testreqheadervalue"
},
"query" : "datetime=2011-12-03T10%3A15%3A30%2B01%3A00&description=hello+world%21",
"body" : {
"test" : true
}
},
"response" : {
"status" : 200,
"headers" : {
"testreqheader" : "testreqheaderval"
},
"body" : {
"responsetest" : true
}
}
} ],
"metadata" : {
"pact-specification" : {
"version" : "2.0.0"
},
"pact-jvm" : {
"version" : ""
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,11 @@ object Fixtures {
consumer = consumer,
interactions = interactionsWithNoBodies
)

val pactDecodedQuery = Pact(provider = provider,
consumer = consumer,
interactions = List(interaction.copy(
request = request.copy(query = Some(
Map("datetime" -> List("2011-12-03T10:15:30+01:00"), "description" -> List("hello world!"))))
)))
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,10 @@ class PactSerializerSpec extends Specification {
val pact = PactSerializer.from(loadTestFile("test_pact_no_metadata.json"))
pact must beEqualTo(Fixtures.pact)
}

"deserialize pact with encoded query string" in {
val pact = PactSerializer.from(loadTestFile("test_pact_encoded_query.json"))
pact must beEqualTo(Fixtures.pactDecodedQuery)
}
}
}

0 comments on commit af87236

Please sign in to comment.