From c174946f855ff445c787c8ddac683b73e26a0378 Mon Sep 17 00:00:00 2001 From: Alessio Stalla Date: Mon, 8 Jan 2018 22:12:47 +0100 Subject: [PATCH 1/4] Code for Alessio Stalla's evaluation article (Different Types of Bean Injection in Spring) --- .../baeldung/typesofbeaninjection/Config.java | 16 +++++ .../typesofbeaninjection/domain/Car.java | 24 ++++++++ .../typesofbeaninjection/domain/Engine.java | 24 ++++++++ .../domain/autowired/constructor/Car.java | 30 ++++++++++ .../domain/autowired/properties/Car.java | 30 ++++++++++ .../typesofbeaninjection-context.xml | 19 ++++++ .../TypeOfBeanInjectionUnitTest.java | 58 +++++++++++++++++++ 7 files changed, 201 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/Config.java create mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Car.java create mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Engine.java create mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/constructor/Car.java create mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/properties/Car.java create mode 100644 spring-core/src/main/resources/typesofbeaninjection-context.xml create mode 100644 spring-core/src/test/java/com/baeldung/typesofbeaninjection/TypeOfBeanInjectionUnitTest.java diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/Config.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/Config.java new file mode 100644 index 0000000000..a967a63005 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/Config.java @@ -0,0 +1,16 @@ +package com.baeldung.typesofbeaninjection; + +import com.baeldung.typesofbeaninjection.domain.Engine; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.baeldung.typesofbeaninjection") +public class Config { + + @Bean + public Engine engine() { + return new Engine("V8", 5); + } +} diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Car.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Car.java new file mode 100644 index 0000000000..878f369c8e --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Car.java @@ -0,0 +1,24 @@ +package com.baeldung.typesofbeaninjection.domain; + +public class Car { + private Engine engine; + + public Car() {} + + public Car(Engine engine) { + this.engine = engine; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + + @Override + public String toString() { + return String.format("Car with %s engine", engine); + } +} diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Engine.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Engine.java new file mode 100644 index 0000000000..79e7a9856d --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Engine.java @@ -0,0 +1,24 @@ +package com.baeldung.typesofbeaninjection.domain; + +public class Engine { + private final String type; + private final int volume; + + public Engine(String type, int volume) { + this.type = type; + this.volume = volume; + } + + public String getType() { + return type; + } + + public int getVolume() { + return volume; + } + + @Override + public String toString() { + return String.format("%s %d", type, volume); + } +} diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/constructor/Car.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/constructor/Car.java new file mode 100644 index 0000000000..33fadf8d2b --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/constructor/Car.java @@ -0,0 +1,30 @@ +package com.baeldung.typesofbeaninjection.domain.autowired.constructor; + +import com.baeldung.typesofbeaninjection.domain.Engine; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component("car-autowired-by-constructor") +public class Car { + private Engine engine; + + public Car() {} + + @Autowired + public Car(Engine engine) { + this.engine = engine; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + + @Override + public String toString() { + return String.format("Car with %s engine", engine); + } +} diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/properties/Car.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/properties/Car.java new file mode 100644 index 0000000000..1df33f5062 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/properties/Car.java @@ -0,0 +1,30 @@ +package com.baeldung.typesofbeaninjection.domain.autowired.properties; + +import com.baeldung.typesofbeaninjection.domain.Engine; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component("car-autowired-by-properties") +public class Car { + private Engine engine; + + public Car() {} + + public Car(Engine engine) { + this.engine = engine; + } + + public Engine getEngine() { + return engine; + } + + @Autowired + public void setEngine(Engine engine) { + this.engine = engine; + } + + @Override + public String toString() { + return String.format("Car with %s engine", engine); + } +} diff --git a/spring-core/src/main/resources/typesofbeaninjection-context.xml b/spring-core/src/main/resources/typesofbeaninjection-context.xml new file mode 100644 index 0000000000..5a9a0b59d7 --- /dev/null +++ b/spring-core/src/main/resources/typesofbeaninjection-context.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/typesofbeaninjection/TypeOfBeanInjectionUnitTest.java b/spring-core/src/test/java/com/baeldung/typesofbeaninjection/TypeOfBeanInjectionUnitTest.java new file mode 100644 index 0000000000..1b1319e001 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/typesofbeaninjection/TypeOfBeanInjectionUnitTest.java @@ -0,0 +1,58 @@ +package com.baeldung.typesofbeaninjection; + +import com.baeldung.typesofbeaninjection.domain.Car; +import com.baeldung.typesofbeaninjection.domain.Engine; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class TypeOfBeanInjectionUnitTest { + + @Test + public void whenConstructionInjectionInXML_thenCarHasEngine() { + ApplicationContext applicationContext + = new ClassPathXmlApplicationContext("/typesofbeaninjection-context.xml"); + Car car = applicationContext.getBean("alices-car", Car.class); + + checkEngine(car.getEngine()); + } + + @Test + public void whenPropertyInjectionInXML_thenCarHasEngine() { + ApplicationContext applicationContext + = new ClassPathXmlApplicationContext("/typesofbeaninjection-context.xml"); + Car car = applicationContext.getBean("bobs-car", Car.class); + + checkEngine(car.getEngine()); + } + + @Test + public void whenConstructionInjectionAnnotations_thenCarHasEngine() { + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class); + com.baeldung.typesofbeaninjection.domain.autowired.constructor.Car car + = applicationContext.getBean(com.baeldung.typesofbeaninjection.domain.autowired.constructor.Car.class); + + checkEngine(car.getEngine()); + } + + @Test + public void whenPropertyInjectionAnnotations_thenCarHasEngine() { + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class); + com.baeldung.typesofbeaninjection.domain.autowired.properties.Car car + = applicationContext.getBean(com.baeldung.typesofbeaninjection.domain.autowired.properties.Car.class); + + checkEngine(car.getEngine()); + } + + private void checkEngine(Engine engine) { + assertNotNull(engine); + assertEquals("V8", engine.getType()); + assertEquals(5, engine.getVolume()); + } + + +} From a758fd9cef9fb4ad945d623eab443e0bd1bfd901 Mon Sep 17 00:00:00 2001 From: Alessio Stalla Date: Sun, 14 Jan 2018 22:50:55 +0100 Subject: [PATCH 2/4] BAEL-1402 code for the article: Try-with-resources in Kotlin --- core-kotlin/pom.xml | 5 ++ .../kotlin/com/baeldung/kotlin/UseTest.kt | 67 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/UseTest.kt diff --git a/core-kotlin/pom.xml b/core-kotlin/pom.xml index b511f0dd7b..2cd5275eeb 100644 --- a/core-kotlin/pom.xml +++ b/core-kotlin/pom.xml @@ -44,6 +44,11 @@ kotlin-stdlib ${kotlin-stdlib.version} + + org.jetbrains.kotlin + kotlin-stdlib-jre8 + ${kotlin-stdlib.version} + org.jetbrains.kotlin kotlin-test-junit diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/UseTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/UseTest.kt new file mode 100644 index 0000000000..15bdfcafd8 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/UseTest.kt @@ -0,0 +1,67 @@ +package com.baeldung.kotlin + +import org.junit.Test +import java.beans.ExceptionListener +import java.beans.XMLEncoder +import java.io.* +import java.lang.Exception +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.test.fail + +class UseTest { + + @Test + fun givenCloseable_whenUseIsCalled_thenItIsClosed() { + val stringWriter = StringWriter() + val writer = BufferedWriter(stringWriter) //Using a BufferedWriter because after close() it throws. + writer.use { + assertEquals(writer, it) + + it.write("something") + } + try { + writer.write("something else") + + fail("write() should have thrown an exception because the writer is closed.") + } catch (e: IOException) { + //Ok + } + + assertEquals("something", stringWriter.toString()) + } + + @Test + fun givenAutoCloseable_whenUseIsCalled_thenItIsClosed() { + val baos = ByteArrayOutputStream() + val encoder = XMLEncoder(PrintStream(baos)) //XMLEncoder is AutoCloseable but not Closeable. + //Here, we use a PrintStream because after close() it throws. + encoder.exceptionListener = ThrowingExceptionListener() + encoder.use { + assertEquals(encoder, it) + + it.writeObject("something") + } + try { + encoder.writeObject("something else") + encoder.flush() + + fail("write() should have thrown an exception because the encoder is closed.") + } catch (e: IOException) { + //Ok + } + } + + @Test + fun whenSimpleFormIsUsed_thenItWorks() { + StringWriter().use { it.write("something") } + } +} + +class ThrowingExceptionListener : ExceptionListener { + override fun exceptionThrown(e: Exception?) { + if(e != null) { + throw e + } + } +} \ No newline at end of file From 3a5804aaa846d2f41501eab538d3f7fa6ed1676c Mon Sep 17 00:00:00 2001 From: Alessio Stalla Date: Thu, 18 Jan 2018 12:54:55 +0100 Subject: [PATCH 3/4] Revert "Code for Alessio Stalla's evaluation article (Different Types of Bean Injection in Spring)" This reverts commit c174946f855ff445c787c8ddac683b73e26a0378. --- .../baeldung/typesofbeaninjection/Config.java | 16 ----- .../typesofbeaninjection/domain/Car.java | 24 -------- .../typesofbeaninjection/domain/Engine.java | 24 -------- .../domain/autowired/constructor/Car.java | 30 ---------- .../domain/autowired/properties/Car.java | 30 ---------- .../typesofbeaninjection-context.xml | 19 ------ .../TypeOfBeanInjectionUnitTest.java | 58 ------------------- 7 files changed, 201 deletions(-) delete mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/Config.java delete mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Car.java delete mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Engine.java delete mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/constructor/Car.java delete mode 100644 spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/properties/Car.java delete mode 100644 spring-core/src/main/resources/typesofbeaninjection-context.xml delete mode 100644 spring-core/src/test/java/com/baeldung/typesofbeaninjection/TypeOfBeanInjectionUnitTest.java diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/Config.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/Config.java deleted file mode 100644 index a967a63005..0000000000 --- a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/Config.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.typesofbeaninjection; - -import com.baeldung.typesofbeaninjection.domain.Engine; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -@Configuration -@ComponentScan("com.baeldung.typesofbeaninjection") -public class Config { - - @Bean - public Engine engine() { - return new Engine("V8", 5); - } -} diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Car.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Car.java deleted file mode 100644 index 878f369c8e..0000000000 --- a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Car.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.typesofbeaninjection.domain; - -public class Car { - private Engine engine; - - public Car() {} - - public Car(Engine engine) { - this.engine = engine; - } - - public Engine getEngine() { - return engine; - } - - public void setEngine(Engine engine) { - this.engine = engine; - } - - @Override - public String toString() { - return String.format("Car with %s engine", engine); - } -} diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Engine.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Engine.java deleted file mode 100644 index 79e7a9856d..0000000000 --- a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/Engine.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.typesofbeaninjection.domain; - -public class Engine { - private final String type; - private final int volume; - - public Engine(String type, int volume) { - this.type = type; - this.volume = volume; - } - - public String getType() { - return type; - } - - public int getVolume() { - return volume; - } - - @Override - public String toString() { - return String.format("%s %d", type, volume); - } -} diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/constructor/Car.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/constructor/Car.java deleted file mode 100644 index 33fadf8d2b..0000000000 --- a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/constructor/Car.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.typesofbeaninjection.domain.autowired.constructor; - -import com.baeldung.typesofbeaninjection.domain.Engine; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component("car-autowired-by-constructor") -public class Car { - private Engine engine; - - public Car() {} - - @Autowired - public Car(Engine engine) { - this.engine = engine; - } - - public Engine getEngine() { - return engine; - } - - public void setEngine(Engine engine) { - this.engine = engine; - } - - @Override - public String toString() { - return String.format("Car with %s engine", engine); - } -} diff --git a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/properties/Car.java b/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/properties/Car.java deleted file mode 100644 index 1df33f5062..0000000000 --- a/spring-core/src/main/java/com/baeldung/typesofbeaninjection/domain/autowired/properties/Car.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.typesofbeaninjection.domain.autowired.properties; - -import com.baeldung.typesofbeaninjection.domain.Engine; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component("car-autowired-by-properties") -public class Car { - private Engine engine; - - public Car() {} - - public Car(Engine engine) { - this.engine = engine; - } - - public Engine getEngine() { - return engine; - } - - @Autowired - public void setEngine(Engine engine) { - this.engine = engine; - } - - @Override - public String toString() { - return String.format("Car with %s engine", engine); - } -} diff --git a/spring-core/src/main/resources/typesofbeaninjection-context.xml b/spring-core/src/main/resources/typesofbeaninjection-context.xml deleted file mode 100644 index 5a9a0b59d7..0000000000 --- a/spring-core/src/main/resources/typesofbeaninjection-context.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/typesofbeaninjection/TypeOfBeanInjectionUnitTest.java b/spring-core/src/test/java/com/baeldung/typesofbeaninjection/TypeOfBeanInjectionUnitTest.java deleted file mode 100644 index 1b1319e001..0000000000 --- a/spring-core/src/test/java/com/baeldung/typesofbeaninjection/TypeOfBeanInjectionUnitTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.baeldung.typesofbeaninjection; - -import com.baeldung.typesofbeaninjection.domain.Car; -import com.baeldung.typesofbeaninjection.domain.Engine; -import org.junit.Test; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -public class TypeOfBeanInjectionUnitTest { - - @Test - public void whenConstructionInjectionInXML_thenCarHasEngine() { - ApplicationContext applicationContext - = new ClassPathXmlApplicationContext("/typesofbeaninjection-context.xml"); - Car car = applicationContext.getBean("alices-car", Car.class); - - checkEngine(car.getEngine()); - } - - @Test - public void whenPropertyInjectionInXML_thenCarHasEngine() { - ApplicationContext applicationContext - = new ClassPathXmlApplicationContext("/typesofbeaninjection-context.xml"); - Car car = applicationContext.getBean("bobs-car", Car.class); - - checkEngine(car.getEngine()); - } - - @Test - public void whenConstructionInjectionAnnotations_thenCarHasEngine() { - ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class); - com.baeldung.typesofbeaninjection.domain.autowired.constructor.Car car - = applicationContext.getBean(com.baeldung.typesofbeaninjection.domain.autowired.constructor.Car.class); - - checkEngine(car.getEngine()); - } - - @Test - public void whenPropertyInjectionAnnotations_thenCarHasEngine() { - ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class); - com.baeldung.typesofbeaninjection.domain.autowired.properties.Car car - = applicationContext.getBean(com.baeldung.typesofbeaninjection.domain.autowired.properties.Car.class); - - checkEngine(car.getEngine()); - } - - private void checkEngine(Engine engine) { - assertNotNull(engine); - assertEquals("V8", engine.getType()); - assertEquals(5, engine.getVolume()); - } - - -} From 7000fca1971ce2c87f7d5222cd5b3b786033ceec Mon Sep 17 00:00:00 2001 From: Alessio Stalla Date: Mon, 22 Jan 2018 14:30:58 +0100 Subject: [PATCH 4/4] BAEL-1398 Code for the article: HTTP Requests with Kotlin and khttp --- core-kotlin/pom.xml | 5 + .../com/baeldung/kotlin/khttp/KhttpTest.kt | 153 ++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpTest.kt diff --git a/core-kotlin/pom.xml b/core-kotlin/pom.xml index 2cd5275eeb..33bdbf719f 100644 --- a/core-kotlin/pom.xml +++ b/core-kotlin/pom.xml @@ -49,6 +49,11 @@ kotlin-stdlib-jre8 ${kotlin-stdlib.version} + + khttp + khttp + 0.1.0 + org.jetbrains.kotlin kotlin-test-junit diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpTest.kt new file mode 100644 index 0000000000..e9147c9489 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/khttp/KhttpTest.kt @@ -0,0 +1,153 @@ +package com.baeldung.kotlin.khttp + +import khttp.structures.files.FileLike +import org.json.JSONObject +import org.junit.Test +import java.beans.ExceptionListener +import java.beans.XMLEncoder +import java.io.* +import java.lang.Exception +import java.net.ConnectException +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.test.fail + +class KhttpTest { + + @Test + fun whenHttpGetRequestIsMade_thenArgsAreReturned() { + val response = khttp.get( + url = "http://httpbin.org/get", + params = mapOf("p1" to "1", "p2" to "2")) + val args = response.jsonObject.getJSONObject("args") + + assertEquals("1", args["p1"]) + assertEquals("2", args["p2"]) + } + + @Test + fun whenAlternateHttpGetRequestIsMade_thenArgsAreReturned() { + val response = khttp.request( + method = "GET", + url = "http://httpbin.org/get", + params = mapOf("p1" to "1", "p2" to "2")) + val args = response.jsonObject.getJSONObject("args") + + assertEquals("1", args["p1"]) + assertEquals("2", args["p2"]) + } + + @Test + fun whenHeadersAreSet_thenHeadersAreSent() { + val response = khttp.get( + url = "http://httpbin.org/get", + headers = mapOf("header1" to "1", "header2" to "2")) + val headers = response.jsonObject.getJSONObject("headers") + + assertEquals("1", headers["Header1"]) + assertEquals("2", headers["Header2"]) + } + + @Test + fun whenHttpPostRequestIsMadeWithJson_thenBodyIsReturned() { + val response = khttp.post( + url = "http://httpbin.org/post", + params = mapOf("p1" to "1", "p2" to "2"), + json = mapOf("pr1" to "1", "pr2" to "2")) + + val args = response.jsonObject.getJSONObject("args") + + assertEquals("1", args["p1"]) + assertEquals("2", args["p2"]) + + val json = response.jsonObject.getJSONObject("json") + + assertEquals("1", json["pr1"]) + assertEquals("2", json["pr2"]) + } + + @Test + fun whenHttpPostRequestIsMadeWithMapData_thenBodyIsReturned() { + val response = khttp.post( + url = "http://httpbin.org/post", + params = mapOf("p1" to "1", "p2" to "2"), + data = mapOf("pr1" to "1", "pr2" to "2")) + + val args = response.jsonObject.getJSONObject("args") + + assertEquals("1", args["p1"]) + assertEquals("2", args["p2"]) + + val form = response.jsonObject.getJSONObject("form") + + assertEquals("1", form["pr1"]) + assertEquals("2", form["pr2"]) + } + + @Test + fun whenHttpPostRequestIsMadeWithFiles_thenBodyIsReturned() { + val response = khttp.post( + url = "http://httpbin.org/post", + params = mapOf("p1" to "1", "p2" to "2"), + files = listOf( + FileLike("file1", "content1"), + FileLike("file2", javaClass.getResource("KhttpTest.class").openStream().readBytes()))) + + val args = response.jsonObject.getJSONObject("args") + + assertEquals("1", args["p1"]) + assertEquals("2", args["p2"]) + + val files = response.jsonObject.getJSONObject("files") + + assertEquals("content1", files["file1"]) + } + + @Test + fun whenHttpPostRequestIsMadeWithInputStream_thenBodyIsReturned() { + val response = khttp.post( + url = "http://httpbin.org/post", + params = mapOf("p1" to "1", "p2" to "2"), + data = ByteArrayInputStream("content!".toByteArray())) + + val args = response.jsonObject.getJSONObject("args") + + assertEquals("1", args["p1"]) + assertEquals("2", args["p2"]) + + assertEquals("content!", response.jsonObject["data"]) + } + + @Test + fun whenHttpPostStreamingRequestIsMade_thenBodyIsReturnedInChunks() { + val response = khttp.post( + url = "http://httpbin.org/post", + stream = true, + json = mapOf("pr1" to "1", "pr2" to "2")) + + val baos = ByteArrayOutputStream() + response.contentIterator(chunkSize = 10).forEach { arr : ByteArray -> baos.write(arr) } + val json = JSONObject(String(baos.toByteArray())).getJSONObject("json") + + assertEquals("1", json["pr1"]) + assertEquals("2", json["pr2"]) + } + + @Test + fun whenHttpRequestFails_thenExceptionIsThrown() { + try { + khttp.get(url = "http://localhost/nothing/to/see/here") + + fail("Should have thrown an exception") + } catch (e : ConnectException) { + //Ok + } + } + + @Test + fun whenHttpNotFound_thenExceptionIsThrown() { + val response = khttp.get(url = "http://httpbin.org/nothing/to/see/here") + + assertEquals(404, response.statusCode) + } +} \ No newline at end of file