package Ġcom . software berg Ċ Ċ import Ġcom . software berg . HttpMethod . GET Ċ import Ġcom . software berg . HttpMethod . POST Ċ import Ġorg . assertj . core . api . Assertions . assertThat Ċ import Ġorg . junit . jupiter . api . After All Ċ import Ġorg . junit . jupiter . api . BeforeEach Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġorg . mock server . integration . Client And Server . start Client And Server Ċ import Ġorg . mock server . model . HttpRequest . request Ċ import Ġorg . mock server . model . HttpResponse . response Ċ Ċ class ĠHttpClient Spec Ġ{ ĊĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġval ĠhttpClient Ġ= ĠSimple HttpClient . create () ĊĠĠĠĠĠĠĠ Ġprivate Ġval Ġmock Server Client Ġ= Ġstart Client And Server ( 1 0 8 0 ) ĊĊĠĠĠĠĠĠĠ Ġ@ After All ĊĠĠĠĠĠĠĠ Ġ@ Jvm Static ĊĠĠĠĠĠĠĠ Ġfun Ġ` it Ġshould Ġclose Ġhttp Ġclient `() Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠhttpClient . close () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` it Ġshould Ġdo ĠGET Ġrequest `() Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Ġgiven ĊĠĠĠĠĠĠĠ Ġmock Server Client .` when `( request (). with Method (" GET "). with Path ("/ get ")). respond ( response (). with StatusCode ( 2 0 0 ). with Body (" OK ")) ĊĊĠĠĠĠĠĠĠ Ġ// Ġwhen ĊĠĠĠĠĠĠĠ Ġval Ġ( statusCode , Ġ_, Ġbody ) Ġ= ĠhttpClient . execute ( HttpRequest ( GET , Ġ" http :// localhost : 1 0 8 0 / get ")). join () ĊĊĠĠĠĠĠĠĠ Ġ// Ġthen ĊĠĠĠĠĠĠĠ ĠassertThat ( statusCode ). isEqualTo ( 2 0 0 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( body ). isEqualTo (" OK ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` it Ġshould Ġdo ĠGET Ġrequest Ġwith Ġheaders `() Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Ġgiven ĊĠĠĠĠĠĠĠ Ġmock Server Client .` when `( request (). with Method (" GET "). with Path ("/ get "). with Header (" Accept ", Ġ" text / plain "). with Header (" Accept - Charset ", Ġ" utf - 8 ")). respond ( response (). with StatusCode ( 2 0 0 ). with Header (" Content - Type ", Ġ" text / plain ; Ġutf - 8 "). with Body (" OK ")) ĊĊĠĠĠĠĠĠĠ Ġ// Ġwhen ĊĠĠĠĠĠĠĠ Ġval Ġ( statusCode , Ġheaders , Ġbody ) Ġ= ĠhttpClient . execute ( HttpRequest ( GET , Ġ" http :// localhost : 1 0 8 0 / get ", ĠlistOf ( Http Header (" Accept ", Ġ" text / plain "), ĠHttp Header (" Accept - Charset ", Ġ" utf - 8 ")) )). join () ĊĊĠĠĠĠĠĠĠ Ġ// Ġthen ĊĠĠĠĠĠĠĠ ĠassertThat ( statusCode ). isEqualTo ( 2 0 0 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( body ). isEqualTo (" OK ") ĊĠĠĠĠĠĠĠ ĠassertThat ( headers ). contains ( Http Header (" Content - Type ", Ġ" text / plain ; Ġutf - 8 ")) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` it Ġshould Ġdo ĠPOST Ġrequest `() Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Ġgiven ĊĠĠĠĠĠĠĠ Ġmock Server Client .` when `( request (). with Method (" POST "). with Path ("/ post "). with Body (" request ")). respond ( response (). with StatusCode ( 2 0 1 ). with Body (" created ")) ĊĠĠĠĠĠĠĠ Ġval ĠhttpClient Ġ= ĠSimple HttpClient . create () ĊĊĠĠĠĠĠĠĠ Ġ// Ġwhen ĊĠĠĠĠĠĠĠ Ġval Ġ( statusCode , Ġ_, Ġbody ) Ġ= ĠhttpClient . execute ( HttpRequest ( POST , Ġ" http :// localhost : 1 0 8 0 / post ", Ġempty List (), Ġ" request ")). join () ĊĊĠĠĠĠĠĠĠ Ġ// Ġthen ĊĠĠĠĠĠĠĠ ĠassertThat ( statusCode ). isEqualTo ( 2 0 1 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( body ). isEqualTo (" created ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ BeforeEach ĊĠĠĠ Ġfun Ġreset Before Test () Ġ{ ĊĠĠĠĠĠĠĠ Ġmock Server Client . reset () ĊĠĠĠ Ġ} Ċ } Ċ