/* Ċ Ġ* ĠCopyright Ġ( C ) Ġ 2 0 1 4 ĠSquare , ĠInc . Ċ Ġ* Ċ Ġ* Ġ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 Ġokhttp 3 . curl Ċ Ċ import Ġjava . io . IOException Ċ import Ġokhttp 3 . RequestBody Ċ import Ġok io . Buffer Ċ import Ġorg . assertj . core . api . Assertions . assertThat Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġpic oc li . CommandLine Ċ Ċ class ĠMain Test Ġ{ ĊĠ Ġ@ Test Ġfun Ġsimple () Ġ{ ĊĠĠĠ Ġval Ġrequest Ġ= Ġfrom Args (" http :// example . com "). create Request () ĊĠĠĠ ĠassertThat ( request . method ). isEqualTo (" GET ") ĊĠĠĠ ĠassertThat ( request . url . toString ()). isEqualTo (" http :// example . com /") ĊĠĠĠ ĠassertThat ( request . body ). isNull () ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġ@ Throws ( IOException :: class ) Ġfun Ġput () Ġ{ ĊĠĠĠ Ġval Ġrequest Ġ= Ġfrom Args ("- X ", Ġ" PUT ", Ġ"- d ", Ġ" foo ", Ġ" http :// example . com "). create Request () ĊĠĠĠ ĠassertThat ( request . method ). isEqualTo (" PUT ") ĊĠĠĠ ĠassertThat ( request . url . toString ()). isEqualTo (" http :// example . com /") ĊĠĠĠ ĠassertThat ( request . body !!. content Length ()). isEqualTo ( 3 ) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġdata Post () Ġ{ ĊĠĠĠ Ġval Ġrequest Ġ= Ġfrom Args ("- d ", Ġ" foo ", Ġ" http :// example . com "). create Request () ĊĠĠĠ Ġval Ġbody Ġ= Ġrequest . body ĊĠĠĠ ĠassertThat ( request . method ). isEqualTo (" POST ") ĊĠĠĠ ĠassertThat ( request . url . toString ()). isEqualTo (" http :// example . com /") ĊĠĠĠ ĠassertThat ( body !!. contentType (). toString ()). isEqualTo ( ĊĠĠĠĠĠ Ġ" application / x - www - form - urlencoded ; Ġcharset = utf - 8 " ĊĠĠĠ Ġ) ĊĠĠĠ ĠassertThat ( body AsString ( body )). isEqualTo (" foo ") ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġdata Put () Ġ{ ĊĠĠĠ Ġval Ġrequest Ġ= Ġfrom Args ("- d ", Ġ" foo ", Ġ"- X ", Ġ" PUT ", Ġ" http :// example . com "). create Request () ĊĠĠĠ Ġval Ġbody Ġ= Ġrequest . body ĊĠĠĠ ĠassertThat ( request . method ). isEqualTo (" PUT ") ĊĠĠĠ ĠassertThat ( request . url . toString ()). isEqualTo (" http :// example . com /") ĊĠĠĠ ĠassertThat ( body !!. contentType (). toString ()). isEqualTo ( ĊĠĠĠĠĠ Ġ" application / x - www - form - urlencoded ; Ġcharset = utf - 8 " ĊĠĠĠ Ġ) ĊĠĠĠ ĠassertThat ( body AsString ( body )). isEqualTo (" foo ") ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun ĠcontentType Header () Ġ{ ĊĠĠĠ Ġval Ġrequest Ġ= Ġfrom Args ( ĊĠĠĠĠĠ Ġ"- d ", Ġ" foo ", Ġ"- H ", Ġ" Content - Type : Ġapplication / json ", ĊĠĠĠĠĠ Ġ" http :// example . com " ĊĠĠĠ Ġ). create Request () ĊĠĠĠ Ġval Ġbody Ġ= Ġrequest . body ĊĠĠĠ ĠassertThat ( request . method ). isEqualTo (" POST ") ĊĠĠĠ ĠassertThat ( request . url . toString ()). isEqualTo (" http :// example . com /") ĊĠĠĠ ĠassertThat ( body !!. contentType (). toString ()) ĊĠĠĠĠĠ Ġ. isEqualTo (" application / json ; Ġcharset = utf - 8 ") ĊĠĠĠ ĠassertThat ( body AsString ( body )). isEqualTo (" foo ") ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġrefer er () Ġ{ ĊĠĠĠ Ġval Ġrequest Ġ= Ġfrom Args ("- e ", Ġ" foo ", Ġ" http :// example . com "). create Request () ĊĠĠĠ ĠassertThat ( request . method ). isEqualTo (" GET ") ĊĠĠĠ ĠassertThat ( request . url . toString ()). isEqualTo (" http :// example . com /") ĊĠĠĠ ĠassertThat ( request . header (" Refer er ")). isEqualTo (" foo ") ĊĠĠĠ ĠassertThat ( request . body ). isNull () ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġuser Agent () Ġ{ ĊĠĠĠ Ġval Ġrequest Ġ= Ġfrom Args ("- A ", Ġ" foo ", Ġ" http :// example . com "). create Request () ĊĠĠĠ ĠassertThat ( request . method ). isEqualTo (" GET ") ĊĠĠĠ ĠassertThat ( request . url . toString ()). isEqualTo (" http :// example . com /") ĊĠĠĠ ĠassertThat ( request . header (" User - Agent ")). isEqualTo (" foo ") ĊĠĠĠ ĠassertThat ( request . body ). isNull () ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġdefault UserAgent () Ġ{ ĊĠĠĠ Ġval Ġrequest Ġ= Ġfrom Args (" http :// example . com "). create Request () ĊĠĠĠ ĠassertThat ( request . header (" User - Agent ")). startsWith (" ok curl /") ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġheader Split With Date () Ġ{ ĊĠĠĠ Ġval Ġrequest Ġ= Ġfrom Args ( ĊĠĠĠĠĠ Ġ"- H ", Ġ" If - Modified - Since : ĠMon , Ġ 1 8 ĠAug Ġ 2 0 1 4 Ġ 1 5 : 1 6 : 0 6 ĠGMT ", ĊĠĠĠĠĠ Ġ" http :// example . com " ĊĠĠĠ Ġ). create Request () ĊĠĠĠ ĠassertThat ( request . header (" If - Modified - Since ")). isEqualTo ( ĊĠĠĠĠĠ Ġ" Mon , Ġ 1 8 ĠAug Ġ 2 0 1 4 Ġ 1 5 : 1 6 : 0 6 ĠGMT " ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠ Ġfun Ġfrom Args ( var arg Ġargs : ĠString ?): ĠMain Ġ{ ĊĠĠĠĠĠ Ġreturn ĠCommandLine . populate Command ( Main (), Ġ* args ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġbody AsString ( body : ĠRequest Body ?): ĠString Ġ{ ĊĠĠĠĠĠ Ġreturn Ġtry Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġbuffer Ġ= ĠBuffer () ĊĠĠĠĠĠĠĠ Ġbody !!. writeTo ( buffer ) ĊĠĠĠĠĠĠĠ Ġbuffer . readString ( body . contentType () !!. charset () !! ) ĊĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠIOException ) Ġ{ ĊĠĠĠĠĠĠĠ Ġthrow ĠRuntimeException ( e ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ