aboutsummaryrefslogtreecommitdiff
path: root/tests/src/test/scala
diff options
context:
space:
mode:
authoradamw <adam@warski.org>2017-07-08 20:13:08 +0200
committeradamw <adam@warski.org>2017-07-08 20:13:08 +0200
commitbc942a647d918c2c725115424be202eb040dd3ca (patch)
tree9dd63bf359dd24c70d45c80c64fb8edd5c94fd09 /tests/src/test/scala
parentf5566b659c4d52de0ff157713fe920671f23a147 (diff)
downloadsttp-bc942a647d918c2c725115424be202eb040dd3ca.tar.gz
sttp-bc942a647d918c2c725115424be202eb040dd3ca.tar.bz2
sttp-bc942a647d918c2c725115424be202eb040dd3ca.zip
Parse tests
Diffstat (limited to 'tests/src/test/scala')
-rw-r--r--tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala112
1 files changed, 67 insertions, 45 deletions
diff --git a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala
index e4ee030..cc2d2fc 100644
--- a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala
+++ b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala
@@ -27,13 +27,13 @@ class BasicTests extends FlatSpec with Matchers with BeforeAndAfterAll with Scal
complete(List("GET", "/echo", paramsToString(params)).filter(_.nonEmpty).mkString(" "))
}
} ~
- post {
- parameterMap { params =>
- entity(as[String]) { body: String =>
- complete(List("POST", "/echo", paramsToString(params), body).filter(_.nonEmpty).mkString(" "))
+ post {
+ parameterMap { params =>
+ entity(as[String]) { body: String =>
+ complete(List("POST", "/echo", paramsToString(params), body).filter(_.nonEmpty).mkString(" "))
+ }
}
}
- }
}
private implicit val actorSystem: ActorSystem = ActorSystem("sttp-test")
@@ -64,60 +64,82 @@ class BasicTests extends FlatSpec with Matchers with BeforeAndAfterAll with Scal
def runTests[R[_]](name: String, handler: SttpHandler[R, Nothing], forceResponse: ForceWrappedValue[R]): Unit = {
implicit val h = handler
- name should "make a get request with parameters" in {
- val response = sttp
- .get(new URI(endpoint + "/echo?p2=v2&p1=v1"))
- .send(responseAsString)
-
- val fc = forceResponse.force(response).body
- fc should be ("GET /echo p1=v1 p2=v2")
- }
-
val postEcho = sttp.post(new URI(endpoint + "/echo"))
val testBody = "this is the body"
val testBodyBytes = testBody.getBytes("UTF-8")
val expectedPostEchoResponse = "POST /echo this is the body"
- name should "post a string" in {
- val response = postEcho.data(testBody).send(responseAsString)
- val fc = forceResponse.force(response).body
- fc should be (expectedPostEchoResponse)
- }
+ parseResponseTests()
+ parameterTests()
+ bodyTests()
- name should "post a byte array" in {
- val response = postEcho.data(testBodyBytes).send(responseAsString)
- val fc = forceResponse.force(response).body
- fc should be (expectedPostEchoResponse)
- }
+ def parseResponseTests(): Unit = {
+ name should "parse response as string" in {
+ val response = postEcho.data(testBody).send(responseAsString)
+ val fc = forceResponse.force(response).body
+ fc should be (expectedPostEchoResponse)
+ }
- name should "post an input stream" in {
- val response = postEcho.data(new ByteArrayInputStream(testBodyBytes)).send(responseAsString)
- val fc = forceResponse.force(response).body
- fc should be (expectedPostEchoResponse)
+ name should "parse response as a byte array" in {
+ val response = postEcho.data(testBody).send(responseAsByteArray)
+ val fc = new String(forceResponse.force(response).body, "UTF-8")
+ fc should be (expectedPostEchoResponse)
+ }
}
- name should "post a byte buffer" in {
- val response = postEcho.data(ByteBuffer.wrap(testBodyBytes)).send(responseAsString)
- val fc = forceResponse.force(response).body
- fc should be (expectedPostEchoResponse)
- }
+ def parameterTests(): Unit = {
+ name should "make a get request with parameters" in {
+ val response = sttp
+ .get(new URI(endpoint + "/echo?p2=v2&p1=v1"))
+ .send(responseAsString)
- name should "post a file" in {
- val f = File.newTemporaryFile().write(testBody)
- try {
- val response = postEcho.data(f.toJava).send(responseAsString)
val fc = forceResponse.force(response).body
- fc should be(expectedPostEchoResponse)
- } finally f.delete()
+ fc should be ("GET /echo p1=v1 p2=v2")
+ }
}
- name should "post a path" in {
- val f = File.newTemporaryFile().write(testBody)
- try {
- val response = postEcho.data(f.toJava.toPath).send(responseAsString)
+ def bodyTests(): Unit = {
+ name should "post a string" in {
+ val response = postEcho.data(testBody).send(responseAsString)
+ val fc = forceResponse.force(response).body
+ fc should be (expectedPostEchoResponse)
+ }
+
+ name should "post a byte array" in {
+ val response = postEcho.data(testBodyBytes).send(responseAsString)
val fc = forceResponse.force(response).body
- fc should be(expectedPostEchoResponse)
- } finally f.delete()
+ fc should be (expectedPostEchoResponse)
+ }
+
+ name should "post an input stream" in {
+ val response = postEcho.data(new ByteArrayInputStream(testBodyBytes)).send(responseAsString)
+ val fc = forceResponse.force(response).body
+ fc should be (expectedPostEchoResponse)
+ }
+
+ name should "post a byte buffer" in {
+ val response = postEcho.data(ByteBuffer.wrap(testBodyBytes)).send(responseAsString)
+ val fc = forceResponse.force(response).body
+ fc should be (expectedPostEchoResponse)
+ }
+
+ name should "post a file" in {
+ val f = File.newTemporaryFile().write(testBody)
+ try {
+ val response = postEcho.data(f.toJava).send(responseAsString)
+ val fc = forceResponse.force(response).body
+ fc should be(expectedPostEchoResponse)
+ } finally f.delete()
+ }
+
+ name should "post a path" in {
+ val f = File.newTemporaryFile().write(testBody)
+ try {
+ val response = postEcho.data(f.toJava.toPath).send(responseAsString)
+ val fc = forceResponse.force(response).body
+ fc should be(expectedPostEchoResponse)
+ } finally f.delete()
+ }
}
}
}